struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [1/2] git commit: Added support for using freemarker templates for resource requests
Date Tue, 08 Jul 2014 21:06:03 GMT
Repository: struts
Updated Branches:
  refs/heads/develop 01e338098 -> cc89601b6


Added support for using freemarker templates for resource requests


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/2786455a
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/2786455a
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/2786455a

Branch: refs/heads/develop
Commit: 2786455a255ce40b83b2a2c809a6dc8c734337cd
Parents: 63de773
Author: Kristoffer Michael <kristoffer@eduix.fi>
Authored: Tue Jun 24 09:21:12 2014 +0300
Committer: Kristoffer Michael <kristoffer@eduix.fi>
Committed: Tue Jun 24 09:21:12 2014 +0300

----------------------------------------------------------------------
 .../portlet/context/PortletActionContext.java   | 14 ++++++++
 .../freemarker/PortletFreemarkerResult.java     | 37 ++++++++++++++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/2786455a/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
----------------------------------------------------------------------
diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
b/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
index 92e0d74..a51a3ca 100644
--- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
+++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
@@ -35,6 +35,7 @@ import javax.portlet.PortletRequest;
 import javax.portlet.PortletResponse;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
+import javax.portlet.ResourceResponse;
 import java.util.Map;
 
 import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE;
@@ -113,6 +114,19 @@ public class PortletActionContext {
         }
         return (ActionResponse) getContext().get(RESPONSE);
     }
+    
+    /**
+     * Get the ResourceResponse. Can only be invoked in the resource phase.
+     *
+     * @return The current ResourceResponse.
+     * @throws IllegalStateException If the method is invoked in the wrong phase.
+     */
+    public static ResourceResponse getResourceResponse() {
+       if (!getPhase().isResource()) {
+           throw new IllegalStateException("ResourceResponse cannot be obtained in event
phase");
+       }
+       return (ResourceResponse) getContext().get(RESPONSE);
+    }
 
     /**
      * Get the action namespace of the portlet. Used to organize actions for multiple portlets
in

http://git-wip-us.apache.org/repos/asf/struts/blob/2786455a/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
----------------------------------------------------------------------
diff --git a/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
b/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
index 3112fb9..71d0cee 100644
--- a/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
+++ b/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
@@ -38,6 +38,7 @@ import org.apache.struts2.views.util.ResourceUtil;
 
 import javax.portlet.ActionResponse;
 import javax.portlet.PortletException;
+import javax.portlet.ResourceResponse;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -103,9 +104,11 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
             throws IOException, TemplateException, PortletException {
         PortletPhase phase = PortletActionContext.getPhase();
         if (phase.isAction()) {
-            executeActionResult(location, invocation);
+           executeActionResult(location, invocation);
         } else if (phase.isRender()) {
-            executeRenderResult(location, invocation);
+           executeRenderResult(location, invocation);
+        } else if (phase.isResource()){
+           executeResourceResult(location, invocation);
         }
     }
 
@@ -147,6 +150,36 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
             }
         }
     }
+    
+    private void executeResourceResult(String location, ActionInvocation invocation)
+             throws TemplateException, IOException, PortletException {
+         this.location = location;
+         this.invocation = invocation;
+         this.configuration = getConfiguration();
+         this.wrapper = getObjectWrapper();
+
+         HttpServletRequest req = ServletActionContext.getRequest();
+
+         if (!location.startsWith("/")) {
+             String base = ResourceUtil.getResourceBase(req);
+             location = base + "/" + location;
+         }
+
+         Template template = configuration.getTemplate(location, deduceLocale());
+         TemplateModel model = createModel();
+         // Give subclasses a chance to hook into preprocessing
+         if (preTemplateProcess(template, model)) {
+             try {
+                 // Process the template
+                 ResourceResponse response = PortletActionContext.getResourceResponse();
+                 response.setContentType(pContentType);
+                 template.process(model, response.getWriter());
+             } finally {
+                 // Give subclasses a chance to hook into postprocessing
+                 postTemplateProcess(template, model);
+             }
+         }
+     }
 
     /**
      * This method is called from {@link #doExecute(String, ActionInvocation)}


Mime
View raw message