1 package com.sslexplorer.tasks; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.UnsupportedEncodingException ; 6 import java.net.MalformedURLException ; 7 import java.net.URL ; 8 import java.security.Principal ; 9 import java.util.Enumeration ; 10 import java.util.Hashtable ; 11 import java.util.Locale ; 12 import java.util.Map ; 13 import java.util.StringTokenizer ; 14 import java.util.Vector ; 15 16 import javax.servlet.RequestDispatcher ; 17 import javax.servlet.ServletInputStream ; 18 import javax.servlet.http.Cookie ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpSession ; 21 22 import org.apache.struts.Globals; 23 import org.apache.struts.config.ModuleConfig; 24 import org.mortbay.util.MultiMap; 25 26 import com.sslexplorer.boot.Util; 27 28 public class TaskHttpServletRequest implements HttpServletRequest { 29 30 public static final String ATTR_TASK = "task"; 31 public static final String ATTR_TASK_FORWARD = "taskForward"; 32 public static final String ATTR_TASK_PROGRESS_HANDLED_EXTERNALLY = "taskProgressHandledExternally"; 33 34 private StringBuffer url; 35 private String uri; 36 private Hashtable <String , String []> params; 37 private Hashtable <String , Object > attributes; 38 private String query; 39 private String servletPath; 40 41 private HttpSession session; 42 private Cookie [] cookies; 43 private String characterEncoding; 44 private String authType; 45 private Vector <String > headerNames; 46 private String contextPath; 47 private MultiMap headers; 48 private String pathInfo; 49 private String pathTranslated; 50 private String remoteUser; 51 private String requestedSessionId; 52 private Principal userPrincipal; 53 private boolean requestedSessionIdFromCookie; 54 private boolean requestedSessionIdFromURL; 55 private boolean requestedSessionIdValid; 56 private String localAddr; 57 private String localName; 58 private int localPort; 59 private Locale locale; 60 private Vector <Locale > locales; 61 private String protocol; 62 private String remoteAddr; 63 private String remoteHost; 64 private int remotePort; 65 private String scheme; 66 private String serverName; 67 private int serverPort; 68 private boolean secure; 69 70 public TaskHttpServletRequest(HttpServletRequest wrapping, Task task) { 71 this.session = wrapping.getSession(); 72 String location = wrapping.getParameter("url"); 73 74 cookies = wrapping.getCookies(); 75 characterEncoding = wrapping.getCharacterEncoding(); 76 authType = wrapping.getAuthType(); 77 headerNames = new Vector <String >(); 78 headers = new MultiMap(); 79 for(Enumeration e = wrapping.getHeaderNames(); 80 e.hasMoreElements(); ) { 81 String headerName = (String )e.nextElement(); 82 for(Enumeration f = wrapping.getHeaders(headerName); f.hasMoreElements(); ) { 83 String headerValue = (String )f.nextElement(); 84 headers.add(headerName, headerValue); 85 } 86 } 87 contextPath = wrapping.getContextPath(); 88 pathInfo = wrapping.getPathInfo(); 89 pathTranslated = wrapping.getPathTranslated(); 90 remoteUser = wrapping.getRemoteUser(); requestedSessionId = wrapping.getRequestedSessionId(); userPrincipal = wrapping.getUserPrincipal(); requestedSessionIdFromCookie = wrapping.isRequestedSessionIdFromCookie(); 94 requestedSessionIdFromURL = wrapping.isRequestedSessionIdFromURL(); 95 requestedSessionIdValid = wrapping.isRequestedSessionIdValid(); 96 localAddr = wrapping.getLocalAddr(); 97 localName = wrapping.getLocalName(); 98 localPort = wrapping.getLocalPort(); 99 locale = wrapping.getLocale(); 100 locales = new Vector <Locale >(); 101 for(Enumeration e = wrapping.getLocales(); e.hasMoreElements(); locales.add((Locale )e.nextElement()) ); 102 protocol = wrapping.getProtocol(); 103 remoteAddr = wrapping.getRemoteAddr(); 104 remoteHost = wrapping.getRemoteHost(); 105 remotePort = wrapping.getRemotePort(); 106 scheme = wrapping.getScheme(); 107 serverName = wrapping.getServerName(); 108 serverPort = wrapping.getServerPort(); 109 secure = wrapping.isSecure(); 110 111 int idx = location.indexOf('?'); 113 query = null; 114 if(idx != -1) { 115 query = location.substring(idx + 1); 116 } 117 118 uri = location; 120 if(idx != -1) { 121 uri = uri.substring(0, idx); 122 } 123 124 servletPath = uri; 126 127 params = new Hashtable <String , String []>(); 129 if(query != null) { 130 StringTokenizer t = new StringTokenizer (query, "&"); 131 while(t.hasMoreTokens()) { 132 String token = t.nextToken(); 133 idx = token.indexOf('='); 134 String name = token; 135 String val = null; 136 if(idx != -1) { 137 name = token.substring(0, idx); 138 val = token.substring(idx + 1); 139 } 140 else { 141 val = ""; 142 } 143 String [] vals = params.get(name); 144 if(vals == null) { 145 vals = new String [] { val }; 146 } 147 else { 148 String [] nvals = new String [vals.length + 1]; 149 System.arraycopy(vals, 0, nvals, 0, vals.length); 150 nvals[vals.length] = val; 151 vals = nvals; 152 } 153 params.put(name, vals); 154 } 155 } 156 157 attributes = new Hashtable <String , Object >(); 159 160 try { 162 URL u = new URL (new URL (wrapping.getRequestURL().toString()), uri); 163 url = new StringBuffer (u.toExternalForm()); 164 } catch (MalformedURLException e) { 165 } 166 167 setAttribute(ATTR_TASK, task); 168 } 169 170 public String getServletPath() { 171 return servletPath; 172 } 173 174 public String getQueryString() { 175 return query; 176 } 177 178 public String getRequestURI() { 179 return uri; 180 } 181 182 public StringBuffer getRequestURL() { 183 return url; 184 } 185 186 public String getParameter(String arg0) { 187 String [] vals = params.get(arg0); 188 return vals == null ? null : vals[0]; 189 } 190 191 public Map getParameterMap() { 192 return params; 193 } 194 195 public Enumeration getParameterNames() { 196 return params.keys(); 197 } 198 199 public String [] getParameterValues(String arg0) { 200 return params.get(arg0); 201 } 202 203 public String getMethod() { 204 return "GET"; 205 } 206 207 public HttpSession getSession(boolean arg0) { 208 if(arg0) 210 throw new IllegalStateException ("Cannot create session"); 211 return getSession(); 212 } 213 214 public Object getAttribute(String arg0) { 215 return attributes.get(arg0); 216 } 217 218 public Enumeration getAttributeNames() { 219 return attributes.keys(); 220 } 221 222 public String getCharacterEncoding() { 223 return characterEncoding; 224 } 225 226 public int getContentLength() { 227 return 0; 228 } 229 230 public String getContentType() { 231 return null; 232 } 233 234 public ServletInputStream getInputStream() throws IOException { 235 return null; 236 } 237 238 public BufferedReader getReader() throws IOException { 239 return null; 240 } 241 242 public void removeAttribute(String arg0) { 243 attributes.remove(arg0); 244 } 245 246 public void setAttribute(String arg0, Object arg1) { 247 attributes.put(arg0, arg1); 248 } 249 250 public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException { 251 this.characterEncoding = characterEncoding; 252 } 253 254 public Cookie [] getCookies() { 255 return cookies; 256 } 257 258 public String getAuthType() { 259 return authType; 260 } 261 262 public String getContextPath() { 263 return contextPath; 264 } 265 266 public long getDateHeader(String name) { 267 throw new IllegalArgumentException ("Not implemented."); 268 } 269 270 public String getHeader(String name) { 271 return headers.getString(name); 272 } 273 274 public Enumeration getHeaderNames() { 275 return headerNames.elements(); 276 } 277 278 public Enumeration getHeaders(String name) { 279 return new Vector (headers.getValues(name)).elements(); 280 } 281 282 public int getIntHeader(String name) { 283 return Integer.parseInt(getHeader(name)); 284 } 285 286 public String getPathInfo() { 287 return pathInfo; 288 } 289 290 public String getPathTranslated() { 291 return pathTranslated; 292 } 293 294 public String getRemoteUser() { 295 return remoteUser; 296 } 297 298 public String getRequestedSessionId() { 299 return requestedSessionId; 300 } 301 302 public HttpSession getSession() { 303 return session; 304 } 305 306 public Principal getUserPrincipal() { 307 return userPrincipal; 308 } 309 310 public boolean isRequestedSessionIdFromCookie() { 311 return requestedSessionIdFromCookie; 312 } 313 314 public boolean isRequestedSessionIdFromURL() { 315 return requestedSessionIdFromURL; 316 } 317 318 public boolean isRequestedSessionIdFromUrl() { 319 return isRequestedSessionIdFromURL(); 320 } 321 322 public boolean isRequestedSessionIdValid() { 323 return requestedSessionIdValid; 324 } 325 326 public boolean isUserInRole(String role) { 327 throw new IllegalArgumentException ("Not implemented."); 328 } 329 330 public String getLocalAddr() { 331 return localAddr; 332 } 333 334 public String getLocalName() { 335 return localName; 336 } 337 338 public int getLocalPort() { 339 return localPort; 340 } 341 342 public Locale getLocale() { 343 return locale; 344 } 345 346 public Enumeration getLocales() { 347 return locales.elements(); 348 } 349 350 public String getProtocol() { 351 return protocol; 352 } 353 354 public String getRealPath(String path) { 355 throw new IllegalArgumentException ("Not implemented."); 356 } 357 358 public String getRemoteAddr() { 359 return remoteAddr; 360 } 361 362 public String getRemoteHost() { 363 return remoteHost; 364 } 365 366 public int getRemotePort() { 367 return remotePort; 368 } 369 370 public RequestDispatcher getRequestDispatcher(String path) { 371 throw new IllegalStateException ("Request dispatcher not available."); 372 } 373 374 public String getScheme() { 375 return scheme; 376 } 377 378 public String getServerName() { 379 return serverName; 380 } 381 382 public int getServerPort() { 383 return serverPort; 384 } 385 386 public boolean isSecure() { 387 return secure; 388 } 389 390 } 391 | Popular Tags |