mergeFeed(transaction,feedRoot,root,DateFormatter.toXSDDateTime(new Date()));
mergeFeed(broker,transaction,feedRoot,root,DateFormatter.toXSDDateTime(new Date()));
   public void doPut(DBBroker broker,IncomingMessage request,OutgoingMessage response)      throws BadRequestException,PermissionDeniedException,NotFoundException,EXistException   {      XmldbURI pathUri = XmldbURI.create(request.getPath());      String contentType = request.getHeader("Content-Type");      String charset = getContext().getDefaultCharset();            MimeType mime = MimeType.BINARY_TYPE;      if (contentType != null) {         int semicolon = contentType.indexOf(';');         if (semicolon>0) {            contentType = contentType.substring(0,semicolon).trim();         }         mime = MimeTable.getInstance().getContentType(contentType);         if (mime==null) {            mime = MimeType.BINARY_TYPE;         }         int equals = contentType.indexOf('=',semicolon);         if (equals>0) {            String param = contentType.substring(semicolon+1,equals).trim();            if (param.compareToIgnoreCase("charset=")==0) {               charset = param.substring(equals+1).trim();            }         }      }            if (mime.getName().equals(Atom.MIME_TYPE)) {         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();         docFactory.setNamespaceAware(true);         DocumentBuilder docBuilder = null;         Document doc = null;         try {            InputSource src = new InputSource(new InputStreamReader(request.getInputStream(),charset));            docBuilder = docFactory.newDocumentBuilder();            doc = docBuilder.parse(src);         } catch (IOException e) {            LOG.warn(e);            throw new BadRequestException(e.getMessage());         } catch (SAXException e) {            LOG.warn(e);            throw new BadRequestException(e.getMessage());         } catch (ParserConfigurationException e) {            LOG.warn(e);            throw new BadRequestException(e.getMessage());         }         Element root = doc.getDocumentElement();         String ns = root.getNamespaceURI();         if (ns==null || !ns.equals(Atom.NAMESPACE_STRING)) {            throw new BadRequestException("Any content posted with the Atom mime type must be in the Atom namespace.");         }         if (root.getLocalName().equals("feed")) {            Collection collection = broker.getCollection(pathUri);            DocumentImpl feedDoc = collection.getDocument(broker,FEED_DOCUMENT_URI);            if (feedDoc==null) {               throw new BadRequestException("Collection at "+request.getPath()+" does not exist.");            }                        if (DOM.findChild(root,Atom.NAMESPACE_STRING,"title")==null) {               throw new BadRequestException("The feed metadata sent does not contain a title.");            }                        TransactionManager transact = broker.getBrokerPool().getTransactionManager();            Txn transaction = transact.beginTransaction();            try {               feedDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);                              ElementImpl feedRoot = (ElementImpl)feedDoc.getDocumentElement();                              // Modify the feed by merging the new feed-level elements               mergeFeed(transaction,feedRoot,root,DateFormatter.toXSDDateTime(new Date()));                              // Store the feed               broker.storeXMLResource(transaction, feedDoc);               transact.commit(transaction);               response.setStatusCode(204);            } catch (LockException ex) {               transact.abort(transaction);               throw new EXistException("Cannot acquire write lock.",ex);            } finally {               if (feedDoc!=null) {                  feedDoc.getUpdateLock().release(Lock.WRITE_LOCK);               }            }         } else if (root.getLocalName().equals("entry")) {            Collection collection = broker.getCollection(pathUri);            if (collection == null) {               throw new BadRequestException("Collection "+request.getPath()+" does not exist.");            }            String id = request.getParameter("id");            if (id==null) {               throw new BadRequestException("The 'id' parameter for the entry is missing.");            }            LOG.debug("Updating entry "+id+" in collection "+request.getPath());            DocumentImpl feedDoc = null;            TransactionManager transact = broker.getBrokerPool().getTransactionManager();            Txn transaction = transact.beginTransaction();            String currentDateTime = DateFormatter.toXSDDateTime(new Date());            try {               // Get the feed               LOG.debug("Acquiring lock on feed document...");               feedDoc = collection.getDocument(broker,FEED_DOCUMENT_URI);               feedDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);               // Find the entry               FindEntry finder = new FindEntry(id);               DOM.findChildren(feedDoc.getDocumentElement(),Atom.NAMESPACE_STRING,"entry",finder);               Element entry = finder.getEntry();               mergeEntry(transaction,(ElementImpl)entry,root,currentDateTime);               // Update the feed time               DOMDB.replaceTextElement(transaction,(ElementImpl)feedDoc.getDocumentElement(),Atom.NAMESPACE_STRING,"updated",currentDateTime,true);               // Store the feed               broker.storeXMLResource(transaction, feedDoc);               transact.commit(transaction);                              // Send back the changed entry               response.setStatusCode(200);               response.setContentType(Atom.MIME_TYPE+"; charset="+charset);               OutputStreamWriter w = new OutputStreamWriter(response.getOutputStream(),charset);               Transformer identity = TransformerFactory.newInstance().newTransformer();               identity.transform(new DOMSource(entry),new StreamResult(w));               w.flush();               w.close();            } catch (LockException ex) {               transact.abort(transaction);               throw new EXistException("Cannot acquire write lock.",ex);            } catch (IOException ex) {               throw new EXistException("I/O exception during serialization of entry response.",ex);            } catch (TransformerException ex) {               throw new EXistException("Serialization error.",ex);            } finally {               if (feedDoc!=null) {                  feedDoc.getUpdateLock().release(Lock.WRITE_LOCK);               }            }         } else {            throw new BadRequestException("Unexpected element: {http://www.w3.org/2005/Atom}"+root.getLocalName());         }      } else {         TransactionManager transact = broker.getBrokerPool().getTransactionManager();         Txn transaction = transact.beginTransaction();         try {            XmldbURI docUri = pathUri.lastSegment();            XmldbURI collUri = pathUri.removeLastSegment();                        if (docUri==null || collUri==null) {               transact.abort(transaction);               throw new BadRequestException("The path is not valid: " + request.getPath());            }            Collection collection = broker.getCollection(collUri);            if (collection == null) {               transact.abort(transaction);               throw new BadRequestException("The collection does not exist: " + collUri);            }            if (collection.getDocument(broker,docUri)==null) {               transact.abort(transaction);               throw new BadRequestException("Resource "+docUri+" does not exist in collection "+collUri);            }                        File tempFile = storeInTemporaryFile(request.getInputStream(),request.getContentLength());                        if (mime.isXMLType()) {               InputStream is = new FileInputStream(tempFile);               IndexInfo info = collection.validateXMLResource(transaction, broker, docUri, new InputSource(new InputStreamReader(is,charset)));               is.close();               info.getDocument().getMetadata().setMimeType(contentType);               is = new FileInputStream(tempFile);               collection.store(transaction, broker, info, new InputSource(new InputStreamReader(is,charset)), false);               is.close();            } else {                              FileInputStream is = new FileInputStream(tempFile);               collection.addBinaryResource(transaction, broker, docUri, is, contentType, (int) tempFile.length());               is.close();            }            transact.commit(transaction);                        // TODO: Change the entry updated and send back the change?            response.setStatusCode(200);                     } catch (IOException ex) {            transact.abort(transaction);            throw new EXistException("I/O error while handling temporary files.",ex);         } catch (SAXParseException e) {            transact.abort(transaction);            throw new BadRequestException("Parsing exception at "                    + e.getLineNumber() + "/" + e.getColumnNumber() + ": "                    + e.toString());         } catch (SAXException e) {            transact.abort(transaction);            Exception o = e.getException();            if (o == null)               o = e;            throw new BadRequestException("Parsing exception: "                    + o.getMessage());         } catch (TriggerException e) {            transact.abort(transaction);            throw new PermissionDeniedException(e.getMessage());         } catch (LockException e) {            transact.abort(transaction);            throw new PermissionDeniedException(e.getMessage());         }      }   }