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