1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.kernel.velocity.VelocityContext;
27  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
28  import com.liferay.portal.struts.StrutsUtil;
29  import com.liferay.portal.velocity.VelocityResourceListener;
30  
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.GenericPortlet;
37  import javax.portlet.MimeResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletContext;
40  import javax.portlet.PortletException;
41  import javax.portlet.PortletRequest;
42  import javax.portlet.PortletResponse;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  import javax.portlet.ResourceRequest;
46  import javax.portlet.ResourceResponse;
47  
48  import org.apache.velocity.io.VelocityWriter;
49  import org.apache.velocity.util.SimplePool;
50  
51  /**
52   * <a href="VelocityPortlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Steven P. Goldsmith
56   * @author Raymond Augé
57   *
58   */
59  public class VelocityPortlet extends GenericPortlet {
60  
61      public void init(PortletConfig portletConfig) throws PortletException {
62          super.init(portletConfig);
63  
64          PortletContext portletContext = portletConfig.getPortletContext();
65  
66          _portletContextName = portletContext.getPortletContextName();
67  
68          _actionTemplateId = getVelocityTemplateId(
69              getInitParameter("action-template"));
70          _editTemplateId = getVelocityTemplateId(
71              getInitParameter("edit-template"));
72          _helpTemplateId = getVelocityTemplateId(
73              getInitParameter("help-template"));
74          _resourceTemplateId = getVelocityTemplateId(
75              getInitParameter("resource-template"));
76          _viewTemplateId = getVelocityTemplateId(
77              getInitParameter("view-template"));
78      }
79  
80      public void processAction(
81              ActionRequest actionRequest, ActionResponse actionResponse)
82          throws PortletException {
83  
84          if (Validator.isNull(_actionTemplateId)) {
85              return;
86          }
87  
88          try {
89              mergeTemplate(_actionTemplateId, actionRequest, actionResponse);
90          }
91          catch (Exception e) {
92              throw new PortletException(e);
93          }
94      }
95  
96      public void serveResource(
97              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
98          throws PortletException, IOException {
99  
100         if (Validator.isNull(_resourceTemplateId)) {
101             super.serveResource(resourceRequest, resourceResponse);
102 
103             return;
104         }
105 
106         try {
107             mergeTemplate(
108                 _resourceTemplateId, resourceRequest, resourceResponse);
109         }
110         catch (Exception e) {
111             throw new PortletException(e);
112         }
113     }
114 
115     public void doEdit(
116             RenderRequest renderRequest, RenderResponse renderResponse)
117         throws IOException, PortletException {
118 
119         if (renderRequest.getPreferences() == null) {
120             super.doEdit(renderRequest, renderResponse);
121 
122             return;
123         }
124 
125         try {
126             mergeTemplate(_editTemplateId, renderRequest, renderResponse);
127         }
128         catch (Exception e) {
129             throw new PortletException(e);
130         }
131     }
132 
133     public void doHelp(
134             RenderRequest renderRequest, RenderResponse renderResponse)
135         throws PortletException {
136 
137         try {
138             mergeTemplate(_helpTemplateId, renderRequest, renderResponse);
139         }
140         catch (Exception e) {
141             throw new PortletException(e);
142         }
143     }
144 
145     public void doView(
146             RenderRequest renderRequest, RenderResponse renderResponse)
147         throws PortletException {
148 
149         try {
150             mergeTemplate(_viewTemplateId, renderRequest, renderResponse);
151         }
152         catch (Exception e) {
153             throw new PortletException(e);
154         }
155     }
156 
157     protected VelocityContext getVelocityContext(
158         PortletRequest portletRequest, PortletResponse portletResponse) {
159 
160         VelocityContext velocityContext =
161             VelocityEngineUtil.getWrappedStandardToolsContext();
162 
163         velocityContext.put("portletConfig", getPortletConfig());
164         velocityContext.put("portletContext", getPortletContext());
165         velocityContext.put("preferences", portletRequest.getPreferences());
166         velocityContext.put(
167             "userInfo", portletRequest.getAttribute(PortletRequest.USER_INFO));
168 
169         velocityContext.put("portletRequest", portletRequest);
170 
171         if (portletRequest instanceof ActionRequest) {
172             velocityContext.put("actionRequest", portletRequest);
173         }
174         else if (portletRequest instanceof RenderRequest) {
175             velocityContext.put("renderRequest", portletRequest);
176         }
177         else {
178             velocityContext.put("resourceRequest", portletRequest);
179         }
180 
181         velocityContext.put("portletResponse", portletResponse);
182 
183         if (portletResponse instanceof ActionResponse) {
184             velocityContext.put("actionResponse", portletResponse);
185         }
186         else if (portletRequest instanceof RenderResponse) {
187             velocityContext.put("renderResponse", portletResponse);
188         }
189         else {
190             velocityContext.put("resourceResponse", portletResponse);
191         }
192 
193         return velocityContext;
194     }
195 
196     protected String getVelocityTemplateId(String name) {
197         if (Validator.isNull(name)) {
198             return name;
199         }
200 
201         StringBuilder sb = new StringBuilder();
202 
203         sb.append(_portletContextName);
204         sb.append(VelocityResourceListener.SERVLET_SEPARATOR);
205         sb.append(StrutsUtil.TEXT_HTML_DIR);
206         sb.append(name);
207 
208         return sb.toString();
209     }
210 
211     protected void mergeTemplate(
212             String velocityTemplateId, PortletRequest portletRequest,
213             PortletResponse portletResponse)
214         throws Exception {
215 
216         mergeTemplate(
217             velocityTemplateId,
218             getVelocityContext(portletRequest, portletResponse),
219             portletRequest, portletResponse);
220     }
221 
222     protected void mergeTemplate(
223             String velocityTemplateId, VelocityContext velocityContext,
224             PortletRequest portletRequest, PortletResponse portletResponse)
225         throws Exception {
226 
227         if (portletResponse instanceof MimeResponse) {
228             MimeResponse mimeResponse = (MimeResponse)portletResponse;
229 
230             mimeResponse.setContentType(
231                 portletRequest.getResponseContentType());
232         }
233 
234         VelocityWriter velocityWriter = null;
235 
236         try {
237             velocityWriter = (VelocityWriter)_writerPool.get();
238 
239             PrintWriter output = null;
240 
241             if (portletResponse instanceof MimeResponse) {
242                 MimeResponse mimeResponse = (MimeResponse)portletResponse;
243 
244                 output = mimeResponse.getWriter();
245             }
246             else {
247                 output = new PrintWriter(System.out);
248             }
249 
250             if (velocityWriter == null) {
251                 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
252             }
253             else {
254                 velocityWriter.recycle(output);
255             }
256 
257             VelocityEngineUtil.mergeTemplate(
258                 velocityTemplateId, null, velocityContext, velocityWriter);
259         }
260         finally {
261             try {
262                 if (velocityWriter != null) {
263                     velocityWriter.flush();
264                     velocityWriter.recycle(null);
265 
266                     _writerPool.put(velocityWriter);
267                 }
268             }
269             catch (Exception e) {
270             }
271         }
272     }
273 
274     private static SimplePool _writerPool = new SimplePool(40);
275 
276     private String _portletContextName;
277     private String _actionTemplateId;
278     private String _editTemplateId;
279     private String _helpTemplateId;
280     private String _resourceTemplateId;
281     private String _viewTemplateId;
282 
283 }