Search in sources :

Example 11 with ManagedCollectionLock

use of org.exist.storage.lock.ManagedCollectionLock in project exist by eXist-db.

the class RESTServer method doPut.

/**
 * Handles PUT requests. The request content is stored as a new resource at
 * the specified location. If the resource already exists, it is overwritten
 * if the user has write permissions.
 *
 * The resource type depends on the content type specified in the HTTP
 * header. The content type will be looked up in the global mime table. If
 * the corresponding mime type is not a know XML mime type, the resource
 * will be stored as a binary resource.
 *
 * @param broker the database broker
 * @param transaction the database transaction
 * @param request the request
 * @param response the response
 * @param path the path of the request
 *
 * @throws BadRequestException if a bad request is made
 * @throws PermissionDeniedException if the request has insufficient permissions
 * @throws NotFoundException if the request resource cannot be found
 * @throws IOException if an I/O error occurs
 */
public void doPut(final DBBroker broker, final Txn transaction, final XmldbURI path, final HttpServletRequest request, final HttpServletResponse response) throws BadRequestException, PermissionDeniedException, IOException, NotFoundException {
    if (checkForXQueryTarget(broker, transaction, path, request, response)) {
        return;
    }
    // fourth, process the request
    final XmldbURI docUri = path.lastSegment();
    final XmldbURI collUri = path.removeLastSegment();
    if (docUri == null || collUri == null) {
        throw new BadRequestException("Bad path: " + path);
    }
    // TODO : use getOrCreateCollection() right now ?
    try (final ManagedCollectionLock managedCollectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collUri)) {
        final Collection collection = broker.getOrCreateCollection(transaction, collUri);
        final MimeType mime;
        String contentType = request.getContentType();
        if (contentType != null) {
            final int semicolon = contentType.indexOf(';');
            if (semicolon > 0) {
                contentType = contentType.substring(0, semicolon).trim();
            }
            mime = MimeTable.getInstance().getContentType(contentType);
        } else {
            mime = MimeTable.getInstance().getContentTypeFor(docUri);
        }
        // TODO(AR) in storeDocument, if the input source has an InputStream (but is not a subclass: FileInputSource or ByteArrayInputSource), need to handle caching and reusing the input stream between validate and store
        try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), request.getInputStream());
            final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
            broker.storeDocument(transaction, docUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
        }
        response.setStatus(HttpServletResponse.SC_CREATED);
    // try(final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), request.getInputStream());
    // final InputStream cfis = new CachingFilterInputStream(cache)) {
    // 
    // if (mime.isXMLType()) {
    // cfis.mark(Integer.MAX_VALUE);
    // final IndexInfo info = collection.validateXMLResource(transaction, broker, docUri, new InputSource(cfis));
    // info.getDocument().setMimeType(contentType);
    // cfis.reset();
    // collection.store(transaction, broker, info, new InputSource(cfis));
    // response.setStatus(HttpServletResponse.SC_CREATED);
    // } else {
    // collection.addBinaryResource(transaction, broker, docUri, cfis, contentType, request.getContentLength());
    // response.setStatus(HttpServletResponse.SC_CREATED);
    // }
    // }
    } catch (final SAXParseException e) {
        throw new BadRequestException("Parsing exception at " + e.getLineNumber() + "/" + e.getColumnNumber() + ": " + e.toString());
    } catch (final TriggerException | LockException e) {
        throw new PermissionDeniedException(e.getMessage());
    } catch (final SAXException e) {
        Exception o = e.getException();
        if (o == null) {
            o = e;
        }
        throw new BadRequestException("Parsing exception: " + o.getMessage());
    } catch (final EXistException e) {
        throw new BadRequestException("Internal error: " + e.getMessage());
    }
}
Also used : EXistException(org.exist.EXistException) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) PermissionDeniedException(org.exist.security.PermissionDeniedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TriggerException(org.exist.collections.triggers.TriggerException) EXistException(org.exist.EXistException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Aggregations

ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)11 Collection (org.exist.collections.Collection)9 StringInputSource (org.exist.util.StringInputSource)5 XmldbURI (org.exist.xmldb.XmldbURI)5 BrokerPool (org.exist.storage.BrokerPool)4 Txn (org.exist.storage.txn.Txn)4 DBBroker (org.exist.storage.DBBroker)3 URISyntaxException (java.net.URISyntaxException)2 EXistException (org.exist.EXistException)2 TriggerException (org.exist.collections.triggers.TriggerException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 SAXException (org.xml.sax.SAXException)2 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1