use of org.exist.util.io.FilterInputStreamCache 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());
}
}
use of org.exist.util.io.FilterInputStreamCache 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;
}
}
Aggregations