Search in sources :

Example 1 with TooManyCallsException

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

the class RequestData method service.

/**
     * Helper method to call the servlet for the current content data. If the
     * current content data has no servlet, <em>NOT_FOUND</em> (404) error is
     * sent and the method terminates.
     * <p>
     * If the the servlet exists, the
     * {@link org.apache.sling.api.SlingConstants#SLING_CURRENT_SERVLET_NAME} request attribute is set
     * to the name of that servlet and that servlet name is also set as the
     * {@link #setActiveServletName(String) currently active servlet}. After
     * the termination of the servlet (normal or throwing a Throwable) the
     * request attribute is reset to the previous value. The name of the
     * currently active servlet is only reset to the previous value if the
     * servlet terminates normally. In case of a Throwable, the active servlet
     * name is not reset and indicates which servlet caused the potential abort
     * of the request.
     *
     * @param request The request object for the service method
     * @param response The response object for the service method
     * @throws IOException May be thrown by the servlet's service method
     * @throws ServletException May be thrown by the servlet's service method
     */
public static void service(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
    RequestData requestData = RequestData.getRequestData(request);
    Servlet servlet = requestData.getContentData().getServlet();
    if (servlet == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "No Servlet to handle request");
    } else {
        String name = RequestUtil.getServletName(servlet);
        // verify the number of service calls in this request
        if (requestData.hasServletMaxCallCount(request)) {
            throw new TooManyCallsException(name);
        }
        // replace the current servlet name in the request
        Object oldValue = request.getAttribute(SLING_CURRENT_SERVLET_NAME);
        request.setAttribute(SLING_CURRENT_SERVLET_NAME, name);
        // setup the tracker for this service call
        String timerName = name + "#" + requestData.servletCallCounter;
        requestData.servletCallCounter++;
        requestData.getRequestProgressTracker().startTimer(timerName);
        try {
            String callerServlet = requestData.setActiveServletName(name);
            servlet.service(request, response);
            requestData.setActiveServletName(callerServlet);
        } finally {
            request.setAttribute(SLING_CURRENT_SERVLET_NAME, oldValue);
            requestData.getRequestProgressTracker().logTimer(timerName);
        }
    }
}
Also used : TooManyCallsException(org.apache.sling.api.request.TooManyCallsException) SlingMainServlet(org.apache.sling.engine.impl.SlingMainServlet) Servlet(javax.servlet.Servlet)

Aggregations

Servlet (javax.servlet.Servlet)1 TooManyCallsException (org.apache.sling.api.request.TooManyCallsException)1 SlingMainServlet (org.apache.sling.engine.impl.SlingMainServlet)1