Search in sources :

Example 6 with Descriptor

use of org.exist.http.Descriptor in project exist by eXist-db.

the class RpcServlet method doPost.

@Override
public void doPost(HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
    try {
        // Request logger
        final Descriptor descriptor = Descriptor.getDescriptorSingleton();
        if (descriptor.allowRequestLogging() && !descriptor.requestsFiltered()) {
            // Wrap HttpServletRequest, because both request Logger and xmlrpc
            // need the request InputStream, which is consumed when read.
            final String cacheClass = (String) BrokerPool.getInstance().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
            request = new HttpServletRequestWrapper(() -> cacheClass, request, /*formEncoding*/
            charset != null ? charset.displayName() : ISO_8859_1.displayName());
            descriptor.doLogRequestInReplayLog(request);
        }
        try {
            if (charset != null) {
                response.setCharacterEncoding(charset.displayName());
            }
            super.doPost(request, response);
        } catch (final Throwable e) {
            LOG.error("Problem during XmlRpc execution", e);
            final String exceptionMessage;
            if (e instanceof XmlRpcException) {
                final Throwable linkedException = ((XmlRpcException) e).linkedException;
                LOG.error(linkedException.getMessage(), linkedException);
                exceptionMessage = "An error occurred: " + e.getMessage() + ": " + linkedException.getMessage();
            } else {
                exceptionMessage = "An unknown error occurred: " + e.getMessage();
            }
            throw new ServletException(exceptionMessage, e);
        }
    } catch (final EXistException e) {
        throw new ServletException(e);
    } finally {
        if (request != null && request instanceof HttpServletRequestWrapper) {
            ((HttpServletRequestWrapper) request).close();
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpServletRequestWrapper(org.exist.http.servlets.HttpServletRequestWrapper) Descriptor(org.exist.http.Descriptor) EXistException(org.exist.EXistException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 7 with Descriptor

use of org.exist.http.Descriptor in project exist by eXist-db.

the class EXistServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse response) throws ServletException, IOException {
    HttpServletRequest request = null;
    try {
        // For POST request, If we are logging the requests we must wrap
        // HttpServletRequest in HttpServletRequestWrapper
        // otherwise we cannot access the POST parameters from the content body
        // of the request!!! - deliriumsky
        final Descriptor descriptor = Descriptor.getDescriptorSingleton();
        if (descriptor != null) {
            if (descriptor.allowRequestLogging()) {
                request = new HttpServletRequestWrapper(() -> (String) getPool().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), req, getFormEncoding());
            } else {
                request = req;
            }
        } else {
            request = req;
        }
        // first, adjust the path
        String path = request.getPathInfo();
        if (path == null) {
            path = "";
        } else {
            path = adjustPath(request);
        }
        // second, perform descriptor actions
        if (descriptor != null && !descriptor.requestsFiltered()) {
            // logs the request if specified in the descriptor
            descriptor.doLogRequestInReplayLog(request);
            // 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()) {
            try {
                srvREST.doPost(broker, transaction, request, response, path);
                transaction.commit();
            } catch (final Throwable t) {
                transaction.abort();
                throw t;
            }
        } 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 BadRequestException e) {
            if (response.isCommitted()) {
                throw new ServletException(e.getMessage(), e);
            }
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        } catch (final NotFoundException e) {
            if (response.isCommitted()) {
                throw new ServletException(e.getMessage(), e);
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
        } catch (final Throwable e) {
            getLog().error(e);
            throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
        }
    } finally {
        if (request instanceof HttpServletRequestWrapper) {
            ((HttpServletRequestWrapper) request).close();
        }
    }
}
Also used : NotFoundException(org.exist.http.NotFoundException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) Descriptor(org.exist.http.Descriptor) BadRequestException(org.exist.http.BadRequestException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 8 with Descriptor

use of org.exist.http.Descriptor in project exist by eXist-db.

the class EXistServlet method doGet.

@Override
protected void doGet(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 && !descriptor.requestsFiltered()) {
        // logs the request if specified in the descriptor
        descriptor.doLogRequestInReplayLog(request);
        // 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()) {
        try {
            srvREST.doGet(broker, transaction, request, response, path);
            transaction.commit();
        } catch (final Throwable t) {
            transaction.abort();
            throw t;
        }
    } catch (final BadRequestException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage());
        }
        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 NotFoundException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage());
        }
        response.sendError(HttpServletResponse.SC_NOT_FOUND, 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 EOFException ee) {
        getLog().error("GET Connection has been interrupted", ee);
        throw new ServletException("GET Connection has been interrupted", ee);
    } catch (final Throwable e) {
        getLog().error(e.getMessage(), e);
        throw new ServletException("An error occurred: " + e.getMessage(), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) EOFException(java.io.EOFException) Descriptor(org.exist.http.Descriptor) BadRequestException(org.exist.http.BadRequestException) NotFoundException(org.exist.http.NotFoundException) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject)

Example 9 with Descriptor

use of org.exist.http.Descriptor in project exist by eXist-db.

the class EXistServlet method doDelete.

@Override
protected void doDelete(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) {
        // 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()) {
        try {
            srvREST.doDelete(broker, transaction, path, request, response);
            transaction.commit();
        } catch (final Throwable t) {
            transaction.abort();
            throw t;
        }
    } 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 NotFoundException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, 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) {
        getLog().error(e);
        throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) Descriptor(org.exist.http.Descriptor) NotFoundException(org.exist.http.NotFoundException) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject)

Example 10 with Descriptor

use of org.exist.http.Descriptor in project exist by eXist-db.

the class EXistServlet method doPut.

@Override
protected void doPut(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 PUT 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_BAD_REQUEST, "A PUT request is not allowed against a plain collection path.");
                return;
            }
        }
        try {
            srvREST.doPut(broker, transaction, dbpath, request, response);
            transaction.commit();
        } catch (final Throwable t) {
            transaction.abort();
            throw t;
        }
    } 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 : 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) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

Descriptor (org.exist.http.Descriptor)11 ServletException (javax.servlet.ServletException)8 EXistException (org.exist.EXistException)8 Subject (org.exist.security.Subject)8 DBBroker (org.exist.storage.DBBroker)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)7 Txn (org.exist.storage.txn.Txn)6 BadRequestException (org.exist.http.BadRequestException)5 NotFoundException (org.exist.http.NotFoundException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Collection (org.exist.collections.Collection)2 XmldbURI (org.exist.xmldb.XmldbURI)2 Item (org.exist.xquery.value.Item)2 Sequence (org.exist.xquery.value.Sequence)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1