use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class EmbeddedInputStream method openStream.
private static Either<IOException, InputStream> openStream(final BrokerPool pool, final XmldbURL url) {
if (LOG.isDebugEnabled()) {
LOG.debug("Begin document download");
}
try {
final XmldbURI path = XmldbURI.create(url.getPath());
try (final DBBroker broker = pool.getBroker()) {
try (final LockedDocument lockedResource = broker.getXMLResource(path, Lock.LockMode.READ_LOCK)) {
if (lockedResource == null) {
// Test for collection
try (final Collection collection = broker.openCollection(path, Lock.LockMode.READ_LOCK)) {
if (collection == null) {
// No collection, no document
return Left(new IOException("Resource " + url.getPath() + " not found."));
} else {
// Collection
return Left(new IOException("Resource " + url.getPath() + " is a collection."));
}
}
} else {
final DocumentImpl resource = lockedResource.getDocument();
if (resource.getResourceType() == DocumentImpl.XML_FILE) {
final Serializer serializer = broker.borrowSerializer();
try {
// Preserve doctype
serializer.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "yes");
// serialize the XML to a temporary file
final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
final Path tempFile = tempFileManager.getTemporaryFile();
try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
serializer.serialize(resource, writer);
}
// NOTE: the temp file will be returned to the manager when the InputStream is closed
return Right(new CloseNotifyingInputStream(Files.newInputStream(tempFile, StandardOpenOption.READ), () -> tempFileManager.returnTemporaryFile(tempFile)));
} finally {
broker.returnSerializer(serializer);
}
} else if (resource.getResourceType() == BinaryDocument.BINARY_FILE) {
return Right(broker.getBinaryResource((BinaryDocument) resource));
} else {
return Left(new IOException("Unknown resource type " + url.getPath() + ": " + resource.getResourceType()));
}
}
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("End document download");
}
}
}
} catch (final EXistException | PermissionDeniedException | SAXException e) {
LOG.error(e);
return Left(new IOException(e.getMessage(), e));
} catch (final IOException e) {
return Left(e);
}
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RpcConnection method getDocumentData.
@Override
public Map<String, Object> getDocumentData(final String docName, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final XmldbURI docUri;
try {
docUri = XmldbURI.xmldbUriFor(docName);
} catch (final URISyntaxException e) {
throw new EXistException(e);
}
return this.<Map<String, Object>>readDocument(docUri).apply((document, broker, transaction) -> {
final Charset encoding = getEncoding(parameters);
// A tweak for very large resources, VirtualTempFile
final Map<String, Object> result = new HashMap<>();
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
if (document.getResourceType() == DocumentImpl.XML_FILE) {
try (final Writer writer = Files.newBufferedWriter(tempFile, encoding)) {
serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(document), writer);
}
} else {
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(tempFile))) {
broker.readBinaryResource(transaction, (BinaryDocument) document, os);
}
}
final byte[] firstChunk = getChunk(tempFile, 0);
result.put("data", firstChunk);
int offset = 0;
if (Files.size(tempFile) > MAX_DOWNLOAD_CHUNK_SIZE) {
offset = firstChunk.length;
final int handle = factory.resultSets.add(new SerializedResult(tempFile));
result.put("handle", Integer.toString(handle));
result.put("supports-long-offset", Boolean.TRUE);
} else {
temporaryFileManager.returnTemporaryFile(tempFile);
}
result.put("offset", offset);
return result;
});
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RpcConnection method retrieveAllFirstChunk.
@Override
public Map<String, Object> retrieveAllFirstChunk(final int resultId, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final boolean compression = useCompression(parameters);
return withDb((broker, transaction) -> {
final QueryResult qr = factory.resultSets.getResult(resultId);
if (qr == null) {
throw new EXistException("result set unknown or timed out");
}
qr.touch();
for (final Map.Entry<Object, Object> entry : qr.serialization.entrySet()) {
parameters.put(entry.getKey().toString(), entry.getValue().toString());
}
final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
try {
final Map<String, Object> result = new HashMap<>();
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
if (compression && LOG.isDebugEnabled()) {
LOG.debug("retrieveAllFirstChunk with compression");
}
try (final OutputStream os = compression ? new DeflaterOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile))) : new BufferedOutputStream(Files.newOutputStream(tempFile));
final Writer writer = new OutputStreamWriter(os, getEncoding(parameters))) {
handler.setOutput(writer, toProperties(parameters));
// serialize results
handler.startDocument();
handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
final AttributesImpl attribs = new AttributesImpl();
attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(qr.result.getItemCount()));
handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
Item current;
char[] value;
try {
for (final SequenceIterator i = qr.result.iterate(); i.hasNext(); ) {
current = i.nextItem();
if (Type.subTypeOf(current.getType(), Type.NODE)) {
((NodeValue) current).toSAX(broker, handler, null);
} else {
value = current.toString().toCharArray();
handler.characters(value, 0, value.length);
}
}
} catch (final XPathException e) {
throw new EXistException(e);
}
handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
handler.endPrefixMapping("exist");
handler.endDocument();
}
final byte[] firstChunk = getChunk(tempFile, 0);
result.put("data", firstChunk);
int offset = 0;
if (Files.size(tempFile) > MAX_DOWNLOAD_CHUNK_SIZE) {
offset = firstChunk.length;
final int handle = factory.resultSets.add(new SerializedResult(tempFile));
result.put("handle", Integer.toString(handle));
result.put("supports-long-offset", Boolean.TRUE);
} else {
temporaryFileManager.returnTemporaryFile(tempFile);
}
result.put("offset", offset);
return result;
} finally {
SerializerPool.getInstance().returnObject(handler);
}
});
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class ZipArchiveBackupDescriptor method getRepoBackup.
@Override
public Path getRepoBackup() throws IOException {
final ZipEntry ze = archive.getEntry(RepoBackup.REPO_ARCHIVE);
if (ze == null) {
return null;
}
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path temp = temporaryFileManager.getTemporaryFile();
try (final InputStream is = archive.getInputStream(ze)) {
Files.copy(is, temp, StandardCopyOption.REPLACE_EXISTING);
}
return temp;
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class EmbeddedOutputStream method openStream.
private static Either<IOException, OutputStream> openStream(final BrokerPool pool, final XmldbURL url) {
if (LOG.isDebugEnabled()) {
LOG.debug("Begin document download");
}
try {
// get a temporary file
final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
final Path tempFile = tempFileManager.getTemporaryFile();
final OutputStream osTemp = new BufferedOutputStream(Files.newOutputStream(tempFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE));
// upload the content of the temp file to the db when it is closed, then return the temp file
final RunnableE<IOException> uploadOnClose = () -> {
uploadToDb(pool, url, tempFile);
tempFileManager.returnTemporaryFile(tempFile);
};
return Right(new CloseNotifyingOutputStream(osTemp, uploadOnClose));
} catch (final IOException e) {
return Left(e);
}
}
Aggregations