KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > WebWorkPortlet


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

4 package com.opensymphony.webwork.portlet;
5
6 import com.opensymphony.module.sitemesh.util.Container;
7 import com.opensymphony.util.FileManager;
8 import com.opensymphony.webwork.ServletActionContext;
9 import com.opensymphony.webwork.config.Configuration;
10 import com.opensymphony.webwork.portlet.context.PortletContext;
11 import com.opensymphony.webwork.portlet.util.PortletMessaging;
12 import com.opensymphony.webwork.util.AttributeMap;
13 import com.opensymphony.xwork.ActionContext;
14 import com.opensymphony.xwork.ActionGlobalContext;
15 import com.opensymphony.xwork.ActionProxy;
16 import com.opensymphony.xwork.ActionProxyFactory;
17 import com.opensymphony.xwork.interceptor.component.ComponentInterceptor;
18 import com.opensymphony.xwork.util.OgnlValueStack;
19 import org.apache.pluto.core.impl.PortletConfigImpl;
20
21 import javax.portlet.*;
22 import javax.servlet.ServletContext JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:hu_pengfei@yahoo.com.cn">Henry Hu </a>
30  * @since 2005-7-18
31  */

32 public class WebWorkPortlet extends GenericPortlet implements WebWorkPortletStatics {
33
34     private static final String JavaDoc NAME_RESULT = "result";
35
36     private static final String JavaDoc NAME_ACTION = "wwAction";
37
38     private static final String JavaDoc NAME_XACTION = "wwXAction";
39
40     private static final String JavaDoc NAME_LINK = "wwLink";
41
42     private static final String JavaDoc NAME_DEFAULT_VIEW_FILE = "defaultViewFile";
43
44     private static final String JavaDoc NAME_DEFAULT_EDIT_FILE = "defaultEditFile";
45
46     private static final String JavaDoc NAME_DEFAULT_HELP_FILE = "defaultHelpFile";
47
48     private String JavaDoc helpFileName = "";
49
50     private String JavaDoc mockHelpFileName = "";
51
52     private Map cheatRequestMap = null;
53
54     public void init() throws PortletException {
55         if ("true".equalsIgnoreCase(Configuration.getString("webwork.configuration.xml.reload"))) {
56             FileManager.setReloadingConfigs(true);
57         }
58
59         helpFileName = getInitParameter(NAME_DEFAULT_HELP_FILE);
60
61         /*
62          * Add JSP support, change the dispatch name from *.jsp to *.jspt, so
63          * our WebWorkJSPServlvet can catch this request.
64          */

65         int jspIndex = helpFileName.lastIndexOf(".jsp");
66         if (jspIndex >= 0) {
67             mockHelpFileName = helpFileName.substring(0, jspIndex) + ".jspt";
68         } else {
69             int index = helpFileName.lastIndexOf(".");
70             mockHelpFileName = helpFileName.substring(0, index) + ".vm";
71         }
72
73     }
74
75     protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
76
77         ActionContext.getContext().put("template", helpFileName);
78         PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(mockHelpFileName);
79         try {
80             rd.include(request, response);
81         } catch (PortletException e) {
82             e.printStackTrace();
83         } catch (IOException JavaDoc e) {
84             e.printStackTrace();
85         }
86
87     }
88
89     protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
90         doService(request, response, NAME_DEFAULT_EDIT_FILE);
91     }
92
93     protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc {
94         doService(request, response, NAME_DEFAULT_VIEW_FILE);
95     }
96
97     private void doService(RenderRequest request, RenderResponse response, String JavaDoc defalutFile) throws IOException JavaDoc, PortletException {
98
99         try {
100
101             response.setContentType("text/html");
102
103             PortletContext portletContext = (PortletContext) PortletMessaging.receive(request, "PortletContext");
104             PortletContext.setContext(portletContext);
105
106             ActionContext actionContext = (ActionContext) PortletMessaging.receive(request, "ActionContext");
107             ActionContext.setContext(actionContext);
108
109             PortletURL portletURL = response.createActionURL();
110             String JavaDoc actionURL = portletURL.toString();
111             PortletContext.getContext().setActionURL(actionURL);
112
113             String JavaDoc template = (String JavaDoc) request.getPortletSession().getAttribute(RENDER_TEMPLATE);
114             if (template == null || "".equals(template)) {
115                 template = getInitParameter(defalutFile);
116             }
117
118             if (!template.startsWith("/"))
119                 template = "/" + template;
120
121             /*
122              * Store the template fileName in the thread, this will be retrieved
123              * in WebWorkVelocityServlet for the real rendering.
124              */

125             ActionContext.getContext().put("template", template);
126
127             /*
128              * Add JSP support, change the dispatch name from *.jsp to *.jspt,
129              * so our WebWorkJSPServlvet can catch this request.
130              */

131             int jspIndex = template.lastIndexOf(".jsp");
132             String JavaDoc dispatchTemplate = template;
133             if (jspIndex >= 0)
134                 dispatchTemplate = template.substring(0, jspIndex) + ".jspt";
135
136             /*
137              * Must put the stack into HttpServletRequest, because the WebWork
138              * JSP Taglib will use it to judge whether there are some errors in
139              * stack.
140              */

141             OgnlValueStack stack = ActionContext.getContext().getValueStack();
142             request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
143
144             PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(dispatchTemplate);
145             rd.include(request, response);
146
147             ActionContext.setContext(null);
148
149         } catch (Exception JavaDoc ex) {
150             ex.printStackTrace();
151         }
152     }
153
154     public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException JavaDoc {
155
156         try {
157
158             ///////////////////////////For OpenSessionInXWorkInterceptor in
159
// WebWork Interceptor/////////////////////
160
if (Container.get() == Container.TOMCAT) {
161                 javax.servlet.http.HttpServletRequestWrapper JavaDoc requestWrapper = (javax.servlet.http.HttpServletRequestWrapper JavaDoc) request;
162                 ServletContext JavaDoc servletContext = requestWrapper.getSession().getServletContext();
163                 PortletContext.getContext().setServletContext(servletContext);
164             } else {
165                 PortletContext.getContext().setServletConfig(((PortletConfigImpl) getPortletConfig()).getServletConfig());
166             }
167
168             /*
169              * Retrieve the "wwAction" parameter from Portlet request, and parse
170              * it to nameSpace & actionName for XWork ActionProxy.
171              */

172             String JavaDoc nameAction = request.getParameter(NAME_ACTION);
173             String JavaDoc nameXAction = request.getParameter(NAME_XACTION);
174             String JavaDoc templateFileName = request.getParameter(NAME_LINK);
175
176             String JavaDoc cheatKey = "";
177             String JavaDoc cheatValue = "";
178
179             if (nameXAction != null && !"".equals(nameXAction)) {
180                 int beginIdx = nameXAction.indexOf("./");
181                 nameAction = nameXAction.substring(((beginIdx == -1) ? 0 : (beginIdx + 1)), nameXAction.length());
182
183                 int beginPIdx = nameAction.indexOf("?");
184                 int beginPEIdx = nameAction.indexOf("=");
185
186                 // TODO: Problem, right now only support one parameter after xxx.action?????
187
// actionXURL=actionURL+"?wwXAction=./" + "xxxx.action?a=a&b=b&c=c
188
// Right now only support xxxx.action?a=a
189
if (beginPIdx >= 0 && beginPEIdx > 0) {
190                     cheatKey = nameAction.substring(beginPIdx + 1, beginPEIdx);
191                     cheatValue = nameAction.substring(beginPEIdx + 1, nameAction.length());
192                 }
193             }
194
195             if (nameAction == null || "".equals(nameAction)) {
196                 if (templateFileName == null)
197                     templateFileName = "";
198                 request.getPortletSession().setAttribute(RENDER_TEMPLATE, templateFileName);
199                 //response.setRenderParameter(RENDER_TEMPLATE,
200
// templateFileName);
201

202                 PortletMessaging.publish(request, "ActionContext", ActionContext.getContext());
203                 PortletMessaging.publish(request, "PortletContext", PortletContext.getContext());
204
205                 return;
206             }
207
208             String JavaDoc nameSpace = "";
209             int lastIndex = nameAction.lastIndexOf("/");
210             if (lastIndex > 0)
211                 nameSpace = nameAction.substring(0, lastIndex);
212             String JavaDoc actionName = getActionName(nameAction);
213
214             //Create ContextMap from Portlet request for XWork.
215
Map contextMap = createContextMap(getRequestMap(request), request.getParameterMap(), getSessionMap(request),
216                     getApplicationMap(request), request, response, cheatKey, cheatValue);
217
218             contextMap.put(PORTLET_DISPATCHER, this);
219
220             try {
221                 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(nameSpace, actionName, contextMap);
222                 proxy.execute();
223
224                 ActionGlobalContext.setContext(null);
225
226             } catch (Throwable JavaDoc e) {
227                 e.printStackTrace();
228             }
229
230             PortletMessaging.publish(request, "ActionContext", ActionContext.getContext());
231             PortletMessaging.publish(request, "PortletContext", PortletContext.getContext());
232
233         } catch (Exception JavaDoc ex) {
234             ex.printStackTrace();
235         }
236
237     }
238
239     private HashMap JavaDoc createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, PortletRequest request,
240                                      PortletResponse response, String JavaDoc cheatKey, String JavaDoc cheatValue) {
241         HashMap JavaDoc extraContext = new HashMap JavaDoc();
242
243         String JavaDoc[] cheatValues = {cheatValue};
244         if (!"".equals(cheatKey) && !"".equals(cheatValue))
245             parameterMap.put(cheatKey, cheatValues);
246
247         extraContext.put(ActionContext.PARAMETERS, parameterMap);
248         extraContext.put(ActionContext.SESSION, sessionMap);
249         extraContext.put(ActionContext.APPLICATION, applicationMap);
250         extraContext.put(ActionContext.LOCALE, request.getLocale());
251
252         extraContext.put(PORTLET_REQUEST, request);
253         extraContext.put(PORTLET_RESPONSE, response);
254         extraContext.put(PORTLET_CONTEXT, getPortletContext());
255
256         extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, request.getAttribute("DefaultComponentManager"));
257
258         // helpers to get access to request/session/application scope
259
extraContext.put("request", requestMap);
260         extraContext.put("session", sessionMap);
261         extraContext.put("application", applicationMap);
262         extraContext.put("parameters", parameterMap);
263
264         AttributeMap attrMap = new AttributeMap(extraContext);
265         extraContext.put("attr", attrMap);
266
267         return extraContext;
268     }
269
270     private Map getSessionMap(PortletRequest request) {
271         return new com.opensymphony.webwork.portlet.SessionMap(request);
272     }
273
274     private Map getApplicationMap(PortletRequest request) {
275         Map result = new HashMap JavaDoc();
276         PortalContext context = request.getPortalContext();
277         if (context == null) {
278             return result;
279         }
280
281         Enumeration JavaDoc propNames = context.getPropertyNames();
282         while (propNames.hasMoreElements()) {
283             String JavaDoc key = (String JavaDoc) propNames.nextElement();
284             Object JavaDoc value = request.getPortalContext().getProperty(key);
285             result.put(key, value);
286         }
287         return result;
288     }
289
290     private Map getRequestMap(PortletRequest request) {
291         Enumeration JavaDoc attNames = request.getAttributeNames();
292         Map result = new HashMap JavaDoc();
293         while (attNames.hasMoreElements()) {
294             String JavaDoc key = (String JavaDoc) attNames.nextElement();
295             Object JavaDoc value = request.getAttribute(key);
296             result.put(key, value);
297         }
298         return result;
299     }
300
301     private String JavaDoc getActionName(String JavaDoc name) {
302         // Get action name ("Foo.action" -> "Foo" action)
303
int beginIdx = name.lastIndexOf("/");
304         name = name.substring(((beginIdx == -1) ? 0 : (beginIdx + 1)), name.length());
305         int endIdx = name.indexOf(".");
306
307         return name.substring(0, (endIdx == -1) ? name.length() : endIdx);
308     }
309 }
Popular Tags