use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class GetThumbnailsFunction method eval.
/*
* (non-Javadoc)
*
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
* org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
ValueSequence result = new ValueSequence();
// boolean isDatabasePath = false;
boolean isSaveToDataBase = false;
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
AnyURIValue picturePath = (AnyURIValue) args[0].itemAt(0);
if (picturePath.getStringValue().startsWith("xmldb:exist://")) {
picturePath = new AnyURIValue(picturePath.getStringValue().substring(14));
}
AnyURIValue thumbPath = null;
if (args[1].isEmpty()) {
thumbPath = new AnyURIValue(picturePath.toXmldbURI().append(THUMBPATH));
isSaveToDataBase = true;
} else {
thumbPath = (AnyURIValue) args[1].itemAt(0);
if (thumbPath.getStringValue().startsWith("file:")) {
isSaveToDataBase = false;
thumbPath = new AnyURIValue(thumbPath.getStringValue().substring(5));
} else {
isSaveToDataBase = true;
try {
XmldbURI thumbsURI = XmldbURI.xmldbUriFor(thumbPath.getStringValue());
if (!thumbsURI.isAbsolute())
thumbsURI = picturePath.toXmldbURI().append(thumbPath.toString());
thumbPath = new AnyURIValue(thumbsURI.toString());
} catch (URISyntaxException e) {
throw new XPathException(this, e.getMessage());
}
}
}
// result.add(new StringValue(picturePath.getStringValue()));
// result.add(new StringValue(thumbPath.getStringValue() + " isDB?= "
// + isSaveToDataBase));
int maxThumbHeight = MAXTHUMBHEIGHT;
int maxThumbWidth = MAXTHUMBWIDTH;
if (!args[2].isEmpty()) {
maxThumbHeight = ((IntegerValue) args[2].itemAt(0)).getInt();
if (args[2].hasMany())
maxThumbWidth = ((IntegerValue) args[2].itemAt(1)).getInt();
}
String prefix = THUMBPREFIX;
if (!args[3].isEmpty()) {
prefix = args[3].itemAt(0).getStringValue();
}
// result.add(new StringValue("maxThumbHeight = " + maxThumbHeight
// + ", maxThumbWidth = " + maxThumbWidth));
BrokerPool pool = null;
try {
pool = BrokerPool.getInstance();
} catch (Exception e) {
result.add(new StringValue(e.getMessage()));
return result;
}
final DBBroker dbbroker = context.getBroker();
// Start transaction
final TransactionManager transact = pool.getTransactionManager();
try (final Txn transaction = transact.beginTransaction()) {
Collection thumbCollection = null;
Path thumbDir = null;
if (isSaveToDataBase) {
try {
thumbCollection = dbbroker.getOrCreateCollection(transaction, thumbPath.toXmldbURI());
dbbroker.saveCollection(transaction, thumbCollection);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
} else {
thumbDir = Paths.get(thumbPath.toString());
if (!Files.isDirectory(thumbDir))
try {
Files.createDirectories(thumbDir);
} catch (IOException e) {
throw new XPathException(this, e.getMessage());
}
}
Collection allPictures = null;
Collection existingThumbsCol = null;
List<Path> existingThumbsArray = null;
try {
allPictures = dbbroker.getCollection(picturePath.toXmldbURI());
if (allPictures == null) {
return Sequence.EMPTY_SEQUENCE;
}
if (isSaveToDataBase) {
existingThumbsCol = dbbroker.getCollection(thumbPath.toXmldbURI());
} else {
existingThumbsArray = FileUtils.list(thumbDir, path -> {
final String fileName = FileUtils.fileName(path);
return fileName.endsWith(".jpeg") || fileName.endsWith(".jpg");
});
}
} catch (PermissionDeniedException | IOException e) {
throw new XPathException(this, e.getMessage(), e);
}
DocumentImpl docImage = null;
BinaryDocument binImage = null;
@SuppressWarnings("unused") BufferedImage bImage = null;
@SuppressWarnings("unused") byte[] imgData = null;
Image image = null;
UnsynchronizedByteArrayOutputStream os = null;
try {
Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);
while (i.hasNext()) {
docImage = i.next();
// is not already existing??
if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(existingThumbsArray, docImage, prefix)))) {
if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
// TODO maybe extends for gifs too.
if (docImage.getMimeType().startsWith("image/jpeg")) {
binImage = (BinaryDocument) docImage;
try (final InputStream is = dbbroker.getBinaryResource(transaction, binImage)) {
image = ImageIO.read(is);
} catch (IOException ioe) {
throw new XPathException(this, ioe.getMessage());
}
try {
bImage = ImageModule.createThumb(image, maxThumbHeight, maxThumbWidth);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
if (isSaveToDataBase) {
os = new UnsynchronizedByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", os);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
try (final StringInputSource sis = new StringInputSource(os.toByteArray())) {
thumbCollection.storeDocument(transaction, dbbroker, XmldbURI.create(prefix + docImage.getFileURI()), sis, new MimeType("image/jpeg", MimeType.BINARY));
} catch (final Exception e) {
throw new XPathException(this, e.getMessage());
}
// result.add(new
// StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
// + docImage.getFileURI()));
} else {
try {
ImageIO.write(bImage, "jpg", Paths.get(thumbPath.toString() + "/" + prefix + docImage.getFileURI()).toFile());
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
// result.add(new StringValue(
// thumbPath.toString() + "/"
// + THUMBPREFIX
// + docImage.getFileURI()));
}
}
} else {
// result.add(new StringValue(""+docImage.getURI()+"|"
// + ((existingThumbsCol != null) ? ""
// + existingThumbsCol.getURI() : thumbDir
// .toString()) + "/" + prefix
// + docImage.getFileURI()));
result.add(new StringValue(docImage.getFileURI().toString()));
}
}
} catch (final PermissionDeniedException | LockException e) {
throw new XPathException(this, e.getMessage(), e);
}
try {
transact.commit(transaction);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
}
final Optional<JournalManager> journalManager = pool.getJournalManager();
journalManager.ifPresent(j -> j.flush(true, false));
dbbroker.closeDocument();
return result;
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class GZipFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// is there some data to GZip?
if (args[0].isEmpty())
return Sequence.EMPTY_SEQUENCE;
BinaryValue bin = (BinaryValue) args[0].itemAt(0);
// gzip the data
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
final GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
bin.streamBinaryTo(gzos);
gzos.flush();
gzos.finish();
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
} catch (final IOException ioe) {
throw new XPathException(this, ioe.getMessage(), ioe);
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class EncodeExiFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
EXISerializer exiSerializer;
if (args.length > 1) {
if (!args[1].isEmpty()) {
Item xsdItem = args[1].itemAt(0);
try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
exiSerializer = new EXISerializer(baos, xsdInputStream);
}
} else {
exiSerializer = new EXISerializer(baos);
}
} else {
exiSerializer = new EXISerializer(baos);
}
Item inputNode = args[0].itemAt(0);
exiSerializer.startDocument();
inputNode.toSAX(context.getBroker(), exiSerializer, new Properties());
exiSerializer.endDocument();
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
} catch (IOException ioex) {
// TODO - test!
throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
} catch (EXIException | SAXException exie) {
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream 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.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class ControllerTest method get.
private Tuple2<Integer, String> get(final String testCollectionName, final String documentName) throws IOException {
final Request request = Request.Get(getAppsUri(existWebServer) + "/" + testCollectionName + "/" + documentName);
final Tuple2<Integer, String> responseCodeAndBody = withHttpExecutor(existWebServer, executor -> {
final HttpResponse response = executor.execute(request).returnResponse();
final int sc = response.getStatusLine().getStatusCode();
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
response.getEntity().writeTo(baos);
return Tuple(sc, baos.toString(UTF_8));
}
});
return responseCodeAndBody;
}
Aggregations