Search in sources :

Example 1 with HttpRequest

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

the class RESTServer method declareVariables.

/**
 * Pass the request, response and session objects to the XQuery context.
 *
 * @param context
 * @param request
 * @param response
 * @throws XPathException
 */
private HttpRequestWrapper declareVariables(final XQueryContext context, final ElementImpl variables, final HttpServletRequest request, final HttpServletResponse response) throws XPathException {
    final HttpRequestWrapper reqw = new HttpRequestWrapper(request, formEncoding, containerEncoding);
    final ResponseWrapper respw = new HttpResponseWrapper(response);
    context.setHttpContext(new XQueryContext.HttpContext(reqw, respw));
    // enable EXQuery Request Module (if present)
    try {
        if (xqueryContextExqueryRequestAttribute != null && cstrHttpServletRequestAdapter != null) {
            final HttpRequest exqueryRequestAdapter = cstrHttpServletRequestAdapter.apply(request, () -> (String) context.getBroker().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY));
            if (exqueryRequestAdapter != null) {
                context.setAttribute(xqueryContextExqueryRequestAttribute, exqueryRequestAdapter);
            }
        }
    } catch (final Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("EXQuery Request Module is not present: {}", e.getMessage(), e);
        }
    }
    if (variables != null) {
        declareExternalAndXQJVariables(context, variables);
    }
    return reqw;
}
Also used : HttpRequest(org.exquery.http.HttpRequest) HttpResponseWrapper(org.exist.http.servlets.HttpResponseWrapper) HttpRequestWrapper(org.exist.http.servlets.HttpRequestWrapper) ResponseWrapper(org.exist.http.servlets.ResponseWrapper) HttpResponseWrapper(org.exist.http.servlets.HttpResponseWrapper) PermissionDeniedException(org.exist.security.PermissionDeniedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TriggerException(org.exist.collections.triggers.TriggerException) EXistException(org.exist.EXistException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with HttpRequest

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

the class RestXqServlet method service.

@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    // authenticate
    final Subject user = authenticate(request, response);
    if (user == null) {
        // "Permission denied: unknown user or password");
        return;
    }
    try (final DBBroker broker = getPool().get(Optional.of(user))) {
        final Configuration configuration = broker.getConfiguration();
        final HttpRequest requestAdapter = new HttpServletRequestAdapter(request, () -> (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY));
        final RestXqService service = getRegistry().findService(requestAdapter);
        if (service != null) {
            if (log.isTraceEnabled()) {
                log.trace("Received {} request for \"{}\" and found Resource Function \"{}\" in  module \"{}\"", requestAdapter.getMethod().name(), requestAdapter.getPath(), service.getResourceFunction().getFunctionSignature(), service.getResourceFunction().getXQueryLocation());
            }
            service.service(requestAdapter, new HttpServletResponseAdapter(response), new ResourceFunctionExecutorImpl(getPool(), request.getContextPath() + request.getServletPath(), request.getRequestURI()), new RestXqServiceSerializerImpl(getPool()));
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Received {} request for \"{}\" but no suitable Resource Function found!", requestAdapter.getMethod().name(), requestAdapter.getPath());
            }
            super.service(request, response);
        }
    } catch (final EXistException e) {
        getLog().error(e.getMessage(), e);
        throw new ServletException(e.getMessage(), e);
    } catch (final RestXqServiceException e) {
        if (e.getCause() instanceof PermissionDeniedException) {
            getAuthenticator().sendChallenge(request, response);
        } else {
            // TODO should probably be caught higher up and returned as a HTTP Response? maybe need two different types of exception to differentiate critical vs processing exception
            getLog().error(e.getMessage(), e);
            throw new ServletException(e.getMessage(), e);
        }
    }
}
Also used : HttpRequest(org.exquery.http.HttpRequest) HttpServletResponseAdapter(org.exist.extensions.exquery.restxq.impl.adapters.HttpServletResponseAdapter) RestXqServiceException(org.exquery.restxq.RestXqServiceException) Configuration(org.exist.util.Configuration) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) ServletException(javax.servlet.ServletException) RestXqService(org.exquery.restxq.RestXqService) DBBroker(org.exist.storage.DBBroker) HttpServletRequestAdapter(org.exist.extensions.exquery.restxq.impl.adapters.HttpServletRequestAdapter) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Aggregations

EXistException (org.exist.EXistException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 HttpRequest (org.exquery.http.HttpRequest)2 ServletException (javax.servlet.ServletException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TriggerException (org.exist.collections.triggers.TriggerException)1 HttpServletRequestAdapter (org.exist.extensions.exquery.restxq.impl.adapters.HttpServletRequestAdapter)1 HttpServletResponseAdapter (org.exist.extensions.exquery.restxq.impl.adapters.HttpServletResponseAdapter)1 HttpRequestWrapper (org.exist.http.servlets.HttpRequestWrapper)1 HttpResponseWrapper (org.exist.http.servlets.HttpResponseWrapper)1 ResponseWrapper (org.exist.http.servlets.ResponseWrapper)1 Subject (org.exist.security.Subject)1 DBBroker (org.exist.storage.DBBroker)1 Configuration (org.exist.util.Configuration)1 RestXqService (org.exquery.restxq.RestXqService)1 RestXqServiceException (org.exquery.restxq.RestXqServiceException)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1