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