1   /**
2    * Copyright (c) 2000-2008 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.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.struts.StrutsUtil;
28  import com.liferay.portal.velocity.VelocityResourceListener;
29  import com.liferay.portal.velocity.VelocityVariables;
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.Template;
49  import org.apache.velocity.VelocityContext;
50  import org.apache.velocity.context.Context;
51  import org.apache.velocity.io.VelocityWriter;
52  import org.apache.velocity.runtime.RuntimeSingleton;
53  import org.apache.velocity.util.SimplePool;
54  
55  /**
56   * <a href="VelocityPortlet.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Steven P. Goldsmith
60   * @author Raymond Augé
61   *
62   */
63  public class VelocityPortlet extends GenericPortlet {
64  
65      public void init(PortletConfig portletConfig) throws PortletException {
66          super.init(portletConfig);
67  
68          PortletContext portletContext = portletConfig.getPortletContext();
69  
70          _portletContextName = portletContext.getPortletContextName();
71  
72          _actionTemplate = getInitParameter("action-template");
73          _editTemplate = getInitParameter("edit-template");
74          _helpTemplate = getInitParameter("help-template");
75          _resourceTemplate = getInitParameter("resource-template");
76          _viewTemplate = getInitParameter("view-template");
77      }
78  
79      public void processAction(
80              ActionRequest actionRequest, ActionResponse actionResponse)
81          throws PortletException {
82  
83          if (Validator.isNull(_actionTemplate)) {
84              return;
85          }
86  
87          try {
88              Template template = getTemplate(_actionTemplate);
89  
90              mergeTemplate(template, actionRequest, actionResponse);
91          }
92          catch (Exception e) {
93              throw new PortletException(e);
94          }
95      }
96  
97      public void serveResource(
98              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
99          throws PortletException, IOException {
100 
101         if (Validator.isNull(_resourceTemplate)) {
102             super.serveResource(resourceRequest, resourceResponse);
103 
104             return;
105         }
106 
107         try {
108             Template template = getTemplate(_resourceTemplate);
109 
110             mergeTemplate(template, resourceRequest, resourceResponse);
111         }
112         catch (Exception e) {
113             throw new PortletException(e);
114         }
115     }
116 
117     public void doEdit(
118             RenderRequest renderRequest, RenderResponse renderResponse)
119         throws IOException, PortletException {
120 
121         if (renderRequest.getPreferences() == null) {
122             super.doEdit(renderRequest, renderResponse);
123 
124             return;
125         }
126 
127         try {
128             Template template = getTemplate(_editTemplate);
129 
130             mergeTemplate(template, renderRequest, renderResponse);
131         }
132         catch (Exception e) {
133             throw new PortletException(e);
134         }
135     }
136 
137     public void doHelp(
138             RenderRequest renderRequest, RenderResponse renderResponse)
139         throws PortletException {
140 
141         try {
142             Template template = getTemplate(_helpTemplate);
143 
144             mergeTemplate(template, renderRequest, renderResponse);
145         }
146         catch (Exception e) {
147             throw new PortletException(e);
148         }
149     }
150 
151     public void doView(
152             RenderRequest renderRequest, RenderResponse renderResponse)
153         throws PortletException {
154 
155         try {
156             Template template = getTemplate(_viewTemplate);
157 
158             mergeTemplate(template, renderRequest, renderResponse);
159         }
160         catch (Exception e) {
161             throw new PortletException(e);
162         }
163     }
164 
165     protected Context getContext(
166         PortletRequest portletRequest, PortletResponse portletResponse) {
167 
168         Context context = new VelocityContext();
169 
170         context.put("portletConfig", getPortletConfig());
171         context.put("portletContext", getPortletContext());
172         context.put("preferences", portletRequest.getPreferences());
173         context.put(
174             "userInfo", portletRequest.getAttribute(PortletRequest.USER_INFO));
175 
176         context.put("portletRequest", portletRequest);
177 
178         if (portletRequest instanceof ActionRequest) {
179             context.put("actionRequest", portletRequest);
180         }
181         else if (portletRequest instanceof RenderRequest) {
182             context.put("renderRequest", portletRequest);
183         }
184         else {
185             context.put("resourceRequest", portletRequest);
186         }
187 
188         context.put("portletResponse", portletResponse);
189 
190         if (portletResponse instanceof ActionResponse) {
191             context.put("actionResponse", portletResponse);
192         }
193         else if (portletRequest instanceof RenderResponse) {
194             context.put("renderResponse", portletResponse);
195         }
196         else {
197             context.put("resourceResponse", portletResponse);
198         }
199 
200         VelocityVariables.insertHelperUtilities((VelocityContext)context, null);
201 
202         return context;
203     }
204 
205     protected Template getTemplate(String name) throws Exception {
206         return RuntimeSingleton.getTemplate(
207             _portletContextName + VelocityResourceListener.SERVLET_SEPARATOR +
208                 StrutsUtil.TEXT_HTML_DIR + name, StringPool.UTF8);
209     }
210 
211     protected Template getTemplate(String name, String encoding)
212         throws Exception {
213 
214         return RuntimeSingleton.getTemplate(
215             StrutsUtil.TEXT_HTML_DIR + name, encoding);
216     }
217 
218     protected void mergeTemplate(
219             Template template, PortletRequest portletRequest,
220             PortletResponse portletResponse)
221         throws Exception {
222 
223         mergeTemplate(
224             template, getContext(portletRequest, portletResponse),
225             portletRequest, portletResponse);
226     }
227 
228     protected void mergeTemplate(
229             Template template, Context context, PortletRequest portletRequest,
230             PortletResponse portletResponse)
231         throws Exception {
232 
233         if (portletResponse instanceof MimeResponse) {
234             MimeResponse mimeResponse = (MimeResponse)portletResponse;
235 
236             mimeResponse.setContentType(
237                 portletRequest.getResponseContentType());
238         }
239 
240         VelocityWriter velocityWriter = null;
241 
242         try {
243             velocityWriter = (VelocityWriter)_writerPool.get();
244 
245             PrintWriter output = null;
246 
247             if (portletResponse instanceof MimeResponse) {
248                 MimeResponse mimeResponse = (MimeResponse)portletResponse;
249 
250                 output = mimeResponse.getWriter();
251             }
252             else {
253                 output = new PrintWriter(System.out);
254             }
255 
256             if (velocityWriter == null) {
257                 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
258             }
259             else {
260                 velocityWriter.recycle(output);
261             }
262 
263             template.merge(context, velocityWriter);
264         }
265         finally {
266             try {
267                 if (velocityWriter != null) {
268                     velocityWriter.flush();
269                     velocityWriter.recycle(null);
270 
271                     _writerPool.put(velocityWriter);
272                 }
273             }
274             catch (Exception e) {
275             }
276         }
277     }
278 
279     private static SimplePool _writerPool = new SimplePool(40);
280 
281     private String _portletContextName;
282     private String _actionTemplate;
283     private String _editTemplate;
284     private String _helpTemplate;
285     private String _resourceTemplate;
286     private String _viewTemplate;
287 
288 }