Search in sources :

Example 16 with MimeType

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

the class XMLDBLoadFromPattern method evalWithCollection.

@Override
protected Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
    final Path baseDir = Paths.get(args[1].getStringValue()).normalize();
    logger.debug("Loading files from directory: {}", baseDir.toAbsolutePath().toString());
    final Sequence patternsSeq = args[2];
    final int patternsLen = patternsSeq.getItemCount();
    final String[] includes = new String[patternsLen];
    for (int i = 0; i < patternsLen; i++) {
        includes[i] = patternsSeq.itemAt(0).getStringValue();
    }
    // determine resource type - xml or binary?
    MimeType mimeTypeFromArgs = null;
    if (getSignature().getArgumentCount() > 3 && args[3].hasOne()) {
        final String mimeTypeParam = args[3].getStringValue();
        mimeTypeFromArgs = MimeTable.getInstance().getContentType(mimeTypeParam);
        if (mimeTypeFromArgs == null) {
            throw new XPathException(this, "Unknown mime type specified: " + mimeTypeParam);
        }
    }
    // keep the directory structure?
    boolean keepDirStructure = false;
    if (getSignature().getArgumentCount() >= 5) {
        keepDirStructure = args[4].effectiveBooleanValue();
    }
    final String[] excludes;
    if (getSignature().getArgumentCount() == 6) {
        final Sequence excludesSeq = args[5];
        final int excludesLen = excludesSeq.getItemCount();
        excludes = new String[excludesLen];
        for (int i = 0; i < excludesLen; i++) {
            excludes[i] = excludesSeq.itemAt(i).getStringValue();
        }
    } else {
        excludes = null;
    }
    final ValueSequence stored = new ValueSequence();
    // scan for files
    final DirectoryScanner directoryScanner = new DirectoryScanner();
    directoryScanner.setIncludes(includes);
    directoryScanner.setExcludes(excludes);
    directoryScanner.setBasedir(baseDir.toFile());
    directoryScanner.setCaseSensitive(true);
    directoryScanner.scan();
    Collection col = collection;
    String relDir;
    String prevDir = null;
    // store according to each pattern
    for (final String includedFile : directoryScanner.getIncludedFiles()) {
        final Path file = baseDir.resolve(includedFile);
        try {
            if (logger.isDebugEnabled()) {
                logger.debug(file.toAbsolutePath().toString());
            }
            String relPath = file.toString().substring(baseDir.toString().length());
            final int p = relPath.lastIndexOf(java.io.File.separatorChar);
            if (p >= 0) {
                relDir = relPath.substring(0, p);
                relDir = relDir.replace(java.io.File.separatorChar, '/');
            } else {
                relDir = relPath;
            }
            if (keepDirStructure && (prevDir == null || (!relDir.equals(prevDir)))) {
                col = createCollectionPath(collection, relDir);
                prevDir = relDir;
            }
            MimeType mimeType = mimeTypeFromArgs;
            if (mimeType == null) {
                mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
                if (mimeType == null) {
                    mimeType = MimeType.BINARY_TYPE;
                }
            }
            // TODO  : these probably need to be encoded and checked for right mime type
            final Resource resource = col.createResource(FileUtils.fileName(file), mimeType.getXMLDBType());
            resource.setContent(file.toFile());
            ((EXistResource) resource).setMimeType(mimeType.getName());
            col.storeResource(resource);
            // TODO : use dedicated function in XmldbURI
            stored.add(new StringValue(col.getName() + "/" + resource.getId()));
        } catch (final XMLDBException e) {
            logger.error("Could not store file {}: {}", file.toAbsolutePath(), e.getMessage());
        }
    }
    return stored;
}
Also used : Path(java.nio.file.Path) XPathException(org.exist.xquery.XPathException) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) MimeType(org.exist.util.MimeType) EXistResource(org.exist.xmldb.EXistResource) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ValueSequence(org.exist.xquery.value.ValueSequence) Collection(org.xmldb.api.base.Collection) StringValue(org.exist.xquery.value.StringValue)

Example 17 with MimeType

use of org.exist.util.MimeType 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)

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