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.request;
 
 
 
 
 public class SlingRequestDispatcher implements RequestDispatcher {

    
default log
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     private Resource resource;
 
     private RequestDispatcherOptions options;
 
     private String path;
 
     public SlingRequestDispatcher(String pathRequestDispatcherOptions options) {
         this. = path;
         this. = options;
         this. = null;
     }
 
     public SlingRequestDispatcher(Resource resource,
             RequestDispatcherOptions options) {
 
         this. = resource;
         this. = options;
         this. = resource.getPath();
     }
 
     public void include(ServletRequest requestServletResponse sResponse)
             throws ServletExceptionIOException {
 
         // guard access to the request and content data: If the request is
         // not (wrapping) a SlingHttpServletRequest, accessing the request Data
         // throws an IllegalArgumentException and we cannot continue
         final ContentData cd;
         try {
             cd = RequestData.getRequestData(request).getContentData();
         } catch (IllegalArgumentException iae) {
             throw new ServletException(iae.getMessage());
         }
 
         // ClassCastException is not expected here because we operate in
         // HTTP requests only (it cannot be excluded though if some client
         // code uses a ServletRequestWrapper rather than an
         // HttpServletRequestWrapper ...)
         final HttpServletRequest hRequest = (HttpServletRequestrequest;
 
         // set the inclusion request attributes from the current request
         final Object v1 = setAttribute(request,
             .cd.getResource());
         final Object v2 = setAttribute(request,
             .cd.getServlet());
         final Object v3 = setAttribute(request,
         final Object v4 = setAttribute(request,
             .hRequest.getContextPath());
         final Object v5 = setAttribute(request,
             .hRequest.getPathInfo());
         final Object v6 = setAttribute(request,
             .hRequest.getQueryString());
         final Object v7 = setAttribute(request,
             .hRequest.getRequestURI());
         final Object v8 = setAttribute(request,
             .hRequest.getServletPath());
 
        try {
            dispatch(requestsResponsetrue);
        } finally {
            // reset inclusion request attributes to previous values
            request.setAttribute(.v1);
            request.setAttribute(.v2);
            request.setAttribute(.v3);
            request.setAttribute(.v4);
            request.setAttribute(.v5);
            request.setAttribute(.v6);
            request.setAttribute(.v7);
            request.setAttribute(.v8);
        }
    }
    public void forward(ServletRequest requestServletResponse response)
            throws ServletExceptionIOException {
        // fail forwarding if the response has already been committed
        if (response.isCommitted()) {
            throw new IllegalStateException("Response already committed");
        }
        // reset the response, will throw an IllegalStateException
        // if already committed, which will not be the case because
        // we already tested for this condition
        response.reset();
        // ensure inclusion information attributes are not set
        // now just include as normal
        dispatch(requestresponsefalse);
        // finally, we would have to ensure the response is committed
        // and closed. Let's just flush the buffer and thus commit the
        // response for now
        response.flushBuffer();
    }
    private String getAbsolutePath(SlingHttpServletRequest requestString path) {
        // path is already absolute
        if (path.startsWith("/")) {
            return path;
        }
        // get parent of current request
        String uri = request.getResource().getPath();
        int lastSlash = uri.lastIndexOf('/');
        if (lastSlash >= 0) {
            uri = uri.substring(0, lastSlash);
        }
        // append relative path to parent
        return uri + '/' + path;
    }
    private void dispatch(ServletRequest requestServletResponse sResponse,
            boolean includethrows ServletExceptionIOException {

        
TODO: I have made some quick fixes in this method for SLING-221 and SLING-222, but haven't had time to do a proper review. This method might deserve a more extensive rewrite.
        SlingHttpServletRequest cRequest = RequestData.unwrap(request);
        RequestData rd = RequestData.getRequestData(cRequest);
        String absPath = getAbsolutePath(cRequest);
        // if the response is not an HttpServletResponse, fail gracefully not
        // doing anything
        if (!(sResponse instanceof HttpServletResponse)) {
            .error("include: Failed to include {}, response has wrong type",
                absPath);
            return;
        }
        if ( == null) {
            // resolve the absolute path in the resource resolver, using
            // only those parts of the path as if it would be request path
             = cRequest.getResourceResolver().resolve(absPath);
            // if the resource could not be resolved, fail gracefully
            if ( == null) {
                .error(
                    "include: Could not resolve {} to a resource, not including",
                    absPath);
                return;
            }
        }
        // ensure request path info and optional merges
        SlingRequestPathInfo info = getMergedRequestPathInfo(cRequest);
        cRequest.getRequestProgressTracker().log(
            "Including resource {0} ({1})"info);
        rd.getSlingRequestProcessor().dispatchRequest(requestsResponse,
            infoinclude);
    }

    
Returns a SlingRequestPathInfo object to use to select the servlet or script to call to render the resource to be dispatched to.

Note: If this request dispatcher has been created with resource type overwriting request dispatcher options, the resource to dispatch to may be wrapped with a SlingRequestDispatcher.TypeOverwritingResourceWrapper as a result of calling this method.

            final SlingHttpServletRequest cRequest) {
        SlingRequestPathInfo info = new SlingRequestPathInfo();
        info = info.merge(cRequest.getRequestPathInfo());
        // merge request dispatcher options and resource type overwrite
        if ( != null) {
            info = info.merge();
            // ensure overwritten resource type
            String rtOverwrite = .getForceResourceType();
            if (rtOverwrite != null
                && !rtOverwrite.equals(.getResourceType())) {
                 = new TypeOverwritingResourceWrapper(,
                    rtOverwrite);
            }
        }
        return info;
    }
    private Object setAttribute(final ServletRequest request,
            final String namefinal Object value) {
        final Object oldValue = request.getAttribute(name);
        request.setAttribute(namevalue);
        return oldValue;
    }
    private static class TypeOverwritingResourceWrapper extends ResourceWrapper {
        private final String resourceType;
        TypeOverwritingResourceWrapper(Resource delegateeString resourceType) {
            super(delegatee);
            this. = resourceType;
        }
        public String getResourceType() {
            return ;
        }

        
Overwrite this here because the wrapped resource will return null as a super type instead of the resource type overwritten here
        @Override
        public String getResourceSuperType() {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X