+ return parsedSone;
+ } catch (IOException ioe1) {
+ logger.log(Level.WARNING, "Could not parse Sone from " + requestUri + "!", ioe1);
+ } finally {
+ Closer.close(soneInputStream);
+ soneBucket.free();
+ }
+ return null;
+ }
+
+ /**
+ * Parses a Sone from the given input stream and creates a new Sone from the
+ * parsed data.
+ *
+ * @param originalSone
+ * The Sone to update
+ * @param soneInputStream
+ * The input stream to parse the Sone from
+ * @return The parsed Sone
+ */
+ public Sone parseSone(Sone originalSone, InputStream soneInputStream) {
+ /* TODO - impose a size limit? */
+
+ Document document;
+ /* XML parsing is not thread-safe. */
+ synchronized (this) {
+ document = XML.transformToDocument(soneInputStream);
+ }
+ if (document == null) {
+ /* TODO - mark Sone as bad. */
+ logger.log(Level.WARNING, "Could not parse XML for Sone %s!", new Object[] { originalSone });
+ return null;
+ }
+
+ Sone sone = new Sone(originalSone.getId()).setIdentity(originalSone.getIdentity());
+
+ SimpleXML soneXml;
+ try {
+ soneXml = SimpleXML.fromDocument(document);
+ } catch (NullPointerException npe1) {
+ /* for some reason, invalid XML can cause NPEs. */
+ logger.log(Level.WARNING, "XML for Sone " + sone + " can not be parsed!", npe1);
+ return null;
+ }
+
+ Integer protocolVersion = null;
+ String soneProtocolVersion = soneXml.getValue("protocol-version", null);
+ if (soneProtocolVersion != null) {
+ protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
+ }
+ if (protocolVersion == null) {
+ logger.log(Level.INFO, "No protocol version found, assuming 0.");
+ protocolVersion = 0;
+ }