1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class VelocityPortlet extends GenericPortlet {
59  
60      public void init(PortletConfig portletConfig) throws PortletException {
61          super.init(portletConfig);
62  
63          PortletContext portletContext = portletConfig.getPortletContext();
64  
65          _portletContextName = portletContext.getPortletContextName();
66  
67          _actionTemplateId = getVelocityTemplateId(
68              getInitParameter("action-template"));
69          _editTemplateId = getVelocityTemplateId(
70              getInitParameter("edit-template"));
71          _helpTemplateId = getVelocityTemplateId(
72              getInitParameter("help-template"));
73          _resourceTemplateId = getVelocityTemplateId(
74              getInitParameter("resource-template"));
75          _viewTemplateId = getVelocityTemplateId(
76              getInitParameter("view-template"));
77      }
78  
79      public void processAction(
80              ActionRequest actionRequest, ActionResponse actionResponse)
81          throws PortletException {
82  
83          if (Validator.isNull(_actionTemplateId)) {
84              return;
85          }
86  
87          try {
88              mergeTemplate(_actionTemplateId, actionRequest, actionResponse);
89          }
90          catch (Exception e) {
91              throw new PortletException(e);
92          }
93      }
94  
95      public void serveResource(
96              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
97          throws PortletException, IOException {
98  
99          if (Validator.isNull(_resourceTemplateId)) {
100             super.serveResource(resourceRequest, resourceResponse);
101 
102             return;
103         }
104 
105         try {
106             mergeTemplate(
107                 _resourceTemplateId, resourceRequest, resourceResponse);
108         }
109         catch (Exception e) {
110             throw new PortletException(e);
111         }
112     }
113 
114     public void doEdit(
115             RenderRequest renderRequest, RenderResponse renderResponse)
116         throws IOException, PortletException {
117 
118         if (renderRequest.getPreferences() == null) {
119             super.doEdit(renderRequest, renderResponse);
120 
121             return;
122         }
123 
124         try {
125             mergeTemplate(_editTemplateId, renderRequest, renderResponse);
126         }
127         catch (Exception e) {
128             throw new PortletException(e);
129         }
130     }
131 
132     public void doHelp(
133             RenderRequest renderRequest, RenderResponse renderResponse)
134         throws PortletException {
135 
136         try {
137             mergeTemplate(_helpTemplateId, renderRequest, renderResponse);
138         }
139         catch (Exception e) {
140             throw new PortletException(e);
141         }
142     }
143 
144     public void doView(
145             RenderRequest renderRequest, RenderResponse renderResponse)
146         throws PortletException {
147 
148         try {
149             mergeTemplate(_viewTemplateId, renderRequest, renderResponse);
150         }
151         catch (Exception e) {
152             throw new PortletException(e);
153         }
154     }
155 
156     protected VelocityContext getVelocityContext(
157         PortletRequest portletRequest, PortletResponse portletResponse) {
158 
159         VelocityContext velocityContext =
160             VelocityEngineUtil.getWrappedStandardToolsContext();
161 
162         velocityContext.put("portletConfig", getPortletConfig());
163         velocityContext.put("portletContext", getPortletContext());
164         velocityContext.put("preferences", portletRequest.getPreferences());
165         velocityContext.put(
166             "userInfo", portletRequest.getAttribute(PortletRequest.USER_INFO));
167 
168         velocityContext.put("portletRequest", portletRequest);
169 
170         if (portletRequest instanceof ActionRequest) {
171             velocityContext.put("actionRequest", portletRequest);
172         }
173         else if (portletRequest instanceof RenderRequest) {
174             velocityContext.put("renderRequest", portletRequest);
175         }
176         else {
177             velocityContext.put("resourceRequest", portletRequest);
178         }
179 
180         velocityContext.put("portletResponse", portletResponse);
181 
182         if (portletResponse instanceof ActionResponse) {
183             velocityContext.put("actionResponse", portletResponse);
184         }
185         else if (portletRequest instanceof RenderResponse) {
186             velocityContext.put("renderResponse", portletResponse);
187         }
188         else {
189             velocityContext.put("resourceResponse", portletResponse);
190         }
191 
192         return velocityContext;
193     }
194 
195     protected String getVelocityTemplateId(String name) {
196         if (Validator.isNull(name)) {
197             return name;
198         }
199 
200         StringBuilder sb = new StringBuilder();
201 
202         sb.append(_portletContextName);
203         sb.append(VelocityResourceListener.SERVLET_SEPARATOR);
204         sb.append(StrutsUtil.TEXT_HTML_DIR);
205         sb.append(name);
206 
207         return sb.toString();
208     }
209 
210     protected void mergeTemplate(
211             String velocityTemplateId, PortletRequest portletRequest,
212             PortletResponse portletResponse)
213         throws Exception {
214 
215         mergeTemplate(
216             velocityTemplateId,
217             getVelocityContext(portletRequest, portletResponse),
218             portletRequest, portletResponse);
219     }
220 
221     protected void mergeTemplate(
222             String velocityTemplateId, VelocityContext velocityContext,
223             PortletRequest portletRequest, PortletResponse portletResponse)
224         throws Exception {
225 
226         if (portletResponse instanceof MimeResponse) {
227             MimeResponse mimeResponse = (MimeResponse)portletResponse;
228 
229             mimeResponse.setContentType(
230                 portletRequest.getResponseContentType());
231         }
232 
233         VelocityWriter velocityWriter = null;
234 
235         try {
236             velocityWriter = (VelocityWriter)_writerPool.get();
237 
238             PrintWriter output = null;
239 
240             if (portletResponse instanceof MimeResponse) {
241                 MimeResponse mimeResponse = (MimeResponse)portletResponse;
242 
243                 output = mimeResponse.getWriter();
244             }
245             else {
246                 output = new PrintWriter(System.out);
247             }
248 
249             if (velocityWriter == null) {
250                 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
251             }
252             else {
253                 velocityWriter.recycle(output);
254             }
255 
256             VelocityEngineUtil.mergeTemplate(
257                 velocityTemplateId, null, velocityContext, velocityWriter);
258         }
259         finally {
260             try {
261                 if (velocityWriter != null) {
262                     velocityWriter.flush();
263                     velocityWriter.recycle(null);
264 
265                     _writerPool.put(velocityWriter);
266                 }
267             }
268             catch (Exception e) {
269             }
270         }
271     }
272 
273     private static SimplePool _writerPool = new SimplePool(40);
274 
275     private String _portletContextName;
276     private String _actionTemplateId;
277     private String _editTemplateId;
278     private String _helpTemplateId;
279     private String _resourceTemplateId;
280     private String _viewTemplateId;
281 
282 }