Search in sources :

Example 61 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class RemoveTest method doUpdate.

@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
    final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
    assertNotNull(proc);
    try (final Txn transaction = transact.beginTransaction()) {
        // append some new element to records
        for (int i = 1; i <= 50; i++) {
            final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
        }
        transact.commit(transaction);
    }
    final Serializer serializer = broker.borrowSerializer();
    try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_XML_URI), LockMode.READ_LOCK)) {
        assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test.xml' should not be null", lockedDoc);
        final String data = serializer.serialize(lockedDoc.getDocument());
    } finally {
        broker.returnSerializer(serializer);
    }
    // the following transaction will not be committed and thus undone during recovery
    final Txn transaction = transact.beginTransaction();
    // remove elements
    for (int i = 1; i <= 25; i++) {
        final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        proc.reset();
    }
// DO NOT COMMIT TRANSACTION
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) Serializer(org.exist.storage.serializers.Serializer)

Example 62 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class RESTServer method writeResourceAs.

// writes out a resource, uses asMimeType as the specified mime-type or if
// null uses the type of the resource
private void writeResourceAs(final DocumentImpl resource, final DBBroker broker, final Txn transaction, final String stylesheet, final String encoding, String asMimeType, final Properties outputProperties, final HttpServletRequest request, final HttpServletResponse response) throws BadRequestException, PermissionDeniedException, IOException {
    // Do we have permission to read the resource
    if (!resource.getPermissions().validate(broker.getCurrentSubject(), Permission.READ)) {
        throw new PermissionDeniedException("Not allowed to read resource");
    }
    // get the document metadata
    final long lastModified = resource.getLastModified();
    setCreatedAndLastModifiedHeaders(response, resource.getCreated(), lastModified);
    // handle If-Modified-Since request header
    try {
        final long ifModifiedSince = request.getDateHeader("If-Modified-Since");
        if (ifModifiedSince > -1) {
            /*
                 a) A date which is later than the server's
                 current time is invalid.
                 */
            if (ifModifiedSince <= System.currentTimeMillis()) {
                /*
                     b) If the variant has been modified since the If-Modified-Since
                     date, the response is exactly the same as for a normal GET.
                     */
                if (lastModified <= ifModifiedSince) {
                    /*
                         c) If the variant has not been modified since a valid If-
                         Modified-Since date, the server SHOULD return a 304 (Not
                         Modified) response.
                         */
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }
            }
        }
    } catch (final IllegalArgumentException iae) {
        LOG.warn("Illegal If-Modified-Since HTTP Header sent on request, ignoring. {}", iae.getMessage(), iae);
    }
    if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
        if (asMimeType == null) {
            // wasn't a mime-type specified?
            asMimeType = resource.getMimeType();
        }
        if (asMimeType.startsWith("text/")) {
            response.setContentType(asMimeType + "; charset=" + encoding);
        } else {
            response.setContentType(asMimeType);
        }
        // As HttpServletResponse.setContentLength is limited to integers,
        // (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4187336)
        // next sentence:
        // response.setContentLength(resource.getContentLength());
        // must be set so
        response.addHeader("Content-Length", Long.toString(resource.getContentLength()));
        final OutputStream os = response.getOutputStream();
        broker.readBinaryResource((BinaryDocument) resource, os);
        os.flush();
    } else {
        // xml resource
        SAXSerializer sax = null;
        final Serializer serializer = broker.borrowSerializer();
        // setup the http context
        final HttpRequestWrapper reqw = new HttpRequestWrapper(request, formEncoding, containerEncoding);
        final HttpResponseWrapper resw = new HttpResponseWrapper(response);
        serializer.setHttpContext(new XQueryContext.HttpContext(reqw, resw));
        // Serialize the document
        try {
            sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            // use a stylesheet if specified in query parameters
            if (stylesheet != null) {
                serializer.setStylesheet(resource, stylesheet);
            }
            serializer.setProperties(outputProperties);
            serializer.prepareStylesheets(resource);
            if (asMimeType != null) {
                // was a mime-type specified?
                response.setContentType(asMimeType + "; charset=" + encoding);
            } else {
                if (serializer.isStylesheetApplied() || serializer.hasXSLPi(resource) != null) {
                    asMimeType = serializer.getStylesheetProperty(OutputKeys.MEDIA_TYPE);
                    if (!useDynamicContentType || asMimeType == null) {
                        asMimeType = MimeType.HTML_TYPE.getName();
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("media-type: {}", asMimeType);
                    }
                    response.setContentType(asMimeType + "; charset=" + encoding);
                } else {
                    asMimeType = resource.getMimeType();
                    response.setContentType(asMimeType + "; charset=" + encoding);
                }
            }
            if (asMimeType.equals(MimeType.HTML_TYPE.getName())) {
                outputProperties.setProperty("method", "xhtml");
                outputProperties.setProperty("media-type", "text/html; charset=" + encoding);
                outputProperties.setProperty("indent", "yes");
                outputProperties.setProperty("omit-xml-declaration", "no");
            }
            final OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), encoding);
            sax.setOutput(writer, outputProperties);
            serializer.setSAXHandlers(sax, sax);
            serializer.toSAX(resource);
            writer.flush();
            // DO NOT use in try-write-resources, otherwise ther response stream is always closed, and we can't report the errors
            writer.close();
        } catch (final SAXException saxe) {
            LOG.warn(saxe);
            throw new BadRequestException("Error while serializing XML: " + saxe.getMessage());
        } catch (final TransformerConfigurationException e) {
            LOG.warn(e);
            throw new BadRequestException(e.getMessageAndLocation());
        } finally {
            if (sax != null) {
                SerializerPool.getInstance().returnObject(sax);
            }
            broker.returnSerializer(serializer);
        }
    }
}
Also used : HttpResponseWrapper(org.exist.http.servlets.HttpResponseWrapper) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException) HttpRequestWrapper(org.exist.http.servlets.HttpRequestWrapper) PermissionDeniedException(org.exist.security.PermissionDeniedException) SAXSerializer(org.exist.util.serializer.SAXSerializer) XQuerySerializer(org.exist.util.serializer.XQuerySerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 63 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class XSLTServlet method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final String uri = (String) request.getAttribute(REQ_ATTRIBUTE_STYLESHEET);
    if (uri == null) {
        throw new ServletException("No stylesheet source specified!");
    }
    Item inputNode = null;
    final String sourceAttrib = (String) request.getAttribute(REQ_ATTRIBUTE_INPUT);
    if (sourceAttrib != null) {
        Object sourceObj = request.getAttribute(sourceAttrib);
        if (sourceObj != null) {
            if (sourceObj instanceof ValueSequence) {
                final ValueSequence seq = (ValueSequence) sourceObj;
                if (seq.size() == 1) {
                    sourceObj = seq.itemAt(0);
                }
            }
            if (sourceObj instanceof Item) {
                inputNode = (Item) sourceObj;
                if (!Type.subTypeOf(inputNode.getType(), Type.NODE)) {
                    throw new ServletException("Input for XSLT servlet is not a node. Read from attribute " + sourceAttrib);
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Taking XSLT input from request attribute {}", sourceAttrib);
                }
            } else {
                throw new ServletException("Input for XSLT servlet is not a node. Read from attribute " + sourceAttrib);
            }
        }
    }
    try {
        pool = BrokerPool.getInstance();
    } catch (final EXistException e) {
        throw new ServletException(e.getMessage(), e);
    }
    Subject user = pool.getSecurityManager().getGuestSubject();
    Subject requestUser = HttpAccount.getUserFromServletRequest(request);
    if (requestUser != null) {
        user = requestUser;
    }
    // Retrieve username / password from HTTP request attributes
    final String userParam = (String) request.getAttribute("xslt.user");
    final String passwd = (String) request.getAttribute("xslt.password");
    if (userParam != null) {
        try {
            user = pool.getSecurityManager().authenticate(userParam, passwd);
        } catch (final AuthenticationException e1) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Wrong password or user");
            return;
        }
    }
    final Stylesheet stylesheet = stylesheet(uri, request, response);
    if (stylesheet == null) {
        return;
    }
    // do the transformation
    try (final DBBroker broker = pool.get(Optional.of(user))) {
        final TransformerHandler handler = stylesheet.newTransformerHandler(broker, errorListener);
        setTransformerParameters(request, handler.getTransformer());
        final Properties properties = handler.getTransformer().getOutputProperties();
        setOutputProperties(request, properties);
        String encoding = properties.getProperty("encoding");
        if (encoding == null) {
            encoding = "UTF-8";
        }
        response.setCharacterEncoding(encoding);
        final String mediaType = properties.getProperty("media-type");
        if (mediaType != null) {
            // check, do mediaType have "charset"
            if (!mediaType.contains("charset")) {
                response.setContentType(mediaType + "; charset=" + encoding);
            } else {
                response.setContentType(mediaType);
            }
        }
        final SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final Writer writer = new BufferedWriter(response.getWriter());
        sax.setOutput(writer, properties);
        final SAXResult result = new SAXResult(sax);
        handler.setResult(result);
        final Serializer serializer = broker.borrowSerializer();
        Receiver receiver = new ReceiverToSAX(handler);
        try {
            XIncludeFilter xinclude = new XIncludeFilter(serializer, receiver);
            receiver = xinclude;
            String baseUri;
            final String base = (String) request.getAttribute(REQ_ATTRIBUTE_BASE);
            if (base != null) {
                baseUri = getServletContext().getRealPath(base);
            } else if (uri.startsWith("xmldb:exist://")) {
                baseUri = XmldbURI.xmldbUriFor(uri).getCollectionPath();
            } else {
                baseUri = getCurrentDir(request).toAbsolutePath().toString();
            }
            xinclude.setModuleLoadPath(baseUri);
            serializer.setReceiver(receiver);
            if (inputNode != null) {
                serializer.toSAX((NodeValue) inputNode);
            } else {
                final SAXToReceiver saxreceiver = new SAXToReceiver(receiver);
                final XMLReader reader = pool.getParserPool().borrowXMLReader();
                try {
                    reader.setContentHandler(saxreceiver);
                    // Handle gziped input stream
                    InputStream stream;
                    InputStream inStream = new BufferedInputStream(request.getInputStream());
                    inStream.mark(10);
                    try {
                        stream = new GZIPInputStream(inStream);
                    } catch (final IOException e) {
                        inStream.reset();
                        stream = inStream;
                    }
                    reader.parse(new InputSource(stream));
                } finally {
                    pool.getParserPool().returnXMLReader(reader);
                }
            }
        } catch (final SAXParseException e) {
            LOG.error(e.getMessage());
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        } catch (final SAXException e) {
            throw new ServletException("SAX exception while transforming node: " + e.getMessage(), e);
        } finally {
            SerializerPool.getInstance().returnObject(sax);
            broker.returnSerializer(serializer);
        }
        writer.flush();
        response.flushBuffer();
    } catch (final IOException e) {
        throw new ServletException("IO exception while transforming node: " + e.getMessage(), e);
    } catch (final TransformerException e) {
        throw new ServletException("Exception while transforming node: " + e.getMessage(), e);
    } catch (final Throwable e) {
        LOG.error(e);
        throw new ServletException("An error occurred: " + e.getMessage(), e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) AuthenticationException(org.exist.security.AuthenticationException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException) ServletException(javax.servlet.ServletException) GZIPInputStream(java.util.zip.GZIPInputStream) Item(org.exist.xquery.value.Item) XIncludeFilter(org.exist.storage.serializers.XIncludeFilter) SAXParseException(org.xml.sax.SAXParseException) ValueSequence(org.exist.xquery.value.ValueSequence) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException) Serializer(org.exist.storage.serializers.Serializer) GZIPInputStream(java.util.zip.GZIPInputStream) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) Stylesheet(org.exist.xslt.Stylesheet) DBBroker(org.exist.storage.DBBroker) SAXResult(javax.xml.transform.sax.SAXResult)

