001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.velocity.VelocityContext;
021    import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
022    import com.liferay.portal.struts.StrutsUtil;
023    import com.liferay.portal.velocity.VelocityResourceListener;
024    
025    import java.io.IOException;
026    import java.io.PrintWriter;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.GenericPortlet;
031    import javax.portlet.MimeResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletContext;
034    import javax.portlet.PortletException;
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletResponse;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    import javax.portlet.ResourceRequest;
040    import javax.portlet.ResourceResponse;
041    
042    import org.apache.velocity.io.VelocityWriter;
043    import org.apache.velocity.util.SimplePool;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Steven P. Goldsmith
048     * @author Raymond Augé
049     */
050    public class VelocityPortlet extends GenericPortlet {
051    
052            @Override
053            public void init(PortletConfig portletConfig) throws PortletException {
054                    super.init(portletConfig);
055    
056                    PortletContext portletContext = portletConfig.getPortletContext();
057    
058                    _portletContextName = portletContext.getPortletContextName();
059    
060                    _actionTemplateId = getVelocityTemplateId(
061                            getInitParameter("action-template"));
062                    _editTemplateId = getVelocityTemplateId(
063                            getInitParameter("edit-template"));
064                    _helpTemplateId = getVelocityTemplateId(
065                            getInitParameter("help-template"));
066                    _resourceTemplateId = getVelocityTemplateId(
067                            getInitParameter("resource-template"));
068                    _viewTemplateId = getVelocityTemplateId(
069                            getInitParameter("view-template"));
070            }
071    
072            @Override
073            public void processAction(
074                            ActionRequest actionRequest, ActionResponse actionResponse)
075                    throws PortletException {
076    
077                    if (Validator.isNull(_actionTemplateId)) {
078                            return;
079                    }
080    
081                    try {
082                            mergeTemplate(_actionTemplateId, actionRequest, actionResponse);
083                    }
084                    catch (Exception e) {
085                            throw new PortletException(e);
086                    }
087            }
088    
089            @Override
090            public void serveResource(
091                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
092                    throws PortletException, IOException {
093    
094                    if (Validator.isNull(_resourceTemplateId)) {
095                            super.serveResource(resourceRequest, resourceResponse);
096    
097                            return;
098                    }
099    
100                    try {
101                            mergeTemplate(
102                                    _resourceTemplateId, resourceRequest, resourceResponse);
103                    }
104                    catch (Exception e) {
105                            throw new PortletException(e);
106                    }
107            }
108    
109            @Override
110            public void doEdit(
111                            RenderRequest renderRequest, RenderResponse renderResponse)
112                    throws IOException, PortletException {
113    
114                    if (renderRequest.getPreferences() == null) {
115                            super.doEdit(renderRequest, renderResponse);
116    
117                            return;
118                    }
119    
120                    try {
121                            mergeTemplate(_editTemplateId, renderRequest, renderResponse);
122                    }
123                    catch (Exception e) {
124                            throw new PortletException(e);
125                    }
126            }
127    
128            @Override
129            public void doHelp(
130                            RenderRequest renderRequest, RenderResponse renderResponse)
131                    throws PortletException {
132    
133                    try {
134                            mergeTemplate(_helpTemplateId, renderRequest, renderResponse);
135                    }
136                    catch (Exception e) {
137                            throw new PortletException(e);
138                    }
139            }
140    
141            @Override
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                    StringBundler sb = new StringBundler(4);
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 = UnsyncPrintWriterPool.borrow(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    }