if (request.getCharacterEncoding() == null) request.setCharacterEncoding(formEncoding);
    public void doPost(DBBroker broker, HttpServletRequest request, HttpServletResponse response, String path) throws BadRequestException, PermissionDeniedException, IOException    {	        Properties outputProperties = new Properties(defaultProperties);        XmldbURI pathUri = XmldbURI.create(path);        DocumentImpl resource = null;        try {            // check if path leads to an XQuery resource.            // if yes, the resource is loaded and the XQuery executed.            resource = (DocumentImpl) broker.getXMLResource(pathUri, Lock.READ_LOCK);            if (resource != null) {                if (resource.getResourceType() == DocumentImpl.BINARY_FILE &&                        "application/xquery".equals(resource.getMetadata().getMimeType())) {                    // found an XQuery resource                    try {                        String result = executeXQuery(broker, resource, request, response, outputProperties);                        String encoding = outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8");                        String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE, "text/html");                        response.setContentType(mimeType);                        writeResponse(response, result, encoding);                    } catch (XPathException e) {                        response.setContentType("text/html");                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                        writeResponse(response, formatXPathException(null, path, e), "UTF-8");                    }                    return;                }            }        } finally {            if(resource != null)                resource.getUpdateLock().release(Lock.READ_LOCK);        }                // third, normal POST: read the request content and check if        // it is an XUpdate or a query request.                int howmany = 10;        int start = 1;        boolean enclose = true;        String mime = "text/xml";        String query = null;        TransactionManager transact = broker.getBrokerPool().getTransactionManager();        Txn transaction = transact.beginTransaction();        try {            String content = getRequestContent(request);            InputSource src = new InputSource(new StringReader(content));            DocumentBuilderFactory docFactory = DocumentBuilderFactory                    .newInstance();            docFactory.setNamespaceAware(true);            DocumentBuilder docBuilder = null;            try {                docBuilder = docFactory.newDocumentBuilder();            } catch (ParserConfigurationException e) {                LOG.warn(e);                transact.abort(transaction);                throw new BadRequestException(e.getMessage());            }            Document doc = docBuilder.parse(src);            Element root = doc.getDocumentElement();            String rootNS = root.getNamespaceURI();            if (rootNS != null && rootNS.equals(NS)) {                if (root.getLocalName().equals("query")) {                    // process <query>xpathQuery</query>                    String option = root.getAttribute("start");                    if (option != null)                        try {                            start = Integer.parseInt(option);                        } catch (NumberFormatException e) {                        }                    option = root.getAttribute("max");                    if (option != null)                        try {                            howmany = Integer.parseInt(option);                        } catch (NumberFormatException e) {                        }                                        option = root.getAttribute("enclose");                    if (option != null) {                        if (option.equals("no"))                            enclose = false;                    }                                        option = root.getAttribute("mime");                    mime = "text/xml";                    if ((option != null) && (!option.equals(""))) {                        mime = option;                    }                                        NodeList children = root.getChildNodes();                    for (int i = 0; i < children.getLength(); i++) {                        Node child = children.item(i);                        if (child.getNodeType() == Node.ELEMENT_NODE                                && child.getNamespaceURI().equals(NS)) {                            if (child.getLocalName().equals("text")) {                                StringBuffer buf = new StringBuffer();                                Node next = child.getFirstChild();                                while (next != null) {                                    if (next.getNodeType() == Node.TEXT_NODE                                            || next.getNodeType() == Node.CDATA_SECTION_NODE)                                        buf.append(next.getNodeValue());                                    next = next.getNextSibling();                                }                                query = buf.toString();                            } else if (child.getLocalName()                            .equals("properties")) {                                Node node = child.getFirstChild();                                while (node != null) {                                    if (node.getNodeType() == Node.ELEMENT_NODE                                            && node.getNamespaceURI()                                            .equals(NS)                                            && node.getLocalName().equals(                                            "property")) {                                        Element property = (Element) node;                                        String key = property                                                .getAttribute("name");                                        String value = property                                                .getAttribute("value");                                        LOG.debug(key + " = " + value);                                        if (key != null && value != null)                                            outputProperties.setProperty(key,                                                    value);                                    }                                    node = node.getNextSibling();                                }                            }                        }                    }                }                // execute query                if (query != null) {                    response.setContentType(mime);                    writeResponse(response, search(broker, query, path, howmany,                            start, outputProperties, enclose, request, response),                            outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));                } else {                    transact.abort(transaction);                    throw new BadRequestException("No query specified");                }            } else if (rootNS != null && rootNS.equals(XUPDATE_NS)) {                LOG.debug("Got xupdate request: " + content);                DocumentSet docs = new DocumentSet();                Collection collection = broker.getCollection(pathUri);                if (collection != null) {                    collection.allDocs(broker, docs, true, true);                } else {                    DocumentImpl xupdateDoc = (DocumentImpl) broker.getXMLResource(pathUri);                    if (xupdateDoc != null) {                        if (!xupdateDoc.getPermissions().validate(                                broker.getUser(), Permission.READ)) {                            transact.abort(transaction);                            throw new PermissionDeniedException(                                    "Not allowed to read collection");                        }                        docs.add(xupdateDoc);                    } else                        broker.getAllXMLResources(docs);                }                                XUpdateProcessor processor = new XUpdateProcessor(broker, docs, AccessContext.REST);                Modification modifications[] = processor.parse(new InputSource(                        new StringReader(content)));                long mods = 0;                for (int i = 0; i < modifications.length; i++) {                    mods += modifications[i].process(transaction);                    broker.flush();                }                transact.commit(transaction);                                // FD : Returns an XML doc                writeResponse(response,                        "<?xml version='1.0'?>\n"                        + "<exist:modifications xmlns:exist='" + NS                        + "' count='" + mods + "'>" + mods                        + "modifications processed.</exist:modifications>",                        "UTF-8");                // END FD            } else {                transact.abort(transaction);                throw new BadRequestException("Unknown XML root element: "                        + root.getNodeName());            }        } catch (SAXException e) {            transact.abort(transaction);            Exception cause = e;            if (e.getException() != null)                cause = e.getException();            LOG.debug("SAX exception while parsing request: "                    + cause.getMessage(), cause);            throw new BadRequestException(                    "SAX exception while parsing request: "                    + cause.getMessage());        } catch (ParserConfigurationException e) {            transact.abort(transaction);            throw new BadRequestException(                    "Parser exception while parsing request: " + e.getMessage());        } catch (XPathException e) {            transact.abort(transaction);            throw new BadRequestException(                    "Query exception while parsing request: " + e.getMessage());        } catch (IOException e) {            transact.abort(transaction);            throw new BadRequestException(                    "IO exception while parsing request: " + e.getMessage());        } catch (EXistException e) {            transact.abort(transaction);            throw new BadRequestException(e.getMessage());        } catch (LockException e) {            transact.abort(transaction);            throw new PermissionDeniedException(e.getMessage());        }    }