1 18 19 package org.apache.struts.upload; 20 21 import java.util.Map ; 22 import java.util.Locale ; 23 import java.util.Vector ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.io.IOException ; 30 import java.io.BufferedReader ; 31 import java.security.Principal ; 32 import javax.servlet.ServletInputStream ; 33 import javax.servlet.RequestDispatcher ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpSession ; 36 import javax.servlet.http.HttpServletRequest ; 37 import java.lang.reflect.InvocationTargetException ; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.commons.beanutils.MethodUtils; 41 42 56 public class MultipartRequestWrapper implements HttpServletRequest { 57 58 59 private static final Log log = LogFactory.getLog(MultipartRequestWrapper.class); 60 61 64 protected Map parameters; 65 66 69 protected HttpServletRequest request; 70 71 public MultipartRequestWrapper(HttpServletRequest request) { 72 this.request = request; 73 this.parameters = new HashMap (); 74 } 75 76 81 public void setParameter(String name, String value) { 82 String [] mValue = (String []) parameters.get(name); 83 if (mValue == null) { 84 mValue = new String [0]; 85 } 86 String [] newValue = new String [mValue.length + 1]; 87 System.arraycopy(mValue, 0, newValue, 0, mValue.length); 88 newValue[mValue.length] = value; 89 90 parameters.put(name, newValue); 91 } 92 93 99 public String getParameter(String name) { 100 String value = request.getParameter(name); 101 if (value == null) { 102 String [] mValue = (String []) parameters.get(name); 103 if ((mValue != null) && (mValue.length > 0)) { 104 value = mValue[0]; 105 } 106 } 107 return value; 108 } 109 110 115 public Enumeration getParameterNames() { 116 Enumeration baseParams = request.getParameterNames(); 117 Vector list = new Vector (); 118 while (baseParams.hasMoreElements()) { 119 list.add(baseParams.nextElement()); 120 } 121 Collection multipartParams = parameters.keySet(); 122 Iterator iterator = multipartParams.iterator(); 123 while (iterator.hasNext()) { 124 list.add(iterator.next()); 125 } 126 return Collections.enumeration(list); 127 } 128 129 public String [] getParameterValues(String name) { 130 String [] value = request.getParameterValues(name); 131 if (value == null) { 132 value = (String []) parameters.get(name); 133 } 134 return value; 135 } 136 137 140 public HttpServletRequest getRequest() { 141 return request; 142 } 143 144 public Object getAttribute(String name) { 146 return request.getAttribute(name); 147 } 148 public Enumeration getAttributeNames() { 149 return request.getAttributeNames(); 150 } 151 public String getCharacterEncoding() { 152 return request.getCharacterEncoding(); 153 } 154 public int getContentLength() { 155 return request.getContentLength(); 156 } 157 public String getContentType() { 158 return request.getContentType(); 159 } 160 public ServletInputStream getInputStream() throws IOException { 161 return request.getInputStream(); 162 } 163 public String getProtocol() { 164 return request.getProtocol(); 165 } 166 public String getScheme() { 167 return request.getScheme(); 168 } 169 public String getServerName() { 170 return request.getServerName(); 171 } 172 public int getServerPort() { 173 return request.getServerPort(); 174 } 175 public BufferedReader getReader() throws IOException { 176 return request.getReader(); 177 } 178 public String getRemoteAddr() { 179 return request.getRemoteAddr(); 180 } 181 public String getRemoteHost() { 182 return request.getRemoteHost(); 183 } 184 public void setAttribute(String name, Object o) { 185 request.setAttribute(name, o); 186 } 187 public void removeAttribute(String name) { 188 request.removeAttribute(name); 189 } 190 public Locale getLocale() { 191 return request.getLocale(); 192 } 193 public Enumeration getLocales() { 194 return request.getLocales(); 195 } 196 public boolean isSecure() { 197 return request.isSecure(); 198 } 199 public RequestDispatcher getRequestDispatcher(String path) { 200 return request.getRequestDispatcher(path); 201 } 202 public String getRealPath(String path) { 203 return request.getRealPath(path); 204 } 205 206 public String getAuthType() { 208 return request.getAuthType(); 209 } 210 public Cookie [] getCookies() { 211 return request.getCookies(); 212 } 213 public long getDateHeader(String name) { 214 return request.getDateHeader(name); 215 } 216 public String getHeader(String name) { 217 return request.getHeader(name); 218 } 219 public Enumeration getHeaders(String name) { 220 return request.getHeaders(name); 221 } 222 public Enumeration getHeaderNames() { 223 return request.getHeaderNames(); 224 } 225 public int getIntHeader(String name) { 226 return request.getIntHeader(name); 227 } 228 public String getMethod() { 229 return request.getMethod(); 230 } 231 public String getPathInfo() { 232 return request.getPathInfo(); 233 } 234 public String getPathTranslated() { 235 return request.getPathTranslated(); 236 } 237 public String getContextPath() { 238 return request.getContextPath(); 239 } 240 public String getQueryString() { 241 return request.getQueryString(); 242 } 243 public String getRemoteUser() { 244 return request.getRemoteUser(); 245 } 246 public boolean isUserInRole(String user) { 247 return request.isUserInRole(user); 248 } 249 public Principal getUserPrincipal() { 250 return request.getUserPrincipal(); 251 } 252 public String getRequestedSessionId() { 253 return request.getRequestedSessionId(); 254 } 255 public String getRequestURI() { 256 return request.getRequestURI(); 257 } 258 public String getServletPath() { 259 return request.getServletPath(); 260 } 261 public HttpSession getSession(boolean create) { 262 return request.getSession(create); 263 } 264 public HttpSession getSession() { 265 return request.getSession(); 266 } 267 public boolean isRequestedSessionIdValid() { 268 return request.isRequestedSessionIdValid(); 269 } 270 public boolean isRequestedSessionIdFromURL() { 271 return request.isRequestedSessionIdFromURL(); 272 } 273 public boolean isRequestedSessionIdFromUrl() { 274 return request.isRequestedSessionIdFromUrl(); 275 } 276 277 279 282 public Map getParameterMap() { 283 Map map = new HashMap (parameters); 284 Enumeration names = request.getParameterNames(); 285 while (names.hasMoreElements()) { 286 String name = (String )names.nextElement(); 287 map.put(name, request.getParameterValues(name)); 288 } 289 return map; 290 } 291 292 296 public void setCharacterEncoding(String encoding) { 297 invokeRequestMethod("setCharacterEncoding", new Object [] {encoding}); 298 } 299 300 304 public StringBuffer getRequestURL() { 305 return (StringBuffer )invokeRequestMethod("getRequestURL", null); 306 } 307 308 312 public boolean isRequestedSessionIdFromCookie() { 313 Object result = invokeRequestMethod("isRequestedSessionIdFromCookie", null); 314 return (result == null) ? false : ((Boolean )result).booleanValue(); 315 } 316 317 319 323 public String getLocalAddr() { 324 return (String )invokeRequestMethod("getLocalAddr", null); 325 } 326 327 331 public String getLocalName() { 332 return (String )invokeRequestMethod("getLocalName", null); 333 } 334 335 339 public int getLocalPort() { 340 Object result = invokeRequestMethod("getLocalPort", null); 341 return (result == null) ? 0 : ((Integer )result).intValue(); 342 } 343 344 348 public int getRemotePort() { 349 Object result = invokeRequestMethod("getRemotePort", null); 350 return (result == null) ? 0 : ((Integer )result).intValue(); 351 } 352 353 357 private Object invokeRequestMethod(String name, Object [] args) { 358 try { 359 return MethodUtils.invokeExactMethod(request, name, args); 360 } catch (NoSuchMethodException e) { 361 if (log.isDebugEnabled()) { 362 log.debug("Method '" +name + "' not defined for javax.servlet.http.HttpServletRequest"); 363 } 364 } catch (InvocationTargetException e) { 365 log.error("Error invoking method '" +name + "' ", e.getTargetException()); 366 } catch (IllegalAccessException e) { 367 log.error("Error invoking method '" +name + "' ", e); 368 } 369 return null; 370 } 371 372 } 373 | Popular Tags |