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 2010/04/12 15:37:30 UTC

svn commit: r933231 - in /sling/trunk/bundles/scripting/jsp-taglib: ./ src/main/java/org/apache/sling/scripting/jsp/taglib/ src/main/resources/META-INF/

Author: cziegeler
Date: Mon Apr 12 13:37:29 2010
New Revision: 933231

URL: http://svn.apache.org/viewvc?rev=933231&view=rev
Log:
SLING-1479 : Add JSP Tag for new script resolution

Added:
    sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java   (with props)
Modified:
    sling/trunk/bundles/scripting/jsp-taglib/NOTICE
    sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java
    sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java
    sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/NOTICE
    sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld

Modified: sling/trunk/bundles/scripting/jsp-taglib/NOTICE
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/NOTICE?rev=933231&r1=933230&r2=933231&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/NOTICE (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/NOTICE Mon Apr 12 13:37:29 2010
@@ -1,5 +1,5 @@
 Apache Sling JSP Taglib
-Copyright 2008-2009 The Apache Software Foundation
+Copyright 2008-2010 The Apache Software Foundation
 
 Apache Sling is based on source code originally developed 
 by Day Software (http://www.day.com/).

Added: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java?rev=933231&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java (added)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java Mon Apr 12 13:37:29 2010
@@ -0,0 +1,171 @@
+/*
+ * 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 javax.servlet.Servlet;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.BodyContent;
+import javax.servlet.jsp.tagext.TagSupport;
+
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.api.servlets.ServletResolver;
+import org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>EvalTagHandler</code> implements the
+ * <code>&lt;sling:eval&gt;</code> custom tag.
+ */
+public class EvalTagHandler extends TagSupport {
+
+    private static final long serialVersionUID = 7070941156517599283L;
+
+    /** default log */
+    private static final Logger log = LoggerFactory.getLogger(EvalTagHandler.class);
+
+    /** resource argument */
+    private Resource resource;
+
+    /** script argument */
+    private String script;
+
+    /** resource type argument */
+    private String resourceType;
+
+    /** ignore resource type hierarchy */
+    private boolean ignoreResourceTypeHierarchy = false;
+
+    /** flush argument */
+    private boolean flush = false;
+
+
+    /**
+     * Called after the body has been processed.
+     *
+     * @return whether additional evaluations of the body are desired
+     */
+    public int doEndTag() throws JspException {
+        log.debug("EvalTagHandler doEndTag");
+
+        final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(
+                SlingBindings.class.getName());
+        final SlingScriptHelper scriptHelper = bindings.getSling();
+        final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
+
+        final Servlet servlet;
+        if ( !this.ignoreResourceTypeHierarchy ) {
+            // detecte resource
+            final Resource resource;
+            if ( this.resource != null ) {
+                resource = this.resource;
+            } else if ( this.resourceType != null ) {
+                resource = new SyntheticResource(bindings.getRequest().getResourceResolver(),
+                        bindings.getResource().getPath(), this.resourceType);
+            } else {
+                resource = bindings.getResource();
+            }
+            servlet = servletResolver.resolveServlet(resource, this.script);
+        } else {
+            final ResourceResolver rr = bindings.getRequest().getResourceResolver();
+            final String scriptPath;
+            if (!script.startsWith("/")) {
+
+                // resolve relative script
+                String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
+                // check if parent resides on search path
+                for (String sp: rr.getSearchPath()) {
+                    if (parentPath.startsWith(sp)) {
+                        parentPath = parentPath.substring(sp.length());
+                        break;
+                    }
+                }
+                scriptPath = parentPath + '/' + script;
+
+            } else {
+
+                scriptPath = this.script;
+            }
+            servlet = servletResolver.resolveServlet(rr, scriptPath);
+        }
+
+        if (servlet == null) {
+            throw new JspException("Could not find script " + script);
+        }
+
+        try {
+            if (flush && !(pageContext.getOut() instanceof BodyContent)) {
+                // might throw an IOException of course
+                pageContext.getOut().flush();
+            }
+
+            // wrap the response to get the correct output order
+            SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(
+                pageContext);
+
+            servlet.service(pageContext.getRequest(), response);
+
+            return EVAL_PAGE;
+
+        } catch (Exception e) {
+            log.error("Error while executing script " + script, e);
+            throw new JspException("Error while executing script " + script, e);
+
+        }
+    }
+
+    /**
+     * @see javax.servlet.jsp.tagext.TagSupport#setPageContext(javax.servlet.jsp.PageContext)
+     */
+    public void setPageContext(final PageContext pageContext) {
+        super.setPageContext(pageContext);
+
+        // init local fields, since tag might be reused
+        resource = null;
+        resourceType = null;
+        ignoreResourceTypeHierarchy = false;
+        script = null;
+        flush = false;
+    }
+
+    public void setFlush(boolean flush) {
+        this.flush = flush;
+    }
+
+    public void setResource(final Resource rsrc) {
+        this.resource = rsrc;
+    }
+
+    public void setScript(final String script) {
+        this.script = script;
+    }
+
+    public void setResourceType(final String rsrcType) {
+        this.resourceType = rsrcType;
+    }
+
+    public void setIgnoreResourceTypeHierarchy(final boolean flag) {
+        this.ignoreResourceTypeHierarchy = flag;
+    }
+}

Propchange: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/EvalTagHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java?rev=933231&r1=933230&r2=933231&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ForwardTagHandler.java Mon Apr 12 13:37:29 2010
@@ -30,6 +30,8 @@ import javax.servlet.jsp.JspTagException
  */
 public class ForwardTagHandler extends AbstractDispatcherTagHandler {
 
+    private static final long serialVersionUID = 4516800311593983971L;
+
     protected void dispatch(RequestDispatcher dispatcher,
             ServletRequest request, ServletResponse response)
             throws IOException, ServletException, JspTagException {

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java?rev=933231&r1=933230&r2=933231&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java Mon Apr 12 13:37:29 2010
@@ -31,6 +31,8 @@ import javax.servlet.jsp.tagext.BodyCont
  */
 public class IncludeTagHandler extends AbstractDispatcherTagHandler {
 
+    private static final long serialVersionUID = 2835586777145471683L;
+
     /** flush argument */
     private boolean flush = false;
 

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/NOTICE
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/NOTICE?rev=933231&r1=933230&r2=933231&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/NOTICE (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/NOTICE Mon Apr 12 13:37:29 2010
@@ -1,5 +1,5 @@
 Apache Sling JSP Taglib
-Copyright 2008-2009 The Apache Software Foundation
+Copyright 2008-2010 The Apache Software Foundation
 
 Apache Sling is based on source code originally developed 
 by Day Software (http://www.day.com/).

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld?rev=933231&r1=933230&r2=933231&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/taglib.tld Mon Apr 12 13:37:29 2010
@@ -236,4 +236,69 @@
         </attribute>
     </tag>
 
+    <tag>
+        <description>
+            Evaluates a script invocation and includes the result
+            in the current page.
+        </description>
+        <name>eval</name>
+        <tag-class>
+            org.apache.sling.scripting.jsp.taglib.EvalTagHandler
+        </tag-class>
+        <body-content>empty</body-content>
+        <attribute>
+            <description>
+                Whether to flush the output before including the target
+            </description>
+            <name>flush</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <type>boolean</type>
+        </attribute>
+        <attribute>
+            <description>
+                The path to the script object to include in the current
+                request processing. By default, the current resource
+                is used for script resolving. This behaviour can 
+                be changed by specifying either resource, resourceType
+                or ignoreResourceTypeHierarchy.
+            </description>
+            <name>script</name>
+            <required>true</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                The resource object to include in the current request
+                processing. This attribute is optional. If it is
+                specified, resourceType should not be used. If both
+                are used, resource takes precedence.
+            </description>
+            <name>resource</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <type>org.apache.sling.api.resource.Resource</type>
+        </attribute>
+        <attribute>
+            <description>
+                The resource type of a resource to include. This
+                attribute is optional. If it is specified, resource
+                should not be used. If bot are used, resource
+                takes precedence.
+            </description>
+            <name>resourceType</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+        <attribute>
+            <description>
+                Prevents using the resource type hierarchy for searching
+                a script.
+            </description>
+            <name>ignoreResourceTypeHierarchy</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <type>boolean</type>
+        </attribute>
+    </tag>
 </taglib>