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());
}
}
Aggregations