Example 64 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class Shared method getStreamSource.

public static StreamSource getStreamSource(Item item, XQueryContext context) throws XPathException, IOException {
    final StreamSource streamSource = new StreamSource();
    if (item.getType() == Type.JAVA_OBJECT) {
        LOG.debug("Streaming Java object");
        final Object obj = ((JavaObjectValue) item).getObject();
        if (!(obj instanceof File)) {
            throw new XPathException("Passed java object should be a File");
        }
        final File inputFile = (File) obj;
        final InputStream is = new FileInputStream(inputFile);
        streamSource.setInputStream(is);
        streamSource.setSystemId(inputFile.toURI().toURL().toString());
    } else if (item.getType() == Type.ANY_URI) {
        LOG.debug("Streaming xs:anyURI");
        // anyURI provided
        String url = item.getStringValue();
        // Fix URL
        if (url.startsWith("/")) {
            url = "xmldb:exist://" + url;
        }
        final InputStream is = new URL(url).openStream();
        streamSource.setInputStream(is);
        streamSource.setSystemId(url);
    } else if (item.getType() == Type.ELEMENT || item.getType() == Type.DOCUMENT) {
        LOG.debug("Streaming element or document node");
        if (item instanceof NodeProxy) {
            final NodeProxy np = (NodeProxy) item;
            final String url = "xmldb:exist://" + np.getOwnerDocument().getBaseURI();
            LOG.debug("Document detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
        // Node provided
        final DBBroker broker = context.getBroker();
        final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
            final Serializer serializer = broker.borrowSerializer();
            try {
                fn.accept(serializer);
            } finally {
                broker.returnSerializer(serializer);
            }
        };
        final NodeValue node = (NodeValue) item;
        final InputStream is = new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        streamSource.setInputStream(is);
    } else if (item.getType() == Type.BASE64_BINARY || item.getType() == Type.HEX_BINARY) {
        LOG.debug("Streaming base64 binary");
        final BinaryValue binary = (BinaryValue) item;
        final byte[] data = binary.toJavaObject(byte[].class);
        final InputStream is = new UnsynchronizedByteArrayInputStream(data);
        streamSource.setInputStream(is);
        if (item instanceof Base64BinaryDocument) {
            final Base64BinaryDocument b64doc = (Base64BinaryDocument) item;
            final String url = "xmldb:exist://" + b64doc.getUrl();
            LOG.debug("Base64BinaryDocument detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
    } else {
        LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
        throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
    return streamSource;
}
Also used : ValidationReport(org.exist.validation.ValidationReport) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeProxy(org.exist.dom.persistent.NodeProxy) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ArrayList(java.util.ArrayList) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeValue(org.exist.xquery.value.NodeValue) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) NodeImpl(org.exist.dom.memtree.NodeImpl) XQueryContext(org.exist.xquery.XQueryContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) InputSource(org.xml.sax.InputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) ValidationReportItem(org.exist.validation.ValidationReportItem) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Logger(org.apache.logging.log4j.Logger) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) DBBroker(org.exist.storage.DBBroker) Serializer(org.exist.storage.serializers.Serializer) Sequence(org.exist.xquery.value.Sequence) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) FileInputStream(java.io.FileInputStream) URL(java.net.URL) DBBroker(org.exist.storage.DBBroker) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) File(java.io.File) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)64 SAXException (org.xml.sax.SAXException)21 DBBroker (org.exist.storage.DBBroker)16 LockedDocument (org.exist.dom.persistent.LockedDocument)15 SAXSerializer (org.exist.util.serializer.SAXSerializer)14 BrokerPool (org.exist.storage.BrokerPool)11 Properties (java.util.Properties)10 Sequence (org.exist.xquery.value.Sequence)10 Txn (org.exist.storage.txn.Txn)8 XPathException (org.exist.xquery.XPathException)8 StringWriter (java.io.StringWriter)7 Collection (org.exist.collections.Collection)7 IOException (java.io.IOException)6 Item (org.exist.xquery.value.Item)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 SequenceIterator (org.exist.xquery.value.SequenceIterator)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 TransactionManager (org.exist.storage.txn.TransactionManager)4 NodeValue (org.exist.xquery.value.NodeValue)4