1   /**
2    * Copyright (c) 2000-2007 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.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.PortletConfig;
35  import javax.portlet.PortletContext;
36  import javax.portlet.PortletException;
37  import javax.portlet.PortletRequest;
38  import javax.portlet.PortletResponse;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.velocity.Template;
43  import org.apache.velocity.VelocityContext;
44  import org.apache.velocity.context.Context;
45  import org.apache.velocity.io.VelocityWriter;
46  import org.apache.velocity.runtime.RuntimeSingleton;
47  import org.apache.velocity.util.SimplePool;
48  
49  /**
50   * <a href="VelocityPortlet.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Steven P. Goldsmith
54   *
55   */
56  public class VelocityPortlet extends GenericPortlet {
57  
58      /**
59       * The context key for the portlet request.
60       */
61      public static final String REQUEST = "VelocityPortlet.portletRequest";
62  
63      /**
64       * The context key for the portlet response.
65       */
66      public static final String RESPONSE = "VelocityPortlet.portletResponse";
67  
68      /**
69       * Cache of writers.
70       */
71      private static SimplePool writerPool = new SimplePool(40);
72  
73      public void init(PortletConfig config) throws PortletException {
74          super.init(config);
75  
76          PortletContext portletCtx = config.getPortletContext();
77  
78          _portletContextName = portletCtx.getPortletContextName();
79  
80          _editTemplate = getInitParameter("edit-template");
81          _helpTemplate = getInitParameter("help-template");
82          _viewTemplate = getInitParameter("view-template");
83      }
84  
85      public void processAction(ActionRequest req, ActionResponse res)
86          throws IOException, PortletException {
87      }
88  
89      public void doEdit(RenderRequest req, RenderResponse res)
90          throws IOException, PortletException {
91  
92          if (req.getPreferences() == null) {
93              super.doEdit(req, res);
94          }
95          else {
96              try {
97                  mergeTemplate(getTemplate(_editTemplate), req, res);
98              }
99              catch (Exception e) {
100                 throw new PortletException(e);
101             }
102         }
103     }
104 
105     public void doHelp(RenderRequest req, RenderResponse res)
106         throws IOException, PortletException {
107 
108         try {
109             mergeTemplate(getTemplate(_helpTemplate), req, res);
110         }
111         catch (Exception e) {
112             throw new PortletException(e);
113         }
114     }
115 
116     public void doView(RenderRequest req, RenderResponse res)
117         throws IOException, PortletException {
118 
119         try {
120             mergeTemplate(getTemplate(_viewTemplate), req, res);
121         }
122         catch (Exception e) {
123             throw new PortletException(e);
124         }
125     }
126 
127     protected Context getContext(PortletRequest req, PortletResponse res) {
128         Context context = new VelocityContext();
129 
130         context.put(REQUEST, req);
131         context.put(RESPONSE, res);
132 
133         return context;
134     }
135 
136     protected Template getTemplate(String name) throws Exception {
137         return RuntimeSingleton.getTemplate(
138             _portletContextName + VelocityResourceListener.SERVLET_SEPARATOR +
139                 StrutsUtil.TEXT_HTML_DIR + name);
140     }
141 
142     protected Template getTemplate(String name, String encoding)
143         throws Exception {
144 
145         return RuntimeSingleton.getTemplate(
146             StrutsUtil.TEXT_HTML_DIR + name, encoding);
147     }
148 
149     protected void mergeTemplate(
150             Template template, RenderRequest req, RenderResponse res)
151         throws Exception {
152 
153         mergeTemplate(template, getContext(req, res), req, res);
154     }
155 
156     protected void mergeTemplate(
157             Template template, Context context, RenderRequest req,
158             RenderResponse res)
159         throws Exception {
160 
161         res.setContentType(req.getResponseContentType());
162 
163         VelocityWriter velocityWriter = null;
164 
165         try {
166             velocityWriter = (VelocityWriter)writerPool.get();
167 
168             PrintWriter output = res.getWriter();
169 
170             if (velocityWriter == null) {
171                 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
172             }
173             else {
174                 velocityWriter.recycle(output);
175             }
176 
177             template.merge(context, velocityWriter);
178         }
179         finally {
180             try {
181                 if (velocityWriter != null) {
182                     velocityWriter.flush();
183                     velocityWriter.recycle(null);
184 
185                     writerPool.put(velocityWriter);
186                 }
187             }
188             catch (Exception e) {
189             }
190         }
191     }
192 
193     private String _portletContextName;
194     private String _editTemplate;
195     private String _helpTemplate;
196     private String _viewTemplate;
197 
198 }