1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.kernel.velocity.VelocityContext;
21  import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
22  import com.liferay.portal.struts.StrutsUtil;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.velocity.VelocityResourceListener;
25  
26  import java.io.IOException;
27  import java.io.PrintWriter;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.GenericPortlet;
32  import javax.portlet.MimeResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletContext;
35  import javax.portlet.PortletException;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.PortletResponse;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  import javax.portlet.ResourceRequest;
41  import javax.portlet.ResourceResponse;
42  
43  import org.apache.velocity.io.VelocityWriter;
44  import org.apache.velocity.util.SimplePool;
45  
46  /**
47   * <a href="VelocityPortlet.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Steven P. Goldsmith
51   * @author Raymond Augé
52   */
53  public class VelocityPortlet extends GenericPortlet {
54  
55      public void init(PortletConfig portletConfig) throws PortletException {
56          super.init(portletConfig);
57  
58          PortletContext portletContext = portletConfig.getPortletContext();
59  
60          _portletContextName = portletContext.getPortletContextName();
61  
62          _actionTemplateId = getVelocityTemplateId(
63              getInitParameter("action-template"));
64          _editTemplateId = getVelocityTemplateId(
65              getInitParameter("edit-template"));
66          _helpTemplateId = getVelocityTemplateId(
67              getInitParameter("help-template"));
68          _resourceTemplateId = getVelocityTemplateId(
69              getInitParameter("resource-template"));
70          _viewTemplateId = getVelocityTemplateId(
71              getInitParameter("view-template"));
72      }
73  
74      public void processAction(
75              ActionRequest actionRequest, ActionResponse actionResponse)
76          throws PortletException {
77  
78          if (Validator.isNull(_actionTemplateId)) {
79              return;
80          }
81  
82          try {
83              mergeTemplate(_actionTemplateId, actionRequest, actionResponse);
84          }
85          catch (Exception e) {
86              throw new PortletException(e);
87          }
88      }
89  
90      public void serveResource(
91              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
92          throws PortletException, IOException {
93  
94          if (Validator.isNull(_resourceTemplateId)) {
95              String resourceId = resourceRequest.getResourceID();
96  
97              if (!PortalUtil.isValidResourceId(resourceId)) {
98                  return;
99              }
100 
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         StringBundler sb = new StringBundler(4);
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 UnsyncPrintWriter(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 }