Search in sources :

Example 1 with Type

use of org.exquery.xquery.Type in project exist by eXist-db.

the class RegistryFunctionsTest method outputMethod.

@Test
public void outputMethod() throws SerializationAnnotationException, TransformerException, IOException, SAXException {
    // test setup
    final String methodStr = "html5";
    final MethodAnnotation method = new MethodAnnotation();
    method.setName(SerializationAnnotationName.method.getQName());
    method.setLiterals(new Literal[] { new Literal() {

        @Override
        public Type getType() {
            return Type.STRING;
        }

        @Override
        public String getValue() {
            return methodStr;
        }
    } });
    method.initialise();
    // execute serialize method
    final MemTreeBuilder builder = new MemTreeBuilder();
    builder.startDocument();
    RegistryFunctions.serializeSerializationAnnotation(builder, method);
    builder.endDocument();
    // assert result
    final String xmlResult = documentToString(builder.getDocument());
    final Source srcExpected = Input.fromString("<method xmlns=\"http://www.w3.org/2010/xslt-xquery-serialization\">" + methodStr + "</method>").build();
    final Source srcActual = Input.fromString(xmlResult).build();
    final Diff diff = DiffBuilder.compare(srcExpected).withTest(srcActual).checkForIdentical().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Type(org.exquery.xquery.Type) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Diff(org.xmlunit.diff.Diff) Literal(org.exquery.xquery.Literal) MethodAnnotation(org.exquery.serialization.annotation.MethodAnnotation) Override(java.lang.Override) DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Example 2 with Type

use of org.exquery.xquery.Type in project exist by eXist-db.

the class RestXqServiceImpl method extractRequestBody.

@Override
protected Sequence extractRequestBody(final HttpRequest request) throws RestXqServiceException {
    // TODO don't use close shield input stream and move parsing of form parameters from HttpServletRequestAdapter into RequestBodyParser
    InputStream is;
    FilterInputStreamCache cache = null;
    try {
        // first, get the content of the request
        is = new CloseShieldInputStream(request.getInputStream());
        if (is.available() <= 0) {
            return null;
        }
        // if marking is not supported, we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
        if (!is.markSupported()) {
            cache = FilterInputStreamCacheFactory.getCacheInstance(() -> {
                final Configuration configuration = getBrokerPool().getConfiguration();
                return (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
            }, is);
            is = new CachingFilterInputStream(cache);
        }
        is.mark(Integer.MAX_VALUE);
    } catch (final IOException ioe) {
        throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
    }
    Sequence result = null;
    try {
        // was there any POST content?
        if (is != null && is.available() > 0) {
            String contentType = request.getContentType();
            // 1) determine if exists mime database considers this binary data
            if (contentType != null) {
                // strip off any charset encoding info
                if (contentType.contains(";")) {
                    contentType = contentType.substring(0, contentType.indexOf(";"));
                }
                MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
                if (mimeType != null && !mimeType.isXMLType()) {
                    // binary data
                    try {
                        final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
                        if (binaryValue != null) {
                            result = new SequenceImpl<>(new BinaryTypedValue(binaryValue));
                        }
                    } catch (final XPathException xpe) {
                        throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, xpe);
                    }
                }
            }
            if (result == null) {
                // 2) not binary, try and parse as an XML document
                final DocumentImpl doc = parseAsXml(is);
                if (doc != null) {
                    result = new SequenceImpl<>(new DocumentTypedValue(doc));
                }
            }
            if (result == null) {
                String encoding = request.getCharacterEncoding();
                // 3) not a valid XML document, return a string representation of the document
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                try {
                    // reset the stream, as we need to reuse for string parsing
                    is.reset();
                    final StringValue str = parseAsString(is, encoding);
                    if (str != null) {
                        result = new SequenceImpl<>(new StringTypedValue(str));
                    }
                } catch (final IOException ioe) {
                    throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
                }
            }
        }
    } catch (IOException e) {
        throw new RestXqServiceException(e.getMessage());
    } finally {
        if (cache != null) {
            try {
                cache.invalidate();
            } catch (final IOException ioe) {
                LOG.error(ioe.getMessage(), ioe);
            }
        }
        if (is != null) {
            /*
                 * Do NOT close the stream if its a binary value,
                 * because we will need it later for serialization
                 */
            boolean isBinaryType = false;
            if (result != null) {
                try {
                    final Type type = result.head().getType();
                    isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
                } catch (final IndexOutOfBoundsException ioe) {
                    LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
                }
            }
            if (!isBinaryType) {
                try {
                    is.close();
                } catch (final IOException ioe) {
                    LOG.error(ioe.getMessage(), ioe);
                }
            }
        }
    }
    if (result != null) {
        return result;
    } else {
        return Sequence.EMPTY_SEQUENCE;
    }
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) Configuration(org.exist.util.Configuration) DocumentTypedValue(org.exist.extensions.exquery.xdm.type.impl.DocumentTypedValue) XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) Sequence(org.exquery.xquery.Sequence) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) DocumentImpl(org.exist.dom.memtree.DocumentImpl) MimeType(org.exist.util.MimeType) BinaryTypedValue(org.exist.extensions.exquery.xdm.type.impl.BinaryTypedValue) StringTypedValue(org.exist.extensions.exquery.xdm.type.impl.StringTypedValue) MimeType(org.exist.util.MimeType) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Type(org.exquery.xquery.Type) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) StringValue(org.exist.xquery.value.StringValue) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 3 with Type

