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