001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.struts.StrutsUtil;
025: import com.liferay.portal.velocity.VelocityResourceListener;
026:
027: import java.io.IOException;
028: import java.io.PrintWriter;
029:
030: import javax.portlet.ActionRequest;
031: import javax.portlet.ActionResponse;
032: import javax.portlet.GenericPortlet;
033: import javax.portlet.PortletConfig;
034: import javax.portlet.PortletContext;
035: import javax.portlet.PortletException;
036: import javax.portlet.PortletRequest;
037: import javax.portlet.PortletResponse;
038: import javax.portlet.RenderRequest;
039: import javax.portlet.RenderResponse;
040:
041: import org.apache.velocity.Template;
042: import org.apache.velocity.VelocityContext;
043: import org.apache.velocity.context.Context;
044: import org.apache.velocity.io.VelocityWriter;
045: import org.apache.velocity.runtime.RuntimeSingleton;
046: import org.apache.velocity.util.SimplePool;
047:
048: /**
049: * <a href="VelocityPortlet.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: * @author Steven P. Goldsmith
053: *
054: */
055: public class VelocityPortlet extends GenericPortlet {
056:
057: /**
058: * The context key for the portlet request.
059: */
060: public static final String REQUEST = "VelocityPortlet.portletRequest";
061:
062: /**
063: * The context key for the portlet response.
064: */
065: public static final String RESPONSE = "VelocityPortlet.portletResponse";
066:
067: /**
068: * Cache of writers.
069: */
070: private static SimplePool writerPool = new SimplePool(40);
071:
072: public void init(PortletConfig config) throws PortletException {
073: super .init(config);
074:
075: PortletContext portletCtx = config.getPortletContext();
076:
077: _portletContextName = portletCtx.getPortletContextName();
078:
079: _editTemplate = getInitParameter("edit-template");
080: _helpTemplate = getInitParameter("help-template");
081: _viewTemplate = getInitParameter("view-template");
082: }
083:
084: public void processAction(ActionRequest req, ActionResponse res)
085: throws IOException, PortletException {
086: }
087:
088: public void doEdit(RenderRequest req, RenderResponse res)
089: throws IOException, PortletException {
090:
091: if (req.getPreferences() == null) {
092: super .doEdit(req, res);
093: } else {
094: try {
095: mergeTemplate(getTemplate(_editTemplate), req, res);
096: } catch (Exception e) {
097: throw new PortletException(e);
098: }
099: }
100: }
101:
102: public void doHelp(RenderRequest req, RenderResponse res)
103: throws IOException, PortletException {
104:
105: try {
106: mergeTemplate(getTemplate(_helpTemplate), req, res);
107: } catch (Exception e) {
108: throw new PortletException(e);
109: }
110: }
111:
112: public void doView(RenderRequest req, RenderResponse res)
113: throws IOException, PortletException {
114:
115: try {
116: mergeTemplate(getTemplate(_viewTemplate), req, res);
117: } catch (Exception e) {
118: throw new PortletException(e);
119: }
120: }
121:
122: protected Context getContext(PortletRequest req, PortletResponse res) {
123: Context context = new VelocityContext();
124:
125: context.put(REQUEST, req);
126: context.put(RESPONSE, res);
127:
128: return context;
129: }
130:
131: protected Template getTemplate(String name) throws Exception {
132: return RuntimeSingleton.getTemplate(_portletContextName
133: + VelocityResourceListener.SERVLET_SEPARATOR
134: + StrutsUtil.TEXT_HTML_DIR + name, StringPool.UTF8);
135: }
136:
137: protected Template getTemplate(String name, String encoding)
138: throws Exception {
139:
140: return RuntimeSingleton.getTemplate(StrutsUtil.TEXT_HTML_DIR
141: + name, encoding);
142: }
143:
144: protected void mergeTemplate(Template template, RenderRequest req,
145: RenderResponse res) throws Exception {
146:
147: mergeTemplate(template, getContext(req, res), req, res);
148: }
149:
150: protected void mergeTemplate(Template template, Context context,
151: RenderRequest req, RenderResponse res) throws Exception {
152:
153: res.setContentType(req.getResponseContentType());
154:
155: VelocityWriter velocityWriter = null;
156:
157: try {
158: velocityWriter = (VelocityWriter) writerPool.get();
159:
160: PrintWriter output = res.getWriter();
161:
162: if (velocityWriter == null) {
163: velocityWriter = new VelocityWriter(output, 4 * 1024,
164: true);
165: } else {
166: velocityWriter.recycle(output);
167: }
168:
169: template.merge(context, velocityWriter);
170: } finally {
171: try {
172: if (velocityWriter != null) {
173: velocityWriter.flush();
174: velocityWriter.recycle(null);
175:
176: writerPool.put(velocityWriter);
177: }
178: } catch (Exception e) {
179: }
180: }
181: }
182:
183: private String _portletContextName;
184: private String _editTemplate;
185: private String _helpTemplate;
186: private String _viewTemplate;
187:
188: }
|