Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.sling.engine.impl;
 
 import static org.apache.sling.api.SlingConstants.ERROR_SERVLET_NAME;
 
 import java.io.Writer;
 
 
 
 public class SlingRequestProcessorImpl implements SlingRequestProcessor {

    
default log
 
     private final Logger log = LoggerFactory.getLogger(SlingRequestProcessorImpl.class);
 
     // used fields ....
 
     private final DefaultErrorHandler defaultErrorHandler = new DefaultErrorHandler();
 
 
     private RequestLogger requestLogger;
 
     private ServletResolver servletResolver;
 
 
     // ---------- helper setters
 
     void setServerInfo(final String serverInfo) {
         .setServerInfo(serverInfo);
     }
 
     void setErrorHandler(final ErrorHandler errorHandler) {
         this. = errorHandler;
     }
 
     void unsetErrorHandler(final ErrorHandler errorHandler) {
         if (this. == errorHandler) {
             this. = ;
         }
     }
 
     void setRequestLogger(final RequestLogger requestLogger) {
         this. = requestLogger;
     }
    void unsetRequestLogger(final RequestLogger requestLogger) {
        if (this. == requestLogger) {
            this. = null;
        }
    }
    void setServletResolver(final ServletResolver servletResolver) {
        this. = servletResolver;
    }
    void unsetServletResolver(final ServletResolver servletResolver) {
        if (this. == servletResolver) {
            this. = null;
        }
    }
    void setFilterManager(final ServletFilterManager filterManager) {
        this. = filterManager;
    }
    // ---------- SlingRequestProcessor interface
    public void processRequest(final HttpServletRequest servletRequest,
            final HttpServletResponse servletResponse,
            final ResourceResolver resourceResolverthrows IOException {
        // setting the Sling request and response
        final RequestData requestData = new RequestData(thisservletRequest,
            servletResponse);
        final SlingHttpServletRequest request = requestData.getSlingRequest();
        final SlingHttpServletResponse response = requestData.getSlingResponse();
        // record the request for the web console display
        RequestHistoryConsolePlugin.recordRequest(request);
        // request entry log
        if ( != null) {
            .logRequestEntry(requestresponse);
        }
        try {
            // check that we have all required services
            if (resourceResolver == null) {
                throw new UnavailableException("ResourceResolver");
            } else if ( == null) {
                throw new UnavailableException("ServletResolver");
            }
            // initialize the request data - resolve resource and servlet
            Resource resource = requestData.initResource(resourceResolver);
            requestData.initServlet(resource);
            Filter[] filters = .getFilters(.);
            if (filters != null) {
                FilterChain processor = new RequestSlingFilterChain(this,
                    filters);
                request.getRequestProgressTracker().log(
                    "Applying " + . + "filters");
                processor.doFilter(requestresponse);
            } else {
                // no filters, directly call resource level filters and servlet
                processComponent(requestresponse.);
            }
        } catch (ResourceNotFoundException rnfe) {
            // send this exception as a 404 status
            .info("service: Resource {} not found"rnfe.getResource());
            handleError(.rnfe.getMessage(),
                requestresponse);
        } catch (SlingException se) {
            // if we have request data and a non-null active servlet name
            // we assume, that this is the name of the causing servlet
            if (requestData.getActiveServletName() != null) {
                request.setAttribute(,
                    requestData.getActiveServletName());
            }
            // send this exception as is (albeit unwrapping and wrapped
            // exception.
            Throwable t = (se.getCause() != null) ? se.getCause() : se;
            .error("service: Uncaught SlingException"t);
            handleError(trequestresponse);
        } catch (AccessControlException ace) {
            // SLING-319 if anything goes wrong, send 403/FORBIDDEN
            .info(
                "service: Authenticated user {} does not have enough rights to executed requested action",
                request.getRemoteUser());
            handleError(.nullrequest,
                response);
        } catch (UnavailableException ue) {
            // exception is thrown before the SlingHttpServletRequest/Response
            // is properly set up due to missing dependencies. In this case
            // we must not use the Sling error handling infrastructure but
            // just return a 503 status response handled by the servlet
            // container environment
            final int status = .;
            final String errorMessage = ue.getMessage()
                + " service missing, cannot service requests";
            .error("{} , sending status {}"errorMessagestatus);
            servletResponse.sendError(statuserrorMessage);
        } catch (IOException ioe) {
            // forward IOException up the call chain to properly handle it
            throw ioe;
        } catch (Throwable t) {
            // if we have request data and a non-null active servlet name
            // we assume, that this is the name of the causing servlet
            if (requestData.getActiveServletName() != null) {
                request.setAttribute(,
                    requestData.getActiveServletName());
            }
            .error("service: Uncaught Throwable"t);
            handleError(trequestresponse);
        } finally {
            // request exit log
            if ( != null) {
                .logRequestExit(requestresponse);
            }
            // dispose any request data
            requestData.dispose();
        }
    }

    
Renders the component defined by the RequestData's current ComponentData instance after calling all filters of the given filterChainType.

Parameters:
request
response
filterChainType
Throws:
java.io.IOException
javax.servlet.ServletException
    public void processComponent(SlingHttpServletRequest request,
            SlingHttpServletResponse response,
            final FilterChainType filterChainTypethrows IOException,
            ServletException {
        Filter filters[] = .getFilters(filterChainType);
        if (filters != null) {
            FilterChain processor = new SlingComponentFilterChain(filters);
            request.getRequestProgressTracker().log(
                "Applying " + filterChainType + "filters");
            processor.doFilter(requestresponse);
        } else {
            .debug("service: No Resource level filters, calling servlet");
            RequestData.service(requestresponse);
        }
    }
    // ---------- Generic Content Request processor ----------------------------

    
Dispatches the request on behalf of the org.apache.sling.engine.impl.request.SlingRequestDispatcher.
    public void dispatchRequest(ServletRequest request,
            ServletResponse responseResource resource,
            RequestPathInfo resolvedURLboolean includethrows IOException,
            ServletException {
        // we need a SlingHttpServletRequest/SlingHttpServletResponse tupel
        // to continue
        SlingHttpServletRequest cRequest = RequestData.toSlingHttpServletRequest(request);
        SlingHttpServletResponse cResponse = RequestData.toSlingHttpServletResponse(response);
        // get the request data (and btw check the correct type)
        RequestData requestData = RequestData.getRequestData(cRequest);
        ContentData contentData = requestData.pushContent(resourceresolvedURL);
        try {
            // resolve the servlet
            Servlet servlet = .resolveServlet(cRequest);
            contentData.setServlet(servlet);
            FilterChainType type = include
                    ? .
                    : .;
            processComponent(cRequestcResponsetype);
        } finally {
            requestData.popContent();
        }
    }
        return ;
    }
    // ---------- Error Handling with Filters
    void handleError(final int statusfinal String message,
            final SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows IOException {
        // wrap the response ensuring getWriter will fall back to wrapping
        // the response output stream if reset does not reset this
        response = new ErrorResponseWrapper(response);
        Filter[] filters = .getFilters(.);
        if (filters != null && filters.length > 0) {
            FilterChain processor = new AbstractSlingFilterChain(filters) {
                @Override
                protected void render(SlingHttpServletRequest request,
                        SlingHttpServletResponse responsethrows IOException {
                    .handleError(statusmessagerequestresponse);
                }
            };
            request.getRequestProgressTracker().log(
                "Applying " + . + " filters");
            try {
                processor.doFilter(requestresponse);
            } catch (ServletException se) {
                throw new SlingServletException(se);
            }
        } else {
            .handleError(statusmessagerequestresponse);
        }
    }
    // just rethrow the exception as explained in the class comment
    private void handleError(final Throwable throwable,
            final SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows IOException {
        // wrap the response ensuring getWriter will fall back to wrapping
        // the response output stream if reset does not reset this
        response = new ErrorResponseWrapper(response);
        Filter[] filters = .getFilters(.);
        if (filters != null && filters.length > 0) {
            FilterChain processor = new AbstractSlingFilterChain(filters) {
                @Override
                protected void render(SlingHttpServletRequest request,
                        SlingHttpServletResponse responsethrows IOException {
                    .handleError(throwablerequestresponse);
                }
            };
            request.getRequestProgressTracker().log(
                "Applying " + . + " filters");
            try {
                processor.doFilter(requestresponse);
            } catch (ServletException se) {
                throw new SlingServletException(se);
            }
        } else {
            .handleError(throwablerequestresponse);
        }
    }
    private static class ErrorResponseWrapper extends
            SlingHttpServletResponseWrapper {
        private PrintWriter writer;
        public ErrorResponseWrapper(SlingHttpServletResponse wrappedResponse) {
            super(wrappedResponse);
        }
        public PrintWriter getWriter() throws IOException {
            if ( == null) {
                try {
                     = super.getWriter();
                } catch (IllegalStateException ise) {
                    // resetting the response did not reset the output channel
                    // status and we have to create a writer based on the output
                    // stream using the character encoding already set on the
                    // response, defaulting to ISO-8859-1
                    OutputStream out = getOutputStream();
                    String encoding = getCharacterEncoding();
                    if (encoding == null) {
                        encoding = "ISO-8859-1";
                        setCharacterEncoding(encoding);
                    }
                    Writer w = new OutputStreamWriter(outencoding);
                     = new PrintWriter(w);
                }
            }
            return ;
        }

        
Flush the writer if the getWriter() method was called to potentially wrap an OuputStream still existing in the response.
        @Override
        public void flushBuffer() throws IOException {
            if ( != null) {
                .flush();
            }
            super.flushBuffer();
        }
    }
New to GrepCode? Check out our FAQ X