Search in sources :

Example 1 with MimeTable

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

the class XMLDBGetMimeType method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String path = new AnyURIValue(args[0].itemAt(0).getStringValue()).toString();
    if (path.matches("^[a-z]+://.*")) {
        // external
        final MimeTable mimeTable = MimeTable.getInstance();
        final MimeType mimeType = mimeTable.getContentTypeFor(path);
        if (mimeType != null) {
            return new StringValue(mimeType.getName());
        }
    } else {
        // database
        try {
            XmldbURI pathUri = XmldbURI.xmldbUriFor(path);
            // relative collection Path: add the current base URI
            pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
            // try to open the document and acquire a lock
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
                if (lockedDoc != null) {
                    return new StringValue(lockedDoc.getDocument().getMimeType());
                }
            }
        } catch (final Exception e) {
            logger.error(e.getMessage());
            throw new XPathException(this, e);
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : MimeTable(org.exist.util.MimeTable) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) LockedDocument(org.exist.dom.persistent.LockedDocument) StringValue(org.exist.xquery.value.StringValue) MimeType(org.exist.util.MimeType) XmldbURI(org.exist.xmldb.XmldbURI) XPathException(org.exist.xquery.XPathException)

Example 2 with MimeTable

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

the class XMLDBSetMimeType method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Get handle to Mime-type info
    final MimeTable mimeTable = MimeTable.getInstance();
    // Get first parameter
    final String pathParameter = new AnyURIValue(args[0].itemAt(0).getStringValue()).toString();
    if (pathParameter.matches("^[a-z]+://.*")) {
        throw new XPathException("Can not set mime-type for resources outside the database.");
    }
    XmldbURI pathUri = null;
    try {
        pathUri = XmldbURI.xmldbUriFor(pathParameter);
    } catch (final URISyntaxException ex) {
        logger.debug(ex.getMessage());
        throw new XPathException("Invalid path '" + pathParameter + "'");
    }
    // Verify mime-type input
    MimeType newMimeType = null;
    if (args[1].isEmpty()) {
        // No input, use default mimetype
        newMimeType = mimeTable.getContentTypeFor(pathParameter);
        if (newMimeType == null) {
            throw new XPathException("Unable to determine mimetype for '" + pathParameter + "'");
        }
    } else {
        // Mimetype is provided, check if valid
        newMimeType = mimeTable.getContentType(args[1].getStringValue());
        if (newMimeType == null) {
            throw new XPathException("mime-type '" + args[1].getStringValue() + "' is not supported.");
        }
    }
    // Get mime-type of resource
    MimeType currentMimeType = getMimeTypeStoredResource(pathUri);
    if (currentMimeType == null) {
        // stored resource has no mime-type (unexpected situation)
        // fall back to document name
        logger.debug("Resource '{}' has no mime-type, retrieve from document name.", pathUri);
        currentMimeType = mimeTable.getContentTypeFor(pathUri);
        // if extension based lookup still fails
        if (currentMimeType == null) {
            throw new XPathException("Unable to determine mime-type from path '" + pathUri + "'.");
        }
    }
    // in some cases value null is set, then allow to set to new value (repair action)
    if (newMimeType.isXMLType() != currentMimeType.isXMLType()) {
        throw new XPathException("New mime-type must be a " + currentMimeType.getXMLDBType() + " mime-type");
    }
    // At this moment it is possible to update the mimetype
    final DBBroker broker = context.getBroker();
    final BrokerPool brokerPool = broker.getBrokerPool();
    // relative collection Path: add the current base URI
    pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
    try (final Txn txn = broker.continueOrBeginTransaction();
        final LockedDocument lockedDocument = broker.getXMLResource(pathUri, LockMode.WRITE_LOCK)) {
        // try to open the document and acquire a lock
        final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
        if (doc == null) {
            // no document selected, abort
            txn.abort();
        } else {
            // set new mime-type
            doc.setMimeType(newMimeType.getName());
            // store meta data into database
            broker.storeMetadata(txn, doc);
            // commit changes
            txn.commit();
        }
    } catch (final Exception e) {
        logger.error(e.getMessage());
        throw new XPathException(this, e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : MimeTable(org.exist.util.MimeTable) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) MimeType(org.exist.util.MimeType) URISyntaxException(java.net.URISyntaxException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XPathException(org.exist.xquery.XPathException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)2 MimeTable (org.exist.util.MimeTable)2 MimeType (org.exist.util.MimeType)2 XmldbURI (org.exist.xmldb.XmldbURI)2 XPathException (org.exist.xquery.XPathException)2 AnyURIValue (org.exist.xquery.value.AnyURIValue)2 URISyntaxException (java.net.URISyntaxException)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 BrokerPool (org.exist.storage.BrokerPool)1 DBBroker (org.exist.storage.DBBroker)1 Txn (org.exist.storage.txn.Txn)1 StringValue (org.exist.xquery.value.StringValue)1