Search in sources :

Example 11 with Descriptor

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

the class EXistServlet method doHead.

@Override
protected void doHead(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.doHead(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(), 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 NotFoundException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage(), 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) 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)

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