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 portletConfig) throws PortletException {
75 super.init(portletConfig);
76
77 PortletContext portletContext = portletConfig.getPortletContext();
78
79 _portletContextName = portletContext.getPortletContextName();
80
81 _editTemplate = getInitParameter("edit-template");
82 _helpTemplate = getInitParameter("help-template");
83 _viewTemplate = getInitParameter("view-template");
84 }
85
86 public void processAction(
87 ActionRequest actionRequest, ActionResponse actionResponse)
88 throws IOException, PortletException {
89 }
90
91 public void doEdit(
92 RenderRequest renderRequest, RenderResponse renderResponse)
93 throws IOException, PortletException {
94
95 if (renderRequest.getPreferences() == null) {
96 super.doEdit(renderRequest, renderResponse);
97 }
98 else {
99 try {
100 mergeTemplate(
101 getTemplate(_editTemplate), renderRequest, renderResponse);
102 }
103 catch (Exception e) {
104 throw new PortletException(e);
105 }
106 }
107 }
108
109 public void doHelp(
110 RenderRequest renderRequest, RenderResponse renderResponse)
111 throws IOException, PortletException {
112
113 try {
114 mergeTemplate(
115 getTemplate(_helpTemplate), renderRequest, renderResponse);
116 }
117 catch (Exception e) {
118 throw new PortletException(e);
119 }
120 }
121
122 public void doView(
123 RenderRequest renderRequest, RenderResponse renderResponse)
124 throws IOException, PortletException {
125
126 try {
127 mergeTemplate(
128 getTemplate(_viewTemplate), renderRequest, renderResponse);
129 }
130 catch (Exception e) {
131 throw new PortletException(e);
132 }
133 }
134
135 protected Context getContext(
136 PortletRequest portletRequest, PortletResponse portletResponse) {
137
138 Context context = new VelocityContext();
139
140 context.put(REQUEST, portletRequest);
141 context.put(RESPONSE, portletResponse);
142
143 return context;
144 }
145
146 protected Template getTemplate(String name) throws Exception {
147 return RuntimeSingleton.getTemplate(
148 _portletContextName + VelocityResourceListener.SERVLET_SEPARATOR +
149 StrutsUtil.TEXT_HTML_DIR + name, StringPool.UTF8);
150 }
151
152 protected Template getTemplate(String name, String encoding)
153 throws Exception {
154
155 return RuntimeSingleton.getTemplate(
156 StrutsUtil.TEXT_HTML_DIR + name, encoding);
157 }
158
159 protected void mergeTemplate(
160 Template template, RenderRequest renderRequest,
161 RenderResponse renderResponse)
162 throws Exception {
163
164 mergeTemplate(
165 template, getContext(renderRequest, renderResponse), renderRequest,
166 renderResponse);
167 }
168
169 protected void mergeTemplate(
170 Template template, Context context, RenderRequest renderRequest,
171 RenderResponse renderResponse)
172 throws Exception {
173
174 renderResponse.setContentType(renderRequest.getResponseContentType());
175
176 VelocityWriter velocityWriter = null;
177
178 try {
179 velocityWriter = (VelocityWriter)writerPool.get();
180
181 PrintWriter output = renderResponse.getWriter();
182
183 if (velocityWriter == null) {
184 velocityWriter = new VelocityWriter(output, 4 * 1024, true);
185 }
186 else {
187 velocityWriter.recycle(output);
188 }
189
190 template.merge(context, velocityWriter);
191 }
192 finally {
193 try {
194 if (velocityWriter != null) {
195 velocityWriter.flush();
196 velocityWriter.recycle(null);
197
198 writerPool.put(velocityWriter);
199 }
200 }
201 catch (Exception e) {
202 }
203 }
204 }
205
206 private String _portletContextName;
207 private String _editTemplate;
208 private String _helpTemplate;
209 private String _viewTemplate;
210
211 }