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
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
 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,
         final Object v2 = setAttribute(request,
         final Object v3 = setAttribute(request,
         final Object v4 = setAttribute(request,
         final Object v5 = setAttribute(request,
         final Object v6 = setAttribute(request,
         final Object v7 = setAttribute(request,
         final Object v8 = setAttribute(request,
        try {
        } finally {
            // reset inclusion request attributes to previous values
    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
        // ensure inclusion information attributes are not set
        // now just include as normal
        // 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
    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);
        RequestProgressTracker requestProgressTracker = cRequest.getRequestProgressTracker();
        // 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",
        if ( == null) {
            String timerName = "resolveIncludedResource(" + absPath + ")";
            // 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) {
                    "include: Could not resolve {} to a resource, not including",
                    "path={0} resolves to Resource={1}",
        // ensure request path info and optional merges
        SlingRequestPathInfo info = getMergedRequestPathInfo(cRequest);
            "Including resource {0} ({1})"info);

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(,
        return info;
    private Object setAttribute(final ServletRequest request,
            final String namefinal Object value) {
        final Object oldValue = request.getAttribute(name);
        return oldValue;
    private static class TypeOverwritingResourceWrapper extends ResourceWrapper {
        private final String resourceType;
        TypeOverwritingResourceWrapper(Resource delegateeString resourceType) {
            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
        public String getResourceSuperType() {
            return null;
New to GrepCode? Check out our FAQ X