001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 doEdit(
054                            RenderRequest renderRequest, RenderResponse renderResponse)
055                    throws IOException, PortletException {
056    
057                    if (renderRequest.getPreferences() == null) {
058                            super.doEdit(renderRequest, renderResponse);
059    
060                            return;
061                    }
062    
063                    try {
064                            mergeTemplate(_editTemplateId, renderRequest, renderResponse);
065                    }
066                    catch (Exception e) {
067                            throw new PortletException(e);
068                    }
069            }
070    
071            @Override
072            public void doHelp(
073                            RenderRequest renderRequest, RenderResponse renderResponse)
074                    throws PortletException {
075    
076                    try {
077                            mergeTemplate(_helpTemplateId, renderRequest, renderResponse);
078                    }
079                    catch (Exception e) {
080                            throw new PortletException(e);
081                    }
082            }
083    
084            @Override
085            public void doView(
086                            RenderRequest renderRequest, RenderResponse renderResponse)
087                    throws PortletException {
088    
089                    try {
090                            mergeTemplate(_viewTemplateId, renderRequest, renderResponse);
091                    }
092                    catch (Exception e) {
093                            throw new PortletException(e);
094                    }
095            }
096    
097            @Override
098            public void init(PortletConfig portletConfig) throws PortletException {
099                    super.init(portletConfig);
100    
101                    PortletContext portletContext = portletConfig.getPortletContext();
102    
103                    _portletContextName = portletContext.getPortletContextName();
104    
105                    _actionTemplateId = getVelocityTemplateId(
106                            getInitParameter("action-template"));
107                    _editTemplateId = getVelocityTemplateId(
108                            getInitParameter("edit-template"));
109                    _helpTemplateId = getVelocityTemplateId(
110                            getInitParameter("help-template"));
111                    _resourceTemplateId = getVelocityTemplateId(
112                            getInitParameter("resource-template"));
113                    _viewTemplateId = getVelocityTemplateId(
114                            getInitParameter("view-template"));
115            }
116    
117            @Override
118            public void processAction(
119                            ActionRequest actionRequest, ActionResponse actionResponse)
120                    throws PortletException {
121    
122                    if (Validator.isNull(_actionTemplateId)) {
123                            return;
124                    }
125    
126                    try {
127                            mergeTemplate(_actionTemplateId, actionRequest, actionResponse);
128                    }
129                    catch (Exception e) {
130                            throw new PortletException(e);
131                    }
132            }
133    
134            @Override
135            public void serveResource(
136                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
137                    throws IOException, PortletException {
138    
139                    if (Validator.isNull(_resourceTemplateId)) {
140                            super.serveResource(resourceRequest, resourceResponse);
141    
142                            return;
143                    }
144    
145                    try {
146                            mergeTemplate(
147                                    _resourceTemplateId, resourceRequest, resourceResponse);
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), portletRequest,
216                            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 _actionTemplateId;
274            private String _editTemplateId;
275            private String _helpTemplateId;
276            private String _portletContextName;
277            private String _resourceTemplateId;
278            private String _viewTemplateId;
279    
280    }