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);
}
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);
}
}
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);
}
}
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();
}
}
}
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;
}
Aggregations