incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r618959 - /incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java
Date Wed, 06 Feb 2008 11:00:21 GMT
Author: fmeschbe
Date: Wed Feb  6 03:00:18 2008
New Revision: 618959

URL: http://svn.apache.org/viewvc?rev=618959&view=rev
Log:
SLING-222 modify last commit slightly to not throw or send an error in case of
inability to include but log a message instead. Also use ResourceResolver.resolve
to resolve the resource from the supplied path to support request extensions,
selectors and suffixes.

Modified:
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java?rev=618959&r1=618958&r2=618959&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java
(original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/SlingRequestDispatcher.java
Wed Feb  6 03:00:18 2008
@@ -32,94 +32,92 @@
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 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 path,
-            RequestDispatcherOptions options) {
+    public SlingRequestDispatcher(String path, RequestDispatcherOptions options) {
         this.path = path;
         this.options = options;
-
         this.resource = null;
     }
 
     public SlingRequestDispatcher(Resource resource,
             RequestDispatcherOptions options) {
+
         this.resource = resource;
         this.options = options;
-        // TODO Is the empty path ok?
-        this.path = (resource == null ? "" : resource.getPath());
+        this.path = resource.getPath();
     }
 
     public void include(ServletRequest request, ServletResponse sResponse)
             throws ServletException, IOException {
 
-        /** 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. 
+        /**
+         * 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.
          */
-        
-        // this may throw an exception in case loading fails, which is
-        // ok here, if no content is available at that path null is
-        // return, which results in using the servlet container
+
         SlingHttpServletRequest cRequest = RequestData.unwrap(request);
         RequestData rd = RequestData.getRequestData(cRequest);
         String absPath = getAbsolutePath(cRequest, path);
-        
-        if( ! (sResponse instanceof HttpServletResponse )) {
-            throw new ServletException("Response is not an HttpServletResponse, cannot continue");
+
+        // if the response is not an HttpServletResponse, fail gracefully not
+        // doing anything
+        if (!(sResponse instanceof HttpServletResponse)) {
+            log.error("include: Failed to include {}, response has wrong type", absPath);
+            return;
         }
-        final HttpServletResponse response = (HttpServletResponse)sResponse;
+        
+        final HttpServletResponse response = (HttpServletResponse) sResponse;
 
         if (resource == null) {
-            ResourceResolver rr = cRequest.getResourceResolver();
-            
-            if(absPath.startsWith(cRequest.getContextPath())) {
+
+            // the absolute path may have the context path, cut it off
+            if (absPath.startsWith(cRequest.getContextPath())) {
                 absPath = absPath.substring(cRequest.getContextPath().length());
             }
+
+            // resolve the absolute path in the resource resolver, using
+            // only those parts of the path as if it would be request path
+            ResourceResolver rr = cRequest.getResourceResolver();
+            resource = rr.resolve(absPath);
             
-            // remove extension before attempting to resolve, like in a "normal" request
-            // TODO use the same parsing as for normal resources?
-            final int lastSlash = absPath.lastIndexOf('/');
-            final int lastDot = absPath.lastIndexOf('.');
-            if(lastDot > lastSlash && lastDot >= 0) {
-                absPath = absPath.substring(0, lastDot);
+            // if the resource could not be resolved, fail gracefully
+            if (resource == null) {
+                log.error("include: Could not resolve {} to a resource, not including", absPath);
             }
-            resource = rr.getResource(absPath);
         }
 
-        if (resource == null) {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found in include:
" + absPath);
-            
-            // The code below was previously used but causes SLING-222...not sure what's
best
-            // rd.getSlingMainServlet().includeServlet(request, response, path);
-
-        } else {
-
-            // ensure request path info and optional merges
-            SlingRequestPathInfo info = new SlingRequestPathInfo(resource, absPath);
-            info = info.merge(cRequest.getRequestPathInfo());
-
-            if (options != null) {
-                info = info.merge(options);
-
-                // ensure overwritten resource type
-                String rtOverwrite = options.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
-                if (rtOverwrite != null
-                    && !rtOverwrite.equals(resource.getResourceType())) {
-                    resource = new ResourceWrapper(resource, rtOverwrite);
-                }
+        // ensure request path info and optional merges
+        SlingRequestPathInfo info = new SlingRequestPathInfo(resource, absPath);
+        info = info.merge(cRequest.getRequestPathInfo());
+
+        // merge request dispatcher options and resource type overwrite
+        if (options != null) {
+            info = info.merge(options);
+
+            // ensure overwritten resource type
+            String rtOverwrite = options.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
+            if (rtOverwrite != null
+                && !rtOverwrite.equals(resource.getResourceType())) {
+                resource = new ResourceWrapper(resource, rtOverwrite);
             }
-
-            rd.getSlingMainServlet().includeContent(request, response,
-                resource, info);
         }
+
+        rd.getSlingMainServlet().includeContent(request, response,
+            resource, info);
     }
 
     public void forward(ServletRequest request, ServletResponse response)



Mime
View raw message