Search in sources :

Example 66 with EXistException

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);
    }
}
Also used : EXistException(org.exist.EXistException)

Example 67 with EXistException

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);
            }
        }
    });
}
Also used : EXistException(org.exist.EXistException) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap)

Example 68 with EXistException

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);
        }
    });
}
Also used : DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource)

Example 69 with EXistException

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();
}
Also used : EXistException(org.exist.EXistException) Restore(org.exist.backup.Restore)

Example 70 with EXistException

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;
    });
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) NodeId(org.exist.numbering.NodeId) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17