1 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 ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 32 public class WebWorkPortlet extends GenericPortlet implements WebWorkPortletStatics { 33 34 private static final String NAME_RESULT = "result"; 35 36 private static final String NAME_ACTION = "wwAction"; 37 38 private static final String NAME_XACTION = "wwXAction"; 39 40 private static final String NAME_LINK = "wwLink"; 41 42 private static final String NAME_DEFAULT_VIEW_FILE = "defaultViewFile"; 43 44 private static final String NAME_DEFAULT_EDIT_FILE = "defaultEditFile"; 45 46 private static final String NAME_DEFAULT_HELP_FILE = "defaultHelpFile"; 47 48 private String helpFileName = ""; 49 50 private String 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 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 { 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 e) { 84 e.printStackTrace(); 85 } 86 87 } 88 89 protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException { 90 doService(request, response, NAME_DEFAULT_EDIT_FILE); 91 } 92 93 protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { 94 doService(request, response, NAME_DEFAULT_VIEW_FILE); 95 } 96 97 private void doService(RenderRequest request, RenderResponse response, String defalutFile) throws IOException , 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 actionURL = portletURL.toString(); 111 PortletContext.getContext().setActionURL(actionURL); 112 113 String template = (String ) 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 125 ActionContext.getContext().put("template", template); 126 127 131 int jspIndex = template.lastIndexOf(".jsp"); 132 String dispatchTemplate = template; 133 if (jspIndex >= 0) 134 dispatchTemplate = template.substring(0, jspIndex) + ".jspt"; 135 136 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 ex) { 150 ex.printStackTrace(); 151 } 152 } 153 154 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { 155 156 try { 157 158 if (Container.get() == Container.TOMCAT) { 161 javax.servlet.http.HttpServletRequestWrapper requestWrapper = (javax.servlet.http.HttpServletRequestWrapper ) request; 162 ServletContext servletContext = requestWrapper.getSession().getServletContext(); 163 PortletContext.getContext().setServletContext(servletContext); 164 } else { 165 PortletContext.getContext().setServletConfig(((PortletConfigImpl) getPortletConfig()).getServletConfig()); 166 } 167 168 172 String nameAction = request.getParameter(NAME_ACTION); 173 String nameXAction = request.getParameter(NAME_XACTION); 174 String templateFileName = request.getParameter(NAME_LINK); 175 176 String cheatKey = ""; 177 String 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 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 202 PortletMessaging.publish(request, "ActionContext", ActionContext.getContext()); 203 PortletMessaging.publish(request, "PortletContext", PortletContext.getContext()); 204 205 return; 206 } 207 208 String nameSpace = ""; 209 int lastIndex = nameAction.lastIndexOf("/"); 210 if (lastIndex > 0) 211 nameSpace = nameAction.substring(0, lastIndex); 212 String actionName = getActionName(nameAction); 213 214 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 e) { 227 e.printStackTrace(); 228 } 229 230 PortletMessaging.publish(request, "ActionContext", ActionContext.getContext()); 231 PortletMessaging.publish(request, "PortletContext", PortletContext.getContext()); 232 233 } catch (Exception ex) { 234 ex.printStackTrace(); 235 } 236 237 } 238 239 private HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, PortletRequest request, 240 PortletResponse response, String cheatKey, String cheatValue) { 241 HashMap extraContext = new HashMap (); 242 243 String [] 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 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 (); 276 PortalContext context = request.getPortalContext(); 277 if (context == null) { 278 return result; 279 } 280 281 Enumeration propNames = context.getPropertyNames(); 282 while (propNames.hasMoreElements()) { 283 String key = (String ) propNames.nextElement(); 284 Object value = request.getPortalContext().getProperty(key); 285 result.put(key, value); 286 } 287 return result; 288 } 289 290 private Map getRequestMap(PortletRequest request) { 291 Enumeration attNames = request.getAttributeNames(); 292 Map result = new HashMap (); 293 while (attNames.hasMoreElements()) { 294 String key = (String ) attNames.nextElement(); 295 Object value = request.getAttribute(key); 296 result.put(key, value); 297 } 298 return result; 299 } 300 301 private String getActionName(String name) { 302 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 |