use of org.exist.EXistException in project exist by eXist-db.
the class RpcConnection method getNextExtendedChunk.
@Override
public Map<String, Object> getNextExtendedChunk(final String handle, final String offset) throws EXistException, PermissionDeniedException {
try {
final int resultId = Integer.parseInt(handle);
final SerializedResult sr = factory.resultSets.getSerializedResult(resultId);
if (sr == null) {
throw new EXistException("Invalid handle specified");
}
// This will keep the serialized result in the cache
sr.touch();
final Path tempFile = sr.result;
final long longOffset = Long.parseLong(offset);
if (longOffset < 0 || longOffset > Files.size(tempFile)) {
factory.resultSets.remove(resultId);
throw new EXistException("No more data available");
}
final byte[] chunk = getChunk(tempFile, (int) longOffset);
final long nextChunk = longOffset + chunk.length;
final Map<String, Object> result = new HashMap<>();
result.put("data", chunk);
result.put("handle", handle);
if (nextChunk >= Files.size(tempFile)) {
factory.resultSets.remove(resultId);
result.put("offset", Long.toString(0));
} else {
result.put("offset", Long.toString(nextChunk));
}
return result;
} catch (final NumberFormatException | IOException e) {
throw new EXistException(e);
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class RpcConnection method retrieveAsString.
private String retrieveAsString(final int resultId, final int num, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
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();
final Item item = qr.result.itemAt(num);
if (item == null) {
throw new EXistException("index out of range");
}
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());
}
try (final StringWriter writer = new StringWriter()) {
serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(nodeValue), writer);
return writer.toString();
}
} else {
try {
return item.getStringValue();
} catch (final XPathException e) {
throw new EXistException(e);
}
}
});
}
use of org.exist.EXistException in project exist by eXist-db.
the class RpcConnection method execute.
@Deprecated
@Override
public Map<String, Object> execute(final String pathToQuery, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final long startTime = System.currentTimeMillis();
final Optional<String> sortBy = Optional.ofNullable(parameters.get(RpcAPI.SORT_EXPR)).map(Object::toString);
return this.<Map<String, Object>>readDocument(XmldbURI.createInternal(pathToQuery)).apply((document, broker, transaction) -> {
final BinaryDocument xquery = (BinaryDocument) document;
if (xquery.getResourceType() != DocumentImpl.BINARY_FILE) {
throw new EXistException("Document " + pathToQuery + " is not a binary resource");
}
if (!xquery.getPermissions().validate(user, Permission.READ | Permission.EXECUTE)) {
throw new PermissionDeniedException("Insufficient privileges to access resource");
}
final Source source = new DBSource(broker, xquery, true);
try {
final Map<String, Object> rpcResponse = this.<Map<String, Object>>compileQuery(broker, transaction, source, parameters).apply(compiledQuery -> queryResultToRpcResponse(startTime, doQuery(broker, compiledQuery, null, parameters), sortBy));
return rpcResponse;
} catch (final XPathException e) {
throw new EXistException(e);
}
});
}
use of org.exist.EXistException in project exist by eXist-db.
the class RpcConnection method restore.
@Override
public String restore(final String newAdminPassword, final String localFile, final boolean overwriteApps) throws EXistException {
final int handle = Integer.parseInt(localFile);
final SerializedResult sr = factory.resultSets.getSerializedResult(handle);
if (sr == null) {
throw new EXistException("Invalid handle specified");
}
final BufferingRestoreListener listener = new BufferingRestoreListener();
final Future<Void> future = factory.restoreExecutorService.get().submit(() -> {
final Path backupFile = sr.result;
try {
// de-reference the temp file in the SerializeResult, so it is not re-claimed before we need it
sr.result = null;
factory.resultSets.remove(handle);
withDb((broker, transaction) -> {
final Restore restore = new Restore();
restore.restore(broker, transaction, newAdminPassword, backupFile, listener, overwriteApps);
return null;
});
return null;
} finally {
TemporaryFileManager.getInstance().returnTemporaryFile(backupFile);
}
});
final UUID uuid = UUID.randomUUID();
factory.restoreTasks.put(uuid, Tuple(listener, future));
return uuid.toString();
}
use of org.exist.EXistException 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