Search in sources :

Example 6 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker in project sling by apache.

the class SlingServletResolver method resolveServlet.

// ---------- ServletResolver interface -----------------------------------
/**
     * @see org.apache.sling.api.servlets.ServletResolver#resolveServlet(org.apache.sling.api.SlingHttpServletRequest)
     */
@Override
public Servlet resolveServlet(final SlingHttpServletRequest request) {
    final Resource resource = request.getResource();
    // start tracking servlet resolution
    final RequestProgressTracker tracker = request.getRequestProgressTracker();
    final String timerName = "resolveServlet(" + resource.getPath() + ")";
    tracker.startTimer(timerName);
    final String type = resource.getResourceType();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("resolveServlet called for resource {}", resource);
    }
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    Servlet servlet = null;
    if (type != null && type.length() > 0) {
        servlet = resolveServletInternal(request, null, type, scriptResolver);
    }
    // last resort, use the core bundle default servlet
    if (servlet == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("No specific servlet found, trying default");
        }
        servlet = getDefaultServlet();
    }
    // track servlet resolution termination
    if (servlet == null) {
        tracker.logTimer(timerName, "Servlet resolution failed. See log for details");
    } else {
        tracker.logTimer(timerName, "Using servlet {0}", RequestUtil.getServletName(servlet));
    }
    // log the servlet found
    if (LOGGER.isDebugEnabled()) {
        if (servlet != null) {
            LOGGER.debug("Servlet {} found for resource={}", RequestUtil.getServletName(servlet), resource);
        } else {
            LOGGER.debug("No servlet found for resource={}", resource);
        }
    }
    return servlet;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet) HttpServlet(javax.servlet.http.HttpServlet) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 7 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker in project sling by apache.

the class SlingServletResolver method handleError.

/**
     * @see org.apache.sling.engine.servlets.ErrorHandler#handleError(java.lang.Throwable, org.apache.sling.api.SlingHttpServletRequest, org.apache.sling.api.SlingHttpServletResponse)
     */
@Override
public void handleError(final Throwable throwable, final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    // do not handle, if already handling ....
    if (request.getAttribute(SlingConstants.ERROR_REQUEST_URI) != null) {
        LOGGER.error("handleError: Recursive invocation. Not further handling Throwable:", throwable);
        return;
    }
    // start tracker
    RequestProgressTracker tracker = request.getRequestProgressTracker();
    String timerName = "handleError:throwable=" + throwable.getClass().getName();
    tracker.startTimer(timerName);
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    try {
        // find the error handler component
        Servlet servlet = null;
        Resource resource = getErrorResource(request);
        Class<?> tClass = throwable.getClass();
        while (servlet == null && tClass != Object.class) {
            // find a servlet for the simple class name as the method name
            ResourceCollector locationUtil = new ResourceCollector(tClass.getSimpleName(), DEFAULT_ERROR_HANDLER_RESOURCE_TYPE, resource, this.executionPaths);
            servlet = getServletInternal(locationUtil, request, scriptResolver);
            // go to the base class
            tClass = tClass.getSuperclass();
        }
        if (servlet == null) {
            servlet = getDefaultErrorServlet(request, resource, scriptResolver);
        }
        // set the message properties
        request.setAttribute(SlingConstants.ERROR_EXCEPTION, throwable);
        request.setAttribute(SlingConstants.ERROR_EXCEPTION_TYPE, throwable.getClass());
        request.setAttribute(SlingConstants.ERROR_MESSAGE, throwable.getMessage());
        // log a track entry after resolution before calling the handler
        tracker.logTimer(timerName, "Using handler {0}", RequestUtil.getServletName(servlet));
        handleError(servlet, request, response);
    } finally {
        tracker.logTimer(timerName, "Error handler finished");
    }
}
Also used : ResourceCollector(org.apache.sling.servlets.resolver.internal.helper.ResourceCollector) AbstractResourceCollector(org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector) NamedScriptResourceCollector(org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet) HttpServlet(javax.servlet.http.HttpServlet) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 8 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker in project sling by apache.

