use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class Jaxp method preparseDTD.
// No-go ...processor is in validating mode
private Path preparseDTD(StreamSource instance, String systemId) throws IOException, TransformerConfigurationException, TransformerException {
// prepare output tmp storage
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tmp = temporaryFileManager.getTemporaryFile();
final StreamResult result = new StreamResult(tmp.toFile());
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemId);
transformer.transform(instance, result);
return tmp;
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class EXistResult method add.
@Override
public void add(final InputStream is) throws HttpClientException {
try {
// we have to make a temporary copy of the data stream, as the socket will be closed shortly
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
result.add(BinaryValueFromFile.getInstance(context, new Base64BinaryValueType(), tempFile, (isClosed, file) -> temporaryFileManager.returnTemporaryFile(file)));
} catch (final XPathException | IOException xpe) {
throw new HttpClientException("Unable to add binary value to result:" + xpe.getMessage(), xpe);
} finally {
try {
is.close();
} catch (final IOException ioe) {
logger.warn(ioe.getMessage(), ioe);
}
}
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RpcConnection method retrieveFirstChunk.
@Override
public Map<String, Object> retrieveFirstChunk(final int resultId, final int num, 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: " + resultId);
}
qr.touch();
final Item item = qr.result.itemAt(num);
if (item == null) {
throw new EXistException("index out of range");
}
final Map<String, Object> result = new HashMap<>();
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
if (compression && LOG.isDebugEnabled()) {
LOG.debug("retrieveFirstChunk 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))) {
if (Type.subTypeOf(item.getType(), Type.NODE)) {
final NodeValue nodeValue = (NodeValue) item;
for (final Map.Entry<Object, Object> entry : qr.serialization.entrySet()) {
parameters.put(entry.getKey().toString(), entry.getValue().toString());
}
serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(nodeValue), writer);
} else {
writer.write(item.getStringValue());
}
} catch (final XPathException e) {
throw new EXistException(e);
}
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 getDocumentChunk.
@Override
public List<String> getDocumentChunk(final String name, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException, IOException {
final List<String> result = new ArrayList<>(2);
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path file = temporaryFileManager.getTemporaryFile();
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {
os.write(getDocument(name, parameters));
}
result.add(FileUtils.fileName(file));
result.add(Long.toString(Files.size(file)));
return result;
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RpcConnection method retrieveFirstChunk.
@Override
public Map<String, Object> retrieveFirstChunk(final String docName, final String id, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final boolean compression = useCompression(parameters);
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 NodeId nodeId = factory.getBrokerPool().getNodeFactory().createFromString(id);
final NodeProxy node = new NodeProxy(document, nodeId);
final Map<String, Object> result = new HashMap<>();
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
if (compression && LOG.isDebugEnabled()) {
LOG.debug("retrieveFirstChunk 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))) {
serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(node), writer);
}
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;
});
}
Aggregations