1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.struts.StrutsUtil;
27 import com.liferay.portal.velocity.VelocityResourceListener;
28
29 import java.io.IOException;
30 import java.io.PrintWriter;
31
32 import javax.portlet.ActionRequest;
33 import javax.portlet.ActionResponse;
34 import javax.portlet.GenericPortlet;
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
43 import org.apache.velocity.Template;
44 import org.apache.velocity.VelocityContext;
45 import org.apache.velocity.context.Context;
46 import org.apache.velocity.io.VelocityWriter;
47 import org.apache.velocity.runtime.RuntimeSingleton;
48 import org.apache.velocity.util.SimplePool;
49
50
57 public class VelocityPortlet extends GenericPortlet {
58
59
62 public static final String REQUEST = "VelocityPortlet.portletRequest";
63
64
67 public static final String RESPONSE = "VelocityPortlet.portletResponse";
68
69
72 private static SimplePool writerPool = new SimplePool(40);
73
74 public void init(PortletConfig config) throws PortletException {
75 super.init(config);
76
77 PortletContext portletCtx = config.getPortletContext();
78
79 _portletContextName = portletCtx.getPortletContextName();
80
81 _editTemplate = getInitParameter("edit-template");
82 _helpTemplate = getInitParameter("help-template");
83 _viewTemplate = getInitParameter("view-template");
84 }
85
86 public void processAction(ActionRequest req, ActionResponse res)
87 throws IOException, PortletException {
88 }
89
90 public void doEdit(RenderRequest req, RenderResponse res)
91 throws IOException, PortletException {
92
93 if (req.getPreferences() == null) {
94 super.doEdit(req, res);
95 }
96 else {
97 try {
98 mergeTemplate(getTemplate(_editTemplate), req, res);
99 }
100 catch (Exception e) {
101 throw new PortletException(e);
102 }
103 }
104 }
105
106 public void doHelp(RenderRequest req, RenderResponse res)
107 throws IOException, PortletException {
108
109 try {
110 mergeTemplate(getTemplate(_helpTemplate), req, res);
111 }
112 catch (Exception e) {
113 throw new PortletException(e);
114 }
115 }
116
117 public void doView(RenderRequest req, RenderResponse res)
118 throws IOException, PortletException {
119
120 try {
121 mergeTemplate(getTemplate(_viewTemplate), req, res);
122 }
123 catch (Exception e) {
124 throw new PortletException(e);
125 }
126 }
127
128 protected Context getContext(PortletRequest req, PortletResponse res) {
129 Context context = new VelocityContext();
130
131 context.put(REQUEST, req);
132 context.put(RESPONSE, res);
133
134 return context;
135 }
136
137 protected Template getTemplate(String name) throws Exception {
138 return RuntimeSingleton.getTemplate(
139 _portletContextName + VelocityResourceListener.SERVLET_SEPARATOR +
140 StrutsUtil.TEXT_HTML_DIR + name, StringPool.UTF8);
141 }
142
143 protected Template getTemplate(String name, String encoding)
144 throws Exception {
145
146 return RuntimeSingleton.getTemplate(
147 StrutsUtil.TEXT_HTML_DIR + name, encoding);
148 }
149
150 protected void mergeTemplate(
151 Template template, RenderRequest req, RenderResponse res)
152 throws Exception {
153
154 mergeTemplate(template, getContext(req, res), req, res);
155 }
156
157 protected void mergeTemplate(
158 Template template, Context context, RenderRequest req,
159 RenderResponse res)
160 throws Exception {
161
162 res.setContentType(req.getResponseContentType());
163
164 VelocityWriter velocityWriter = null;
165
166 try {
167 velocityWriter = (VelocityWriter)writerPool.get();
168
169 PrintWriter output = res.getWriter();
170
171 if (velocityWriter == null) {
172 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
173 }
174 else {
175 velocityWriter.recycle(output);
176 }
177
178 template.merge(context, velocityWriter);
179 }
180 finally {
181 try {
182 if (velocityWriter != null) {
183 velocityWriter.flush();
184 velocityWriter.recycle(null);
185
186 writerPool.put(velocityWriter);
187 }
188 }
189 catch (Exception e) {
190 }
191 }
192 }
193
194 private String _portletContextName;
195 private String _editTemplate;
196 private String _helpTemplate;
197 private String _viewTemplate;
198
199 }