the class DefaultErrorHandlerServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws IOException {
    // get settings
    Integer scObject = (Integer) req.getAttribute(SlingConstants.ERROR_STATUS);
    String statusMessage = (String) req.getAttribute(SlingConstants.ERROR_MESSAGE);
    String requestUri = (String) req.getAttribute(SlingConstants.ERROR_REQUEST_URI);
    String servletName = (String) req.getAttribute(SlingConstants.ERROR_SERVLET_NAME);
    // ensure values
    int statusCode = (scObject != null) ? scObject.intValue() : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    if (statusMessage == null) {
        statusMessage = statusToString(statusCode);
    }
    // start the response message
    final PrintWriter pw = sendIntro((HttpServletResponse) res, statusCode, statusMessage, requestUri, servletName);
    // write the exception message
    final PrintWriter escapingWriter = new PrintWriter(ResponseUtil.getXmlEscapingWriter(pw));
    // dump the stack trace
    if (req.getAttribute(SlingConstants.ERROR_EXCEPTION) instanceof Throwable) {
        final Throwable throwable = (Throwable) req.getAttribute(SlingConstants.ERROR_EXCEPTION);
        pw.println("<h3>Exception:</h3>");
        pw.println("<pre>");
        pw.flush();
        printStackTrace(escapingWriter, throwable);
        escapingWriter.flush();
        pw.println("</pre>");
    }
    // dump the request progress tracker
    if (req instanceof SlingHttpServletRequest) {
        final RequestProgressTracker tracker = ((SlingHttpServletRequest) req).getRequestProgressTracker();
        pw.println("<h3>Request Progress:</h3>");
        pw.println("<pre>");
        pw.flush();
        tracker.dump(escapingWriter);
        escapingWriter.flush();
        pw.println("</pre>");
    }
    // conclude the response message
    sendEpilogue(pw);
}
Also used : RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 9 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker in project sling by apache.

the class DefaultErrorHandler method sendError.

private void sendError(final int status, final String message, final Throwable throwable, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    if (response.isCommitted()) {
        log.error("handleError: Response already committed; cannot send error " + status + message, throwable);
    } else {
        // error situation
        final String servletName = (String) request.getAttribute(ERROR_SERVLET_NAME);
        String requestURI = (String) request.getAttribute(ERROR_REQUEST_URI);
        if (requestURI == null) {
            requestURI = request.getRequestURI();
        }
        // reset anything in the response first
        response.reset();
        // set the status, content type and encoding
        response.setStatus(status);
        response.setContentType("text/html; charset=UTF-8");
        final PrintWriter pw = response.getWriter();
        pw.println("<html><head><title>");
        pw.println(ResponseUtil.escapeXml(message));
        pw.println("</title></head><body><h1>");
        if (throwable != null) {
            pw.println(ResponseUtil.escapeXml(throwable.toString()));
        } else if (message != null) {
            pw.println(ResponseUtil.escapeXml(message));
        } else {
            pw.println("Internal error (no Exception to report)");
        }
        pw.println("</h1><p>");
        pw.print("RequestURI=");
        pw.println(ResponseUtil.escapeXml(request.getRequestURI()));
        if (servletName != null) {
            pw.println("</p><p>Servlet=");
            pw.println(ResponseUtil.escapeXml(servletName));
        }
        pw.println("</p>");
        if (throwable != null) {
            final PrintWriter escapingWriter = new PrintWriter(ResponseUtil.getXmlEscapingWriter(pw));
            pw.println("<h3>Exception stacktrace:</h3>");
            pw.println("<pre>");
            pw.flush();
            throwable.printStackTrace(escapingWriter);
            escapingWriter.flush();
            pw.println("</pre>");
            final RequestProgressTracker tracker = ((SlingHttpServletRequest) request).getRequestProgressTracker();
            pw.println("<h3>Request Progress:</h3>");
            pw.println("<pre>");
            pw.flush();
            tracker.dump(new PrintWriter(escapingWriter));
            escapingWriter.flush();
            pw.println("</pre>");
        }
        pw.println("<hr /><address>");
        pw.println(ResponseUtil.escapeXml(serverInfo));
        pw.println("</address></body></html>");
        // commit the response
        response.flushBuffer();
        // close the response (SLING-2724)
        pw.close();
    }
}
Also used : RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 10 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker in project sling by apache.

the class CallTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final RequestProgressTracker tracker = TagUtil.getRequest(pageContext).getRequestProgressTracker();
    String servletName = null;
    final Servlet servlet;
    if (!ignoreComponentHierarchy) {
        final Resource resource = bindings.getResource();
        servlet = servletResolver.resolveServlet(resource, this.script);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} for path={1}, type={2}: {3}", script, resource.getPath(), resource.getResourceType(), servletName);
        }
    } else {
        final ResourceResolver resolver = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : resolver.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + "/" + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(resolver, scriptPath);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} (ignoring component hierarchy): {1}", script, servletName);
        }
    }
    if (servlet == null) {
        throw new JspException("Could not find script " + script);
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        tracker.startTimer(servletName);
        servlet.service(pageContext.getRequest(), response);
        tracker.logTimer(servletName);
        return EVAL_PAGE;
    } catch (Exception e) {
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Aggregations

RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)11 Servlet (javax.servlet.Servlet)4 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)4 Resource (org.apache.sling.api.resource.Resource)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 HttpServlet (javax.servlet.http.HttpServlet)3 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)3 OptingServlet (org.apache.sling.api.servlets.OptingServlet)3 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)3 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)3 PrintWriter (java.io.PrintWriter)2 RequestData (org.apache.sling.engine.impl.request.RequestData)2 AbstractResourceCollector (org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector)2 NamedScriptResourceCollector (org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector)2 ResourceCollector (org.apache.sling.servlets.resolver.internal.helper.ResourceCollector)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 JspException (javax.servlet.jsp.JspException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1