use of org.exquery.xquery.Type in project exist by eXist-db.

the class RegistryFunctionsTest method outputMediaType.

@Test
public void outputMediaType() throws URISyntaxException, TransformerException, IOException, SAXException, SerializationAnnotationException {
    // test setup
    final String internetMediaType = "application/octet-stream";
    final MediaTypeAnnotation mediaType = new MediaTypeAnnotation();
    mediaType.setName(SerializationAnnotationName.mediatype.getQName());
    mediaType.setLiterals(new Literal[] { new Literal() {

        @Override
        public Type getType() {
            return Type.STRING;
        }

        @Override
        public String getValue() {
            return internetMediaType;
        }
    } });
    mediaType.initialise();
    // execute serialize method
    final MemTreeBuilder builder = new MemTreeBuilder();
    builder.startDocument();
    RegistryFunctions.serializeSerializationAnnotation(builder, mediaType);
    builder.endDocument();
    // assert result
    final String xmlResult = documentToString(builder.getDocument());
    final Source srcExpected = Input.fromString("<media-type xmlns=\"http://www.w3.org/2010/xslt-xquery-serialization\">" + internetMediaType + "</media-type>").build();
    final Source srcActual = Input.fromString(xmlResult).build();
    final Diff diff = DiffBuilder.compare(srcExpected).withTest(srcActual).checkForIdentical().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Type(org.exquery.xquery.Type) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) MediaTypeAnnotation(org.exquery.serialization.annotation.MediaTypeAnnotation) Diff(org.xmlunit.diff.Diff) Literal(org.exquery.xquery.Literal) Override(java.lang.Override) DOMSource(javax.xml.transform.dom.DOMSource) Test(org.junit.Test)

Aggregations

Type (org.exquery.xquery.Type)3 Override (java.lang.Override)2 DOMSource (javax.xml.transform.dom.DOMSource)2 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)2 Literal (org.exquery.xquery.Literal)2 Test (org.junit.Test)2 Diff (org.xmlunit.diff.Diff)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 DocumentImpl (org.exist.dom.memtree.DocumentImpl)1 BinaryTypedValue (org.exist.extensions.exquery.xdm.type.impl.BinaryTypedValue)1 DocumentTypedValue (org.exist.extensions.exquery.xdm.type.impl.DocumentTypedValue)1 StringTypedValue (org.exist.extensions.exquery.xdm.type.impl.StringTypedValue)1 Configuration (org.exist.util.Configuration)1 MimeType (org.exist.util.MimeType)1 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)1 FilterInputStreamCache (org.exist.util.io.FilterInputStreamCache)1 XPathException (org.exist.xquery.XPathException)1 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)1