Search in sources :

Example 6 with MimeType

use of org.exist.util.MimeType 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;
}
Also used : Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) ValueSequence(org.exist.xquery.value.ValueSequence) StringInputSource(org.exist.util.StringInputSource) FunctionSignature(org.exist.xquery.FunctionSignature) PermissionDeniedException(org.exist.security.PermissionDeniedException) Cardinality(org.exist.xquery.Cardinality) MimeType(org.exist.util.MimeType) FileUtils(org.exist.util.FileUtils) BasicFunction(org.exist.xquery.BasicFunction) StringValue(org.exist.xquery.value.StringValue) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) LockException(org.exist.util.LockException) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) ImageIO(javax.imageio.ImageIO) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Path(java.nio.file.Path) XQueryContext(org.exist.xquery.XQueryContext) Iterator(java.util.Iterator) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) JournalManager(org.exist.storage.journal.JournalManager) AnyURIValue(org.exist.xquery.value.AnyURIValue) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) TransactionManager(org.exist.storage.txn.TransactionManager) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) DBBroker(org.exist.storage.DBBroker) IntegerValue(org.exist.xquery.value.IntegerValue) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BufferedImage(java.awt.image.BufferedImage) MimeType(org.exist.util.MimeType) StringInputSource(org.exist.util.StringInputSource) LockException(org.exist.util.LockException) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI) Path(java.nio.file.Path) InputStream(java.io.InputStream) AnyURIValue(org.exist.xquery.value.AnyURIValue) IntegerValue(org.exist.xquery.value.IntegerValue) JournalManager(org.exist.storage.journal.JournalManager) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool)

Example 7 with MimeType

use of org.exist.util.MimeType in project exist by eXist-db.

the class XmlrpcUpload method stream.

/**
 * Write data from a (input)stream to the specified XMLRPC url and leave
 * the input stream open.
 *
 * @param xmldbURL URL pointing to location on eXist-db server.
 * @param is Document stream
 * @throws IOException When something is wrong.
 */
public void stream(XmldbURL xmldbURL, InputStream is) throws IOException {
    LOG.debug("Begin document upload");
    try {
        // Setup xmlrpc client
        final XmlRpcClient client = new XmlRpcClient();
        final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setEncoding("UTF-8");
        config.setEnabledForExtensions(true);
        config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
        if (xmldbURL.hasUserInfo()) {
            config.setBasicUserName(xmldbURL.getUsername());
            config.setBasicPassword(xmldbURL.getPassword());
        }
        client.setConfig(config);
        String contentType = MimeType.BINARY_TYPE.getName();
        final MimeType mime = MimeTable.getInstance().getContentTypeFor(xmldbURL.getDocumentName());
        if (mime != null) {
            contentType = mime.getName();
        }
        // Initialize xmlrpc parameters
        final List<Object> params = new ArrayList<>(5);
        String handle = null;
        // Copy data from inputstream to database
        final byte[] buf = new byte[4096];
        int len;
        while ((len = is.read(buf)) > 0) {
            params.clear();
            if (handle != null) {
                params.add(handle);
            }
            params.add(buf);
            params.add(len);
            handle = (String) client.execute("upload", params);
        }
        // All data transported, now parse data on server
        params.clear();
        params.add(handle);
        params.add(xmldbURL.getCollectionPath());
        params.add(Boolean.TRUE);
        params.add(contentType);
        final Boolean result = (Boolean) client.execute("parseLocal", params);
        // Check XMLRPC result
        if (result) {
            LOG.debug("Document stored.");
        } else {
            LOG.debug("Could not store document.");
            throw new IOException("Could not store document.");
        }
    } catch (final IOException ex) {
        LOG.debug(ex);
        throw ex;
    } catch (final Exception ex) {
        LOG.debug(ex);
        throw new IOException(ex.getMessage(), ex);
    } finally {
        LOG.debug("Finished document upload");
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) ArrayList(java.util.ArrayList) XmlRpcClientConfigImpl(org.apache.xmlrpc.client.XmlRpcClientConfigImpl) IOException(java.io.IOException) URL(java.net.URL) XmldbURL(org.exist.protocolhandler.xmldb.XmldbURL) MimeType(org.exist.util.MimeType) IOException(java.io.IOException)

Example 8 with MimeType

use of org.exist.util.MimeType in project exist by eXist-db.

the class ModificationTimeTest method storeBinary.

private BinaryDocument storeBinary(final DBBroker broker, final Txn transaction, final String name, final String data, final String mimeType) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final Collection root = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
    broker.saveCollection(transaction, root);
    assertNotNull(root);
    root.storeDocument(transaction, broker, XmldbURI.create(name), new StringInputSource(data.getBytes(UTF_8)), new MimeType(mimeType, MimeType.BINARY));
    return (BinaryDocument) root.getDocument(broker, XmldbURI.create(name));
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) MimeType(org.exist.util.MimeType)

