Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 811BB1136C for ; Tue, 8 Jul 2014 21:06:04 +0000 (UTC) Received: (qmail 20535 invoked by uid 500); 8 Jul 2014 21:06:04 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 20488 invoked by uid 500); 8 Jul 2014 21:06:04 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 20465 invoked by uid 99); 8 Jul 2014 21:06:04 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Jul 2014 21:06:04 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id D36D49A843E; Tue, 8 Jul 2014 21:06:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Tue, 08 Jul 2014 21:06:03 -0000 Message-Id: <149cb417c5aa4a7091d812a05ce9636a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: Added support for using freemarker templates for resource requests 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 Authored: Tue Jun 24 09:21:12 2014 +0300 Committer: Kristoffer Michael 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)}