1 package com.opensymphony.webwork.dispatcher; 2 3 import com.opensymphony.util.ClassLoaderUtil; 4 import com.opensymphony.util.FileManager; 5 import com.opensymphony.webwork.ServletActionContext; 6 import com.opensymphony.webwork.WebWorkStatics; 7 import com.opensymphony.webwork.config.Configuration; 8 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping; 9 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest; 10 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper; 11 import com.opensymphony.webwork.dispatcher.multipart.WebWorkRequestWrapper; 12 import com.opensymphony.webwork.util.AttributeMap; 13 import com.opensymphony.xwork.ActionContext; 14 import com.opensymphony.xwork.ActionProxy; 15 import com.opensymphony.xwork.ActionProxyFactory; 16 import com.opensymphony.xwork.ObjectFactory; 17 import com.opensymphony.xwork.config.ConfigurationException; 18 import com.opensymphony.xwork.interceptor.component.ComponentInterceptor; 19 import com.opensymphony.xwork.interceptor.component.ComponentManager; 20 import com.opensymphony.xwork.util.LocalizedTextUtil; 21 import com.opensymphony.xwork.util.OgnlValueStack; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.util.HashMap ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 39 public class DispatcherUtils { 40 private static final Log LOG = LogFactory.getLog(DispatcherUtils.class); 41 42 private static DispatcherUtils instance; 43 44 public static void initialize(ServletContext servletContext) { 45 synchronized (DispatcherUtils.class) { 46 if (instance == null) { 47 instance = new DispatcherUtils(servletContext); 48 } 49 } 50 } 51 52 public static DispatcherUtils getInstance() { 53 return instance; 54 } 55 56 public static void setInstance(DispatcherUtils instance) { 57 DispatcherUtils.instance = instance; 58 } 59 60 protected String encoding = null; 62 63 protected Locale locale = null; 65 66 protected boolean paramsWorkaroundEnabled = false; 68 69 protected DispatcherUtils(ServletContext servletContext) { 70 boolean reloadi18n = Boolean.valueOf((String ) Configuration.get("webwork.i18n.reload")).booleanValue(); 71 LocalizedTextUtil.setReloadBundles(reloadi18n); 72 73 if (Configuration.isSet("webwork.objectFactory")) { 74 String className = (String ) Configuration.get("webwork.objectFactory"); 75 try { 76 Class clazz = ClassLoaderUtil.loadClass(className, DispatcherUtils.class); 77 ObjectFactory objectFactory = (ObjectFactory) clazz.newInstance(); 78 ObjectFactory.setObjectFactory(objectFactory); 79 } catch (Exception e) { 80 LOG.error("Could not load ObjectFactory named " + className + ". Using default ObjectFactory.", e); 81 } 82 } 83 84 LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/webwork/webwork-messages"); 85 86 if ("true".equalsIgnoreCase(Configuration.getString("webwork.configuration.xml.reload"))) { 88 FileManager.setReloadingConfigs(true); 89 } 90 91 if (Configuration.isSet("webwork.i18n.encoding")) { 92 encoding = Configuration.getString("webwork.i18n.encoding"); 93 } 94 95 if (Configuration.isSet("webwork.locale")) { 96 locale = localeFromString(Configuration.getString("webwork.locale")); 97 } 98 99 if (servletContext.getServerInfo().indexOf("WebLogic") >= 0) { 101 LOG.info("WebLogic server detected. Enabling WebWork parameter access work-around."); 102 paramsWorkaroundEnabled = true; 103 } else if (Configuration.isSet("webwork.dispatcher.parametersWorkaround")) { 104 paramsWorkaroundEnabled = "true".equals(Configuration.get("webwork.dispatcher.parametersWorkaround")); 105 } else { 106 LOG.debug("Parameter access work-around disabled."); 107 } 108 } 109 110 120 public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) { 121 Map requestMap = new RequestMap(request); 123 124 Map params = mapping.getParams(); 126 Map requestParams = new HashMap (request.getParameterMap()); 127 if (params != null) { 128 params.putAll(requestParams); 129 } else { 130 params = requestParams; 131 } 132 133 Map session = new SessionMap(request); 135 136 Map application = new ApplicationMap(context); 138 139 HashMap extraContext = createContextMap(requestMap, params, session, application, request, response, context); 140 141 OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY); 143 if (stack != null) { 144 extraContext.put(ActionContext.VALUE_STACK, new OgnlValueStack(stack)); 145 } 146 try { 147 String namespace = mapping.getNamespace(); 148 String name = mapping.getName(); 149 String method = mapping.getMethod(); 150 151 ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext); 152 proxy.setMethod(method); 153 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack()); 154 proxy.execute(); 155 if (stack != null) { 157 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack); 158 } 159 } catch (ConfigurationException e) { 160 LOG.error("Could not find action", e); 161 sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e); 162 } catch (Exception e) { 163 LOG.error("Could not execute action", e); 164 sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); 165 } 166 } 167 168 181 public HashMap createContextMap(Map requestMap, 182 Map parameterMap, 183 Map sessionMap, 184 Map applicationMap, 185 HttpServletRequest request, 186 HttpServletResponse response, 187 ServletContext servletContext) { 188 HashMap extraContext = new HashMap (); 189 extraContext.put(ActionContext.PARAMETERS, new HashMap (parameterMap)); 190 extraContext.put(ActionContext.SESSION, sessionMap); 191 extraContext.put(ActionContext.APPLICATION, applicationMap); 192 extraContext.put(ActionContext.LOCALE, (locale == null) ? request.getLocale() : locale); 193 194 extraContext.put(WebWorkStatics.HTTP_REQUEST, request); 195 extraContext.put(WebWorkStatics.HTTP_RESPONSE, response); 196 extraContext.put(WebWorkStatics.SERVLET_CONTEXT, servletContext); 197 extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, request.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY)); 198 199 extraContext.put("request", requestMap); 201 extraContext.put("session", sessionMap); 202 extraContext.put("application", applicationMap); 203 extraContext.put("parameters", parameterMap); 204 205 AttributeMap attrMap = new AttributeMap(extraContext); 206 extraContext.put("attr", attrMap); 207 208 return extraContext; 209 } 210 211 217 public Locale localeFromString(String localeStr) { 218 if ((localeStr == null) || (localeStr.trim().length() == 0) || (localeStr.equals("_"))) { 219 return null; 220 } 221 int index = localeStr.indexOf('_'); 222 if (index < 0) { 223 return new Locale (localeStr, ""); 224 } 225 String language = localeStr.substring(0, index); 226 if (index == localeStr.length()) { 227 return new Locale (language, ""); 228 } 229 localeStr = localeStr.substring(index + 1); 230 index = localeStr.indexOf('_'); 231 if (index < 0) { 232 return new Locale (language, localeStr); 233 } 234 String country = localeStr.substring(0, index); 235 if (index == localeStr.length()) { 236 return new Locale (language, country); 237 } 238 localeStr = localeStr.substring(index + 1); 239 return new Locale (language, country, localeStr); 240 } 241 242 247 public static int getMaxSize() { 248 Integer maxSize = new Integer (Integer.MAX_VALUE); 249 try { 250 String maxSizeStr = Configuration.getString("webwork.multipart.maxSize"); 251 252 if (maxSizeStr != null) { 253 try { 254 maxSize = new Integer (maxSizeStr); 255 } catch (NumberFormatException e) { 256 LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE"); 257 } 258 } else { 259 LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE"); 260 } 261 } catch (IllegalArgumentException e1) { 262 LOG.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE"); 263 } 264 265 if (LOG.isDebugEnabled()) { 266 LOG.debug("maxSize=" + maxSize); 267 } 268 269 return maxSize.intValue(); 270 } 271 272 277 public String getSaveDir(ServletContext servletContext) { 278 String saveDir = Configuration.getString("webwork.multipart.saveDir").trim(); 279 280 if (saveDir.equals("")) { 281 File tempdir = (File ) servletContext.getAttribute("javax.servlet.context.tempdir"); 282 LOG.info("Unable to find 'webwork.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir"); 283 284 if (tempdir != null) { 285 saveDir = tempdir.toString(); 286 } 287 } else { 288 File multipartSaveDir = new File (saveDir); 289 290 if (!multipartSaveDir.exists()) { 291 multipartSaveDir.mkdir(); 292 } 293 } 294 295 if (LOG.isDebugEnabled()) { 296 LOG.debug("saveDir=" + saveDir); 297 } 298 299 return saveDir; 300 } 301 302 public void prepare(HttpServletRequest request, HttpServletResponse response) { 303 if (encoding != null) { 304 try { 305 request.setCharacterEncoding(encoding); 306 } catch (Exception e) { 307 LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e); 308 } 309 } 310 311 if (locale != null) { 312 response.setLocale(locale); 313 } 314 315 if (paramsWorkaroundEnabled) { 316 request.getParameter("foo"); } 318 } 319 320 330 public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException { 331 if (request instanceof WebWorkRequestWrapper) { 333 return request; 334 } 335 336 if (MultiPartRequest.isMultiPart(request)) { 337 request = new MultiPartRequestWrapper(request, getSaveDir(servletContext), getMaxSize()); 338 } else { 339 request = new WebWorkRequestWrapper(request); 340 } 341 342 return request; 343 } 344 345 353 public void sendError(HttpServletRequest request, HttpServletResponse response, int code, Exception e) { 354 try { 355 request.setAttribute("javax.servlet.error.exception", e); 358 359 request.setAttribute("javax.servlet.jsp.jspException", e); 361 362 response.sendError(code, e.getMessage()); 364 } catch (IOException e1) { 365 } 366 } 367 } 368 | Popular Tags |