Example 9 with MimeType

use of org.exist.util.MimeType in project exist by eXist-db.

the class AbstractExtractFunction method processCompressedEntry.

/**
 * Processes a compressed entry from an archive
 *
 * @param name The name of the entry
 * @param isDirectory true if the entry is a directory, false otherwise
 * @param is an InputStream for reading the uncompressed data of the entry
 * @param filterParam is an additional param for entry filtering function
 * @param storeParam is an additional param for entry storing function
 *
 * @return the result of processing the compressed entry.
 *
 * @throws XPathException if a query error occurs
 * @throws XMLDBException if a database error occurs
 * @throws IOException if an I/O error occurs
 */
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
    String dataType = isDirectory ? "folder" : "resource";
    // call the entry-filter function
    Sequence[] filterParams = new Sequence[3];
    filterParams[0] = new StringValue(name);
    filterParams[1] = new StringValue(dataType);
    filterParams[2] = filterParam;
    Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
    if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        Sequence entryDataFunctionResult;
        Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
        if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
            Sequence[] dataParams = new Sequence[3];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
            String path = entryDataFunctionResult.itemAt(0).getStringValue();
            Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
            if (isDirectory) {
                XMLDBAbstractCollectionManipulator.createCollection(root, path);
            } else {
                Resource resource;
                Path file = Paths.get(path).normalize();
                name = FileUtils.fileName(file);
                path = file.getParent().toAbsolutePath().toString();
                Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
                MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                // copy the input data
                final byte[] entryData;
                try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                    baos.write(is);
                    entryData = baos.toByteArray();
                }
                try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                    NodeValue content = ModuleUtils.streamToXML(context, bis);
                    resource = target.createResource(name, "XMLResource");
                    ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
                    handler.startDocument();
                    content.toSAX(context.getBroker(), handler, null);
                    handler.endDocument();
                } catch (SAXException e) {
                    resource = target.createResource(name, "BinaryResource");
                    resource.setContent(entryData);
                }
                if (resource != null) {
                    if (mime != null) {
                        ((EXistResource) resource).setMimeType(mime.getName());
                    }
                    target.storeResource(resource);
                }
            }
        } else {
            // copy the input data
            final byte[] entryData;
            try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
                baos.write(is);
                entryData = baos.toByteArray();
            }
            // try and parse as xml, fall back to binary
            try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                uncompressedData = ModuleUtils.streamToXML(context, bis);
            } catch (SAXException saxe) {
                if (entryData.length > 0) {
                    try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
                        uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
                    }
                }
            }
            // call the entry-data function
            Sequence[] dataParams = new Sequence[4];
            System.arraycopy(filterParams, 0, dataParams, 0, 2);
            dataParams[2] = uncompressedData;
            dataParams[3] = storeParam;
            entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
        }
        return entryDataFunctionResult;
    }
}
Also used : Path(java.nio.file.Path) LocalCollection(org.exist.xmldb.LocalCollection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) MimeType(org.exist.util.MimeType) ContentHandler(org.xml.sax.ContentHandler) XMLResource(org.xmldb.api.modules.XMLResource) SAXException(org.xml.sax.SAXException) EXistResource(org.exist.xmldb.EXistResource) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 10 with MimeType

use of org.exist.util.MimeType in project exist by eXist-db.

the class ExistXmldbEmbeddedServer method storeResource.

public static void storeResource(final Collection collection, final String documentName, final byte[] content) throws XMLDBException {
    final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentName);
    final String type = mime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
    final Resource resource = collection.createResource(documentName, type);
    resource.setContent(content);
    collection.storeResource(resource);
}
Also used : ExternalResource(org.junit.rules.ExternalResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) MimeType(org.exist.util.MimeType)

Aggregations

MimeType (org.exist.util.MimeType)17 XPathException (org.exist.xquery.XPathException)7 IOException (java.io.IOException)6 Path (java.nio.file.Path)6 XmldbURI (org.exist.xmldb.XmldbURI)5 StringValue (org.exist.xquery.value.StringValue)5 InputStream (java.io.InputStream)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 Txn (org.exist.storage.txn.Txn)4 EXistResource (org.exist.xmldb.EXistResource)4 AnyURIValue (org.exist.xquery.value.AnyURIValue)4 Resource (org.xmldb.api.base.Resource)4 XMLResource (org.xmldb.api.modules.XMLResource)4 URISyntaxException (java.net.URISyntaxException)3 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)3 Collection (org.exist.collections.Collection)3 DocumentImpl (org.exist.dom.persistent.DocumentImpl)3 LockedDocument (org.exist.dom.persistent.LockedDocument)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2