Search in sources :

Example 56 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MiltonCollection method getDocumentResources.

private List<MiltonDocument> getDocumentResources() {
    List<MiltonDocument> allResources = new ArrayList<>();
    for (XmldbURI path : existCollection.getDocumentURIs()) {
        MiltonDocument mdoc = new MiltonDocument(this.host, path, brokerPool, subject);
        // Show (restimated) size for PROPFIND only
        mdoc.setIsPropFind(true);
        allResources.add(mdoc);
    }
    return allResources;
}
Also used : XmldbURI(org.exist.xmldb.XmldbURI)

Example 57 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class MiltonResource method decodePath.

/**
 * Convert % encoded string back to text.
 *
 * @param uri URI pf path
 * @return decoded path
 */
protected XmldbURI decodePath(XmldbURI uri) {
    XmldbURI retval = null;
    try {
        String path = new URI(uri.toString()).getPath();
        retval = XmldbURI.xmldbUriFor("" + path, false);
    } catch (URISyntaxException ex) {
        // oops
        LOG.error(ex.getMessage());
    }
    return retval;
}
Also used : URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI) URI(java.net.URI) XmldbURI(org.exist.xmldb.XmldbURI)

Example 58 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class ControllerForward method rewriteRequest.

@Override
protected void rewriteRequest(final XQueryURLRewrite.RequestWrapper request) {
    if (target != null && target.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        final XmldbURI dbURI = XmldbURI.create(target);
        this.uri = "/rest";
        String colPath = dbURI.getCollectionPath();
        final String contextPath = request.getInContextPath();
        if (contextPath.startsWith(colPath)) {
            colPath = "";
        }
        request.setPaths("/rest" + colPath + contextPath, "/rest");
        request.setBasePath("/rest" + colPath);
        request.setAttribute(XQueryURLRewrite.RQ_ATTR, "true");
    }
}
Also used : XmldbURI(org.exist.xmldb.XmldbURI)

Example 59 with XmldbURI

use of org.exist.xmldb.XmldbURI in project exist by eXist-db.

the class EXistServlet method doPatch.

protected void doPatch(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    // first, adjust the path
    String path = adjustPath(request);
    // second, perform descriptor actions
    final Descriptor descriptor = Descriptor.getDescriptorSingleton();
    if (descriptor != null) {
        // TODO: figure out a way to log PATCH requests with
        // HttpServletRequestWrapper and
        // Descriptor.doLogRequestInReplayLog()
        // map's the path if a mapping is specified in the descriptor
        path = descriptor.mapPath(path);
    }
    // third, authenticate the user
    final Subject user = authenticate(request, response);
    if (user == null) {
        // You now get a HTTP Authentication challenge if there is no user
        return;
    }
    // fourth, process the request
    try (final DBBroker broker = getPool().get(Optional.of(user));
        final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
        final XmldbURI dbpath = XmldbURI.createInternal(path);
        try (final Collection collection = broker.getCollection(dbpath)) {
            if (collection != null) {
                transaction.abort();
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "A PATCH request is not allowed against a plain collection path.");
                return;
            }
        }
        try {
            srvREST.doPatch(broker, transaction, dbpath, request, response);
            transaction.commit();
        } catch (final Throwable t) {
            transaction.abort();
            throw t;
        }
    } catch (final MethodNotAllowedException e) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
    } catch (final BadRequestException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage(), e);
        }
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    } catch (final PermissionDeniedException e) {
        // Else return a FORBIDDEN Error
        if (user.equals(getDefaultUser())) {
            getAuthenticator().sendChallenge(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
        }
    } catch (final EXistException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage(), e);
        }
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (final Throwable e) {
        LOG.error(e);
        throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
    }
}
Also used : MethodNotAllowedException(org.exist.http.MethodNotAllowedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) Descriptor(org.exist.http.Descriptor) Collection(org.exist.collections.Collection) BadRequestException(org.exist.http.BadRequestException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 60 with XmldbURI

use of org.exist.xmldb.XmldbURI 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

XmldbURI (org.exist.xmldb.XmldbURI)260 Collection (org.exist.collections.Collection)100 PermissionDeniedException (org.exist.security.PermissionDeniedException)69 Test (org.junit.Test)56 Txn (org.exist.storage.txn.Txn)55 EXistException (org.exist.EXistException)42 URISyntaxException (java.net.URISyntaxException)39 LockedDocument (org.exist.dom.persistent.LockedDocument)39 IOException (java.io.IOException)38 DBBroker (org.exist.storage.DBBroker)38 DocumentImpl (org.exist.dom.persistent.DocumentImpl)34 SAXException (org.xml.sax.SAXException)33 Permission (org.exist.security.Permission)30 LockException (org.exist.util.LockException)27 Path (java.nio.file.Path)22 XPathException (org.exist.xquery.XPathException)22 BrokerPool (org.exist.storage.BrokerPool)21 TransactionManager (org.exist.storage.txn.TransactionManager)20 Subject (org.exist.security.Subject)19 StringInputSource (org.exist.util.StringInputSource)17