Search in sources :

Example 6 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType in project exist by eXist-db.

the class FileReadBinary method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole()) {
        XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
        logger.error("Invalid user", xPathException);
        throw xPathException;
    }
    final String inputPath = args[0].getStringValue();
    final Path file = FileModuleHelper.getFile(inputPath);
    return BinaryValueFromFile.getInstance(context, new Base64BinaryValueType(), file);
}
Also used : Path(java.nio.file.Path) XPathException(org.exist.xquery.XPathException) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType)

Example 7 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType in project exist by eXist-db.

the class InflateFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to inflate?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    final BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    boolean rawflag = false;
    if (args.length > 1 && !args[1].isEmpty())
        rawflag = args[1].itemAt(0).convertTo(Type.BOOLEAN).effectiveBooleanValue();
    Inflater infl = new Inflater(rawflag);
    // uncompress the data
    try (final InflaterInputStream iis = new InflaterInputStream(bin.getInputStream(), infl);
        final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        int read = -1;
        final byte[] b = new byte[4096];
        while ((read = iis.read(b)) != -1) {
            baos.write(b, 0, read);
        }
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (final IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) InflaterInputStream(java.util.zip.InflaterInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Inflater(java.util.zip.Inflater) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 8 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType in project exist by eXist-db.

the class DeflateFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is there some data to Deflate?
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    BinaryValue bin = (BinaryValue) args[0].itemAt(0);
    boolean rawflag = false;
    if (args.length > 1 && !args[1].isEmpty())
        rawflag = args[1].itemAt(0).convertTo(Type.BOOLEAN).effectiveBooleanValue();
    Deflater defl = new Deflater(java.util.zip.Deflater.DEFAULT_COMPRESSION, rawflag);
    // deflate the data
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(baos, defl)) {
        bin.streamBinaryTo(dos);
        dos.flush();
        dos.finish();
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
    } catch (IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    }
}
Also used : Deflater(java.util.zip.Deflater) XPathException(org.exist.xquery.XPathException) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 9 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType in project exist by eXist-db.

the class RenderFunction method eval.

/*
     * Actual implementation of the rendering process. When a function in this
     * module is called, this method is executed with the given inputs. @param
     * Sequence[] args (XSL-FO, mime-type, parameters) @param Sequence
     * contextSequence (default sequence)
     *
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
     *      org.exist.xquery.value.Sequence)
     */
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    // process
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Item inputNode = args[0].itemAt(0);
    // get media-type
    final String mediaType = args[1].getStringValue();
    // get parameters
    final Properties parameters;
    if (!args[2].isEmpty()) {
        parameters = ParametersExtractor.parseParameters(((NodeValue) args[2].itemAt(0)).getNode());
    } else {
        parameters = new Properties();
    }
    ProcessorAdapter adapter = null;
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        adapter = ((XSLFOModule) getParentModule()).getProcessorAdapter();
        final NodeValue processorConfig = args.length == 4 ? (NodeValue) args[3].itemAt(0) : null;
        final ContentHandler contentHandler = adapter.getContentHandler(context.getBroker(), processorConfig, parameters, mediaType, baos);
        // process the XSL-FO
        contentHandler.startDocument();
        inputNode.toSAX(context.getBroker(), contentHandler, new Properties());
        contentHandler.endDocument();
        // return the result
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new ByteArrayInputStream(baos.toByteArray()));
    } catch (final IOException | SAXException se) {
        throw new XPathException(this, se.getMessage(), se);
    } finally {
        if (adapter != null) {
            adapter.cleanup();
        }
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) Properties(java.util.Properties) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 10 with Base64BinaryValueType

use of org.exist.xquery.value.Base64BinaryValueType in project exist by eXist-db.

the class GetUploadedFile method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    final String uploadParamName = args[0].getStringValue();
    final List<Path> files = request.getFileUploadParam(uploadParamName);
    if (files == null || files.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.debug("File param not found: {}", uploadParamName);
        }
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    for (final Path file : files) {
        result.add(BinaryValueFromFile.getInstance(context, new Base64BinaryValueType(), file));
    }
    return result;
}
Also used : Path(java.nio.file.Path) ValueSequence(org.exist.xquery.value.ValueSequence) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType)

Aggregations

Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)14 XPathException (org.exist.xquery.XPathException)10 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)7 IOException (java.io.IOException)6 BinaryValue (org.exist.xquery.value.BinaryValue)6 StringValue (org.exist.xquery.value.StringValue)5 InputStream (java.io.InputStream)4 BinaryValueFromInputStream (org.exist.xquery.value.BinaryValueFromInputStream)4 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 Item (org.exist.xquery.value.Item)3 Image (java.awt.Image)2 BufferedImage (java.awt.image.BufferedImage)2 Path (java.nio.file.Path)2 Properties (java.util.Properties)2 AtomicValue (org.exist.xquery.value.AtomicValue)2 NodeValue (org.exist.xquery.value.NodeValue)2 ValueSequence (org.exist.xquery.value.ValueSequence)2 RestXqServiceException (org.exquery.restxq.RestXqServiceException)2