You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/10/20 11:37:54 UTC

svn commit: r706167 - in /incubator/sling/trunk/scripting/jsp-taglib/src/main: java/org/apache/sling/scripting/jsp/taglib/ resources/META-INF/

Author: fmeschbe
Date: Mon Oct 20 02:37:54 2008
New Revision: 706167

URL: http://svn.apache.org/viewvc?rev=706167&view=rev
Log:
SLING-703 Implement forward tag. The code is almost the same, except in the
actual method called on the RequestDispatcher and the flush attribute
which is only supported for the include tag. Therefore, added an
abstract dispatcher handler, which does almost all work except for
the flushing (include only) and actual dispatching

Added:
    incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractDispatcherTagHandler.java
    incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java
Modified:
    incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java
    incubator/sling/trunk/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld

Added: incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractDispatcherTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractDispatcherTagHandler.java?rev=706167&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractDispatcherTagHandler.java (added)
+++ incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractDispatcherTagHandler.java Mon Oct 20 02:37:54 2008
@@ -0,0 +1,183 @@
+/*
+ * 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.scripting.jsp.taglib;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.JspTagException;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.TagSupport;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper;
+import org.apache.sling.scripting.jsp.util.TagUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>IncludeTagHandler</code> implements the
+ * <code>&lt;sling:include&gt;</code> custom tag.
+ */
+public abstract class AbstractDispatcherTagHandler extends TagSupport {
+
+    private static final long serialVersionUID = 6275972595573203863L;
+
+    /** default log */
+    private static final Logger log = LoggerFactory.getLogger(AbstractDispatcherTagHandler.class);
+
+    /** resource argument */
+    private Resource resource;
+
+    /** path argument */
+    private String path;
+
+    /** resource type argument */
+    private String resourceType;
+
+    /** replace selectors argument */
+    private String replaceSelectors;
+
+    /** additional selectors argument */
+    private String addSelectors;
+
+    /** replace suffix argument */
+    private String replaceSuffix;
+
+    /**
+     * Called after the body has been processed.
+     * 
+     * @return whether additional evaluations of the body are desired
+     */
+    public int doEndTag() throws JspException {
+        log.debug("AbstractDispatcherTagHandler.doEndTag");
+
+        final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
+
+        // set request dispatcher options according to tag attributes. This
+        // depends on the implementation, that using a "null" argument
+        // has no effect
+        final RequestDispatcherOptions opts = new RequestDispatcherOptions();
+        opts.setForceResourceType(resourceType);
+        opts.setReplaceSelectors(replaceSelectors);
+        opts.setAddSelectors(addSelectors);
+        opts.setReplaceSuffix(replaceSuffix);
+
+        // ensure the path (if set) is absolute and normalized
+        if (path != null) {
+            if (!path.startsWith("/")) {
+                path = request.getResource().getPath() + "/" + path;
+            }
+            path = ResourceUtil.normalize(path);
+        }
+
+        // check the resource
+        if (resource == null) {
+            if (path == null) {
+                // neither resource nor path is defined, use current resource
+                resource = request.getResource();
+            } else {
+                // check whether the path (would) resolve, else SyntheticRes.
+                Resource tmp = request.getResourceResolver().resolve(path);
+                if (tmp == null && resourceType != null) {
+                    resource = new SyntheticResource(
+                        request.getResourceResolver(), path, resourceType);
+
+                    // remove resource type overwrite as synthetic resource
+                    // is correctly typed as requested
+                    opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
+                }
+            }
+        }
+
+        try {
+            // create a dispatcher for the resource or path
+            RequestDispatcher dispatcher;
+            if (resource != null) {
+                dispatcher = request.getRequestDispatcher(resource, opts);
+            } else {
+                dispatcher = request.getRequestDispatcher(path, opts);
+            }
+
+            if (dispatcher != null) {
+                SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(
+                    pageContext);
+                dispatch(dispatcher, request, response);
+            } else {
+                TagUtil.log(log, pageContext, "No content to include...", null);
+            }
+
+        } catch (JspTagException jte) {
+            throw jte;
+        } catch (IOException ioe) {
+            throw new JspTagException(ioe);
+        } catch (ServletException ce) {
+            throw new JspTagException(TagUtil.getRootCause(ce));
+        }
+
+        return EVAL_PAGE;
+    }
+
+    protected abstract void dispatch(RequestDispatcher dispatcher,
+            ServletRequest request, ServletResponse response)
+            throws IOException, ServletException, JspTagException;
+
+    public void setPageContext(PageContext pageContext) {
+        super.setPageContext(pageContext);
+
+        // init local fields, since tag might be reused
+        resource = null;
+        resourceType = null;
+        replaceSelectors = null;
+        addSelectors = null;
+        replaceSuffix = null;
+        path = null;
+    }
+
+    public void setResource(Resource rsrc) {
+        this.resource = rsrc;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public void setResourceType(String rsrcType) {
+        this.resourceType = rsrcType;
+    }
+
+    public void setReplaceSelectors(String selectors) {
+        this.replaceSelectors = selectors;
+    }
+
+    public void setAddSelectors(String selectors) {
+        this.addSelectors = selectors;
+    }
+
+    public void setReplaceSuffix(String suffix) {
+        this.replaceSuffix = suffix;
+    }
+}

Added: incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java?rev=706167&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java (added)
+++ incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java Mon Oct 20 02:37:54 2008
@@ -0,0 +1,43 @@
+/*
+ * 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.scripting.jsp.taglib;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.jsp.JspTagException;
+
+/**
+ * The <code>ForwardTagHandler</code> implements the
+ * <code>&lt;sling:forward&gt;</code> custom tag.
+ */
+public class ForwardTagHandler extends AbstractDispatcherTagHandler {
+
+    protected void dispatch(RequestDispatcher dispatcher,
+            ServletRequest request, ServletResponse response)
+            throws IOException, ServletException, JspTagException {
+        try {
+            dispatcher.forward(request, response);
+        } catch (IllegalStateException ise) {
+            throw new JspTagException(ise);
+        }
+    }
+
+}

Modified: incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java?rev=706167&r1=706166&r2=706167&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java (original)
+++ incubator/sling/trunk/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java Mon Oct 20 02:37:54 2008
@@ -20,131 +20,31 @@
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
-import javax.servlet.jsp.JspException;
-import javax.servlet.jsp.JspTagException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
 import javax.servlet.jsp.PageContext;
 import javax.servlet.jsp.tagext.BodyContent;
-import javax.servlet.jsp.tagext.TagSupport;
-
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.api.SlingHttpServletResponse;
-import org.apache.sling.api.request.RequestDispatcherOptions;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceUtil;
-import org.apache.sling.api.resource.SyntheticResource;
-import org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper;
-import org.apache.sling.scripting.jsp.util.TagUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * The <code>IncludeTagHandler</code> implements the
  * <code>&lt;sling:include&gt;</code> custom tag.
  */
-public class IncludeTagHandler extends TagSupport {
-
-    private static final long serialVersionUID = 6275972595573203863L;
-
-    /** default log */
-    private static final Logger log = LoggerFactory.getLogger(IncludeTagHandler.class);
+public class IncludeTagHandler extends AbstractDispatcherTagHandler {
 
     /** flush argument */
     private boolean flush = false;
 
-    /** resource argument */
-    private Resource resource;
-
-    /** path argument */
-    private String path;
-
-    /** resource type argument */
-    private String resourceType;
-
-    /** replace selectors argument */
-    private String replaceSelectors;
-
-    /** additional selectors argument */
-    private String addSelectors;
-
-    /** replace suffix argument */
-    private String replaceSuffix;
-
-    /**
-     * Called after the body has been processed.
-     *
-     * @return whether additional evaluations of the body are desired
-     */
-    public int doEndTag() throws JspException {
-        log.debug("IncludeTagHandler.doEndTag");
-
-        final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
-
-        // set request dispatcher options according to tag attributes. This
-        // depends on the implementation, that using a "null" argument
-        // has no effect
-        final RequestDispatcherOptions opts = new RequestDispatcherOptions();
-        opts.setForceResourceType(resourceType);
-        opts.setReplaceSelectors(replaceSelectors);
-        opts.setAddSelectors(addSelectors);
-        opts.setReplaceSuffix(replaceSuffix);
-
-        // ensure the path (if set) is absolute and normalized
-        if (path != null) {
-            if (!path.startsWith("/")) {
-                path = request.getResource().getPath() + "/" + path;
-            }
-            path = ResourceUtil.normalize(path);
+    protected void dispatch(RequestDispatcher dispatcher,
+            ServletRequest request, ServletResponse response)
+            throws IOException, ServletException {
+
+        // optionally flush
+        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
+            // might throw an IOException of course
+            pageContext.getOut().flush();
         }
 
-        // check the resource
-        if (resource == null) {
-            if (path == null) {
-                // neither resource nor path is defined, use current resource
-                resource = request.getResource();
-            } else {
-                // check whether the path (would) resolve, else SyntheticRes.
-                Resource tmp = request.getResourceResolver().resolve(path);
-                if (tmp == null && resourceType != null) {
-                    resource = new SyntheticResource(
-                        request.getResourceResolver(), path, resourceType);
-
-                    // remove resource type overwrite as synthetic resource
-                    // is correctly typed as requested
-                    opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
-                }
-            }
-        }
-
-        try {
-            // optionally flush
-            if (flush && !(pageContext.getOut() instanceof BodyContent)) {
-                // might throw an IOException of course
-                pageContext.getOut().flush();
-            }
-
-            // create a dispatcher for the resource or path
-            RequestDispatcher dispatcher;
-            if (resource != null) {
-                dispatcher = request.getRequestDispatcher(resource, opts);
-            } else {
-                dispatcher = request.getRequestDispatcher(path, opts);
-            }
-
-            if (dispatcher != null) {
-                SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(
-                    pageContext);
-                dispatcher.include(request, response);
-            } else {
-                TagUtil.log(log, pageContext, "No content to include...", null);
-            }
-            
-        } catch (IOException ioe) {
-            throw new JspTagException(ioe);
-        } catch (ServletException ce) {
-            throw new JspTagException(TagUtil.getRootCause(ce));
-        }
-
-        return EVAL_PAGE;
+        dispatcher.include(request, response);
     }
 
     public void setPageContext(PageContext pageContext) {
@@ -152,39 +52,10 @@
 
         // init local fields, since tag might be reused
         flush = false;
-        resource = null;
-        resourceType = null;
-        replaceSelectors = null;
-        addSelectors = null;
-        replaceSuffix = null;
-        path = null;
     }
 
     public void setFlush(boolean flush) {
         this.flush = flush;
     }
 
-    public void setResource(Resource rsrc) {
-        this.resource = rsrc;
-    }
-
-    public void setPath(String path) {
-        this.path = path;
-    }
-
-    public void setResourceType(String rsrcType) {
-        this.resourceType = rsrcType;
-    }
-
-    public void setReplaceSelectors(String selectors) {
-        this.replaceSelectors = selectors;
-    }
-
-    public void setAddSelectors(String selectors) {
-        this.addSelectors = selectors;
-    }
-
-    public void setReplaceSuffix(String suffix) {
-        this.replaceSuffix = suffix;
-    }
 }

Modified: incubator/sling/trunk/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld?rev=706167&r1=706166&r2=706167&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld (original)
+++ incubator/sling/trunk/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld Mon Oct 20 02:37:54 2008
@@ -114,6 +114,81 @@
 
     <tag>
         <description>
+            Forwards a request to a resource rendering the current page
+        </description>
+        <name>forward</name>
+        <tag-class>
+            org.apache.sling.scripting.jsp.taglib.ForwardTagHandler
+        </tag-class>
+        <body-content>empty</body-content>
+        <attribute>
+            <description>
+                The resource object to forward the request to. Either
+                resource or path must be specified. If both are
+                specified, the resource takes precedences.
+            </description>
+            <name>resource</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <type>org.apache.sling.api.resource.Resource</type>
+        </attribute>
+        <attribute>
+            <description>
+                The path to the resource object to forward the request
+                to. If this path is relative it is appended to the path
+                of the current resource whose script is forwarding the
+                given resource. Either resource or path must be specified.
+                If both are specified, the resource takes precedences.
+            </description>
+            <name>path</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                The resource type of a resource to forward. If the resource
+                to be forwarded is specified with the path attribute,
+                which cannot be resolved to a resource, the tag may
+                create a synthetic resource object out of the path and
+                this resource type. If the resource type is set the path
+                must be the exact path to a resource object. That is,
+                adding parameters, selectors and extensions to the path
+                is not supported if the resource type is set.
+            </description>
+            <name>resourceType</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                When dispatching, replace selectors by the value
+                provided by this option.
+            </description>
+            <name>replaceSelectors</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                When dispatching, add the value provided by this option to the selectors.
+            </description>
+            <name>addSelectors</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                When dispatching, replace the suffix by the value
+                provided by this option.
+            </description>
+            <name>replaceSuffix</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+    </tag>
+
+    <tag>
+        <description>
             Defines regularly used scripting variables
         </description>
         <name>defineObjects</name>