1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.bridges.velocity;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortletConfig;
27 import javax.portlet.PortletException;
28 import javax.portlet.PortletMode;
29 import javax.portlet.PortletPreferences;
30 import javax.portlet.PortletRequest;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33 import javax.portlet.WindowState;
34
35 import org.apache.portals.bridges.common.GenericServletPortlet;
36 import org.apache.velocity.VelocityContext;
37 import org.apache.velocity.context.Context;
38
39 /***
40 * <p>
41 * Generic Velocity Portlet emulating basic functionality provided in the
42 * Portlet API (for JSPs) to Velocity portlets and templates. Provides the
43 * following Velocity context variables emulating PLT.22 JSP request variables: *
44 * <ul>
45 * <li>$renderRequest
46 * <li>$renderResponse
47 * <li>$portletConfig
48 * </ul>
49 * </p>
50 * <p>
51 * PLT.22 Tags:
52 * <ul>
53 * <li>$actionURL -- use renderResponse.createActionURL()
54 * <li>$renderURL -- use renderResponse.createRenderURL()
55 * <li>$namespace -- use rennderResponse.getNamespace() (Namespace)
56 * </ul>
57 * Beware that Param tags cannot be added incrementally i.e.
58 * $renderURL.setParameter("name","value").setParameter("name","value") since
59 * the portlet api returns void on setParameter (or setWindowState,
60 * setPortletMode) Thus it is required to set each param or state on a single
61 * line:
62 * </p>
63 * <p>
64 * #set($max = $renderResponse.createRenderURL())
65 * $max.setWindowState($STATE_MAX) $max.setParameter("bush", "war")
66 * </p>
67 * <p>
68 * Constants: $MODE_EDIT, $MODE_HELP, $MODE_VIEW, $STATE_NORMAL, $STATE_MIN,
69 * $STATE_MAX, $USER_INFO
70 *
71 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
72 * @version $Id: GenericVelocityPortlet.java,v 1.1 2004/10/29 01:29:50 taylor
73 * Exp $
74 */
75 public class GenericVelocityPortlet extends GenericServletPortlet
76 {
77
78 public final static String PORTLET_BRIDGE_CONTEXT = "portals.bridges.velocity.context";
79
80 public GenericVelocityPortlet()
81 {
82 }
83
84 public void init(PortletConfig config) throws PortletException
85 {
86 super.init(config);
87 }
88
89 /***
90 * Execute the servlet as define by the init parameter or preference
91 * PARAM_ACTION_PAGE. The value if the parameter is a relative URL, i.e.
92 * /actionPage.jsp will execute the JSP editPage.jsp in the portlet
93 * application's web app. The action should not generate any content. The
94 * content will be generate by doCustom(), doHelp() , doEdit(), or doView().
95 *
96 * See section PLT.16.2 of the JSR 168 Portlet Spec for more information
97 * around executing a servlet or JSP in processAction()
98 *
99 * @see javax.portlet.GenericPortlet#processAction
100 *
101 * @task Need to be able to execute a servlet for the action
102 */
103 public void processAction(ActionRequest request, ActionResponse actionResponse) throws PortletException,
104 IOException
105 {
106 super.processAction(request, actionResponse);
107 }
108
109 /***
110 * Execute the servlet as define by the init parameter or preference
111 * PARAM_EDIT_PAGE. The value if the parameter is a relative URL, i.e.
112 * /editPage.jsp will execute the JSP editPage.jsp in the portlet
113 * application's web app.
114 *
115 * @see javax.portlet.GenericPortlet#doCustom
116 */
117 public void doCustom(RenderRequest request, RenderResponse response) throws PortletException, IOException
118 {
119 super.doCustom(request, response);
120 }
121
122 /***
123 * Execute the servlet as define by the init parameter or preference
124 * PARAM_EDIT_PAGE. The value if the parameter is a relative URL, i.e.
125 * /editPage.jsp will execute the JSP editPage.jsp in the portlet
126 * application's web app.
127 *
128 * @see javax.portlet.GenericPortlet#doEdit
129 */
130 public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
131 {
132 super.doEdit(request, response);
133 }
134
135 /***
136 * Execute the servlet as define by the init parameter or preference
137 * PARAM_HELP_PAGE. The value if the parameter is a relative URL, i.e.
138 * /helpPage.jsp will exeute the JSP helpPage.jsp in the portlet
139 * application's web app.
140 *
141 * @see javax.portlet.GenericPortlet#doView
142 */
143 public void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException
144 {
145 super.doHelp(request, response);
146 }
147
148 /***
149 * Execute the servlet as define by the init parameter or preference
150 * PARAM_VIEW_PAGE. The value if the parameter is a relative URL, i.e.
151 * /viewPage.jsp will execute the JSP viewPage.jsp in the portlet
152 * application's web app.
153 *
154 * @see javax.portlet.GenericPortlet#doView
155 */
156 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
157 {
158 super.doView(request, response);
159 }
160
161 public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException
162 {
163 createPortletVelocityContext(request, response);
164 super.render(request, response);
165 }
166
167 private Context createPortletVelocityContext(RenderRequest request, RenderResponse response)
168 {
169 Context ctx = new VelocityContext();
170 request.setAttribute(PORTLET_BRIDGE_CONTEXT, ctx);
171
172 ctx.put("renderRequest", request);
173 ctx.put("renderResponse", response);
174 ctx.put("portletConfig", getPortletConfig());
175
176 ctx.put("STATE_NORMAL", WindowState.NORMAL);
177 ctx.put("STATE_MAX", WindowState.MAXIMIZED);
178 ctx.put("STATE_MIN", WindowState.MINIMIZED);
179 ctx.put("MODE_VIEW", PortletMode.VIEW);
180 ctx.put("MODE_EDIT", PortletMode.EDIT);
181 ctx.put("MODE_HELP", PortletMode.HELP);
182 ctx.put("USER_INFO", PortletRequest.USER_INFO);
183 return ctx;
184 }
185
186 public Context getContext(RenderRequest request)
187 {
188 return (Context) request.getAttribute(PORTLET_BRIDGE_CONTEXT);
189 }
190
191 public Context getContext(RenderRequest request, RenderResponse response)
192 {
193 Context context = (Context) request.getAttribute(PORTLET_BRIDGE_CONTEXT);
194
195 if (context == null)
196 {
197 context = createPortletVelocityContext(request, response);
198 }
199
200 return context;
201 }
202
203 public void setupPreferencesEdit(RenderRequest request, RenderResponse response)
204 {
205 Context context = getContext(request, response);
206 PortletPreferences prefs = request.getPreferences();
207 Map map = prefs.getMap();
208 Iterator it = map.entrySet().iterator();
209 context.put("prefs", it);
210
211 Map result = new HashMap(map.size());
212 Iterator f = map.entrySet().iterator();
213 while(f.hasNext())
214 {
215 Map.Entry e = (Map.Entry)f.next();
216 String []why = (String[])e.getValue();
217 if (why == null || why[0] == null)
218 {
219 result.put(e.getKey(), "");
220 }
221 else
222 {
223 result.put(e.getKey(), why[0]);
224 }
225 }
226 context.put("prefsMap", result);
227 }
228
229 public void doPreferencesEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
230 {
231 setupPreferencesEdit(request, response);
232 super.doEdit(request, response);
233 }
234
235 }