You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/09/08 16:51:38 UTC

svn commit: r693119 - /incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java

Author: cziegeler
Date: Mon Sep  8 07:51:36 2008
New Revision: 693119

URL: http://svn.apache.org/viewvc?rev=693119&view=rev
Log:
SLING-650 : Check if resource has property sling:target for doing the redirect.

Modified:
    incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java

Modified: incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java?rev=693119&r1=693118&r2=693119&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java (original)
+++ incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/RedirectServlet.java Mon Sep  8 07:51:36 2008
@@ -30,6 +30,7 @@
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
 import org.apache.sling.servlets.get.helpers.JsonRendererServlet;
 
@@ -53,13 +54,13 @@
  * sent where the target is the relative URL from the current resource to the
  * target resource. Selectors, extension, suffix and query string are also
  * appended to the redirect URL.
- * 
+ *
  * @scr.component immediate="true" metatype="no"
  * @scr.service interface="javax.servlet.Servlet"
- * 
+ *
  * @scr.property name="service.description" value="Request Redirect Servlet"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
- * 
+ *
  * @scr.property name="sling.servlet.resourceTypes" value="sling:redirect"
  * @scr.property name="sling.servlet.methods" value="GET"
  */
@@ -81,34 +82,44 @@
             return;
         }
 
-        Resource targetResource = request.getResourceResolver().getResource(
-            request.getResource(), TARGET_PROP);
-        if (targetResource == null) {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND,
-                "Missing target for redirection");
-            return;
-        }
-
         String targetPath = null;
 
-        // if the target resource is a reference, we can adapt to node
-        Node targetNode = targetResource.adaptTo(Node.class);
-        if (targetNode != null) {
+        // convert resource to a value map
+        final Resource rsrc = request.getResource();
+        final ValueMap valueMap = rsrc.adaptTo(ValueMap.class);
+        if ( valueMap != null ) {
+            targetPath = valueMap.get(TARGET_PROP, String.class);
+        }
+        if ( targetPath == null ) {
+            // old behaviour
+            final Resource targetResource = request.getResourceResolver().getResource(
+                    rsrc, TARGET_PROP);
+            if (targetResource == null) {
+                response.sendError(HttpServletResponse.SC_NOT_FOUND,
+                    "Missing target for redirection");
+                return;
+            }
 
-            // get the node path (aka resource path)
-            try {
-                targetPath = targetNode.getPath();
-            } catch (RepositoryException re) {
-                throw new ServletException(
-                    "Failed to access repository for redirection", re);
 
-            }
+            // if the target resource is a reference, we can adapt to node
+            Node targetNode = targetResource.adaptTo(Node.class);
+            if (targetNode != null) {
+
+                // get the node path (aka resource path)
+                try {
+                    targetPath = targetNode.getPath();
+                } catch (RepositoryException re) {
+                    throw new ServletException(
+                        "Failed to access repository for redirection", re);
 
-        } else {
+                }
 
-            // if the target resource is a path (string), redirect there
-            targetPath = targetResource.adaptTo(String.class);
+            } else {
 
+                // if the target resource is a path (string), redirect there
+                targetPath = targetResource.adaptTo(String.class);
+
+            }
         }
 
         // if we got a target path, make it external and redirect to it
@@ -124,7 +135,7 @@
 
         // no way of finding the target, just fail
         response.sendError(HttpServletResponse.SC_NOT_FOUND,
-            "Cannot redirect to target resource " + targetResource);
+            "Cannot redirect to target resource " + targetPath);
     }
 
     /**