001 package railo.runtime.net.http; 002 003 import java.io.BufferedReader; 004 import java.io.IOException; 005 import java.io.Serializable; 006 import java.io.UnsupportedEncodingException; 007 import java.net.InetAddress; 008 import java.net.UnknownHostException; 009 import java.security.Principal; 010 import java.util.ArrayList; 011 import java.util.Date; 012 import java.util.Enumeration; 013 import java.util.HashSet; 014 import java.util.Locale; 015 import java.util.Map; 016 017 import javax.servlet.RequestDispatcher; 018 import javax.servlet.ServletInputStream; 019 import javax.servlet.http.Cookie; 020 import javax.servlet.http.HttpServletRequest; 021 import javax.servlet.http.HttpSession; 022 023 import railo.commons.collections.HashTable; 024 import railo.commons.io.IOUtil; 025 import railo.commons.io.res.Resource; 026 import railo.commons.lang.Pair; 027 import railo.runtime.config.Config; 028 import railo.runtime.exp.PageException; 029 import railo.runtime.op.Caster; 030 import railo.runtime.op.date.DateCaster; 031 import railo.runtime.type.Array; 032 import railo.runtime.type.KeyImpl; 033 import railo.runtime.type.Struct; 034 import railo.runtime.type.StructImpl; 035 import railo.runtime.type.dt.DateTimeImpl; 036 import railo.runtime.type.it.ItAsEnum; 037 import railo.runtime.util.EnumerationWrapper; 038 039 public final class HttpServletRequestDummy implements HttpServletRequest,Serializable { 040 041 042 private Cookie[] cookies; 043 044 private String authType; 045 private Pair<String,Object>[] headers=new Pair[0]; 046 private Pair<String,Object>[] parameters=new Pair[0]; 047 private Struct attributes=new StructImpl(); 048 private String method="GET"; 049 private String pathInfo; 050 private String pathTranslated; 051 private String contextPath="/"; 052 private String queryString; 053 private String remoteUser; 054 private String requestedSessionId; 055 private String requestURI; 056 057 private String protocol="HTTP/1.1"; 058 private String serverName="localhost"; 059 private int port=80; 060 061 private String characterEncoding="ISO-8859-1"; 062 063 private String contentType; 064 private byte[] inputData=new byte[0]; 065 066 067 private static InetAddress DEFAULT_REMOTE; 068 private static String DEFAULT_REMOTE_ADDR; 069 private static String DEFAULT_REMOTE_HOST; 070 static { 071 try { 072 DEFAULT_REMOTE=InetAddress.getLocalHost(); 073 DEFAULT_REMOTE_ADDR=DEFAULT_REMOTE.getHostAddress(); 074 DEFAULT_REMOTE_HOST=DEFAULT_REMOTE.getHostName(); 075 } 076 catch (UnknownHostException e) {} 077 } 078 //private InetAddress remoteq=DEFAULT_REMOTE; 079 private String remoteAddr=DEFAULT_REMOTE_ADDR; 080 private String remoteHost=DEFAULT_REMOTE_HOST; 081 082 private Locale locale=Locale.getDefault(); 083 084 private boolean secure; 085 086 private Resource contextRoot; 087 088 private String scheme="http"; 089 090 private HttpSession session; 091 092 093 094 /** 095 * constructor of the class 096 * @param headers 097 * @param parameters 098 * @param httpSession 099 * @param pairs 100 * @param cookiess 101 */ 102 public HttpServletRequestDummy(Resource contextRoot,String serverName, String scriptName,String queryString, 103 Cookie[] cookies, Pair[] headers, Pair[] parameters, Struct attributes, HttpSession session) { 104 this.serverName=serverName; 105 requestURI=scriptName; 106 this.queryString=queryString; 107 this.parameters=translateQS(queryString); 108 this.contextRoot=contextRoot; 109 if(cookies!=null)setCookies(cookies); 110 if(headers!=null)this.headers=headers; 111 if(parameters!=null)this.parameters=parameters; 112 if(attributes!=null)this.attributes=attributes; 113 this.session=session; 114 } 115 /** 116 * constructor of the class 117 * @throws PageException 118 * / 119 public HttpServletRequestDummy(String serverName, String scriptName,Struct queryString) throws PageException { 120 this.serverName=serverName; 121 requestURI=scriptName; 122 123 StringBuffer qs=new StringBuffer(); 124 String[] keys=queryString.keys(); 125 parameters=new Item[keys.length]; 126 String key; 127 Object value; 128 for(int i=0;i<keys.length;i++) { 129 if(i>0) qs.append('&'); 130 key=keys[i]; 131 value=queryString.get(key); 132 parameters[i]=new Item(key,value); 133 134 qs.append(key); 135 qs.append('='); 136 qs.append(Caster.toString(value)); 137 } 138 139 this.queryString=qs.toString(); 140 }*/ 141 142 private Pair[] translateQS(String qs) { 143 if(qs==null) return new Pair[0]; 144 Array arr=railo.runtime.type.util.ListUtil.listToArrayRemoveEmpty(qs,"&"); 145 Pair[] parameters=new Pair[arr.size()]; 146 //Array item; 147 int index; 148 String name; 149 150 for(int i=1;i<=parameters.length;i++) { 151 name=Caster.toString(arr.get(i,""),""); 152 index=name.indexOf('='); 153 if(index!=-1) parameters[i-1]=new Pair(name.substring(0,index),name.substring(index+1)); 154 else parameters[i-1]=new Pair(name,""); 155 156 } 157 return parameters; 158 } 159 160 161 162 @Override 163 public String getAuthType() { 164 return authType; 165 } 166 167 /** 168 * sets the name of the authentication scheme used to protect the servlet. 169 * All servlet containers support basic, 170 * form and client certificate authentication, 171 * and may additionally support digest authentication. 172 * @param authType authentication type 173 */ 174 public void setAuthType(String authType) { 175 this.authType=authType; 176 } 177 178 @Override 179 public Cookie[] getCookies() { 180 return cookies; 181 } 182 183 /** 184 * sets an array containing all of the Cookie objects 185 * the client sent with this request. 186 * This method returns null if no cookies were sent. 187 * @param cookies 188 */ 189 public void setCookies(Cookie[] cookies) { 190 this.cookies=cookies; 191 } 192 193 @Override 194 public long getDateHeader(String name) { 195 Object value=getHeader(name); 196 if(value!=null) { 197 Date date=DateCaster.toDateAdvanced(value,null,null); 198 if(date!=null)return date.getTime(); 199 throw new IllegalArgumentException("can't convert value "+value+" to a Date"); 200 } 201 return -1; 202 } 203 204 public void setDateHeader(String name, long value) { 205 // TODO wrong format 206 setHeader(name,new DateTimeImpl(value,false).castToString()); 207 } 208 209 @Override 210 public String getHeader(String name) { 211 return ReqRspUtil.get(headers,name); 212 } 213 214 /** 215 * sets a new header value 216 * @param name name of the new value 217 * @param value header value 218 */ 219 public void setHeader(String name, String value) { 220 headers=ReqRspUtil.set(headers,name,value); 221 } 222 223 /** 224 * add a new header value 225 * @param name name of the new value 226 * @param value header value 227 */ 228 public void addHeader(String name, String value) { 229 headers=ReqRspUtil.add(headers,name,value); 230 } 231 232 233 @Override 234 public Enumeration getHeaders(String name) { 235 HashSet set=new HashSet(); 236 for(int i=0;i<headers.length;i++) { 237 if(headers[i].getName().equalsIgnoreCase(name)) 238 set.add(Caster.toString(headers[i].getValue(),null)); 239 } 240 return new EnumerationWrapper(set); 241 } 242 243 @Override 244 public Enumeration getHeaderNames() { 245 HashSet set=new HashSet(); 246 for(int i=0;i<headers.length;i++) { 247 set.add(headers[i].getName()); 248 } 249 return new EnumerationWrapper(set); 250 } 251 252 @Override 253 public int getIntHeader(String name) { 254 Object value=getHeader(name); 255 if(value!=null) { 256 try { 257 return Caster.toIntValue(value); 258 } catch (PageException e) { 259 throw new NumberFormatException(e.getMessage()); 260 } 261 } 262 return -1; 263 } 264 265 @Override 266 public String getMethod() { 267 return method; 268 } 269 270 /** 271 * sets the request method 272 * @param method 273 */ 274 public void setMethod(String method) { 275 this.method = method; 276 } 277 278 @Override 279 public String getPathInfo() { 280 return pathInfo; 281 } 282 283 284 /** 285 * Sets any extra path information associated with the URL the client sent 286 * when it made this request. 287 * The extra path information follows the servlet path but precedes 288 * the query string. 289 * @param pathInfo 290 */ 291 public void setPathInfo(String pathInfo) { 292 this.pathInfo = pathInfo; 293 } 294 295 296 @Override 297 public String getPathTranslated() { 298 return pathTranslated; 299 } 300 301 /** 302 * sets any extra path information after the servlet name 303 * but before the query string, translates to a real path. 304 * Same as the value of the CGI variable PATH_TRANSLATED. 305 * @param pathTranslated 306 */ 307 public void setPathTranslated(String pathTranslated) { 308 // TODO muss auf pathinfo basieren 309 this.pathTranslated = pathTranslated; 310 } 311 312 @Override 313 public String getContextPath() { 314 return contextPath; 315 } 316 317 /** 318 * sets the portion of the request URI that indicates the context of the request. 319 * The context path always comes first in a request URI. 320 * The path starts with a "/" character but does not end with a "/" character. 321 * @param contextPath 322 */ 323 public void setContextPath(String contextPath) { 324 this.contextPath = contextPath; 325 } 326 327 @Override 328 public String getQueryString() { 329 return queryString; 330 } 331 332 /** 333 * sets the query string that is contained in the request URL after the path. 334 * Same as the value of the CGI variable QUERY_STRING. 335 336 * @param queryString 337 */ 338 public void setQueryString(String queryString) { 339 this.queryString = queryString; 340 parameters=translateQS(queryString); 341 } 342 343 @Override 344 public String getRemoteUser() { 345 return remoteUser; 346 } 347 348 /** 349 * sets the login of the user making this request, 350 * if the user has been authenticated, 351 * or null if the user has not been authenticated. 352 * Whether the user name is sent with each subsequent request depends 353 * on the browser and type of authentication. 354 * Same as the value of the CGI variable REMOTE_USER. 355 * @param remoteUser 356 */ 357 public void setRemoteUser(String remoteUser) { 358 this.remoteUser = remoteUser; 359 } 360 361 @Override 362 public boolean isUserInRole(String role) { 363 // TODO impl 364 return false; 365 } 366 367 @Override 368 public Principal getUserPrincipal() { 369 //TODO impl 370 return null; 371 } 372 373 @Override 374 public String getRequestedSessionId() { 375 return requestedSessionId; 376 } 377 378 /** 379 * sets the session ID specified by the client. 380 * This may not be the same as the ID of the actual session in use. 381 * For example, if the request specified an old (expired) session ID 382 * and the server has started a new session, 383 * this method gets a new session with a new ID. 384 * @param requestedSessionId 385 */ 386 public void setRequestedSessionId(String requestedSessionId) { 387 this.requestedSessionId = requestedSessionId; 388 } 389 390 @Override 391 public String getRequestURI() { 392 return requestURI; 393 } 394 395 /** 396 * sets the part of this request's URL from the protocol name 397 * up to the query string in the first line of the HTTP request. 398 * The web container does not decode this String. 399 * @param requestURI 400 */ 401 public void setRequestURI(String requestURI) { 402 this.requestURI = requestURI; 403 } 404 405 @Override 406 public StringBuffer getRequestURL() { 407 return new StringBuffer(isSecure()?"https":"http"). 408 append("://"). 409 append(serverName). 410 append(':'). 411 append(port). 412 append('/'). 413 append(requestURI); 414 } 415 416 @Override 417 public String getServletPath() { 418 // TODO when different ? 419 return requestURI; 420 } 421 @Override 422 public HttpSession getSession(boolean arg0) { 423 return session; 424 } 425 @Override 426 public HttpSession getSession() { 427 return getSession(true); 428 } 429 @Override 430 public boolean isRequestedSessionIdValid() { 431 // not supported 432 return false; 433 } 434 @Override 435 public boolean isRequestedSessionIdFromCookie() { 436 // not supported 437 return false; 438 } 439 @Override 440 public boolean isRequestedSessionIdFromURL() { 441 // not supported 442 return false; 443 } 444 @Override 445 public boolean isRequestedSessionIdFromUrl() { 446 return isRequestedSessionIdFromURL(); 447 } 448 @Override 449 public Object getAttribute(String key) { 450 return attributes.get(key,null); 451 } 452 453 @Override 454 public void setAttribute(String key, Object value) { 455 attributes.setEL(key,value); 456 } 457 458 @Override 459 public void removeAttribute(String key) { 460 attributes.removeEL(KeyImpl.init(key)); 461 } 462 463 @Override 464 public Enumeration getAttributeNames() { 465 return ItAsEnum.toStringEnumeration(attributes.keyIterator()); 466 } 467 @Override 468 public String getCharacterEncoding() { 469 return characterEncoding; 470 } 471 @Override 472 public void setCharacterEncoding(String characterEncoding) 473 throws UnsupportedEncodingException { 474 this.characterEncoding=characterEncoding; 475 } 476 @Override 477 public int getContentLength() { 478 return -1; 479 } 480 @Override 481 public String getContentType() { 482 return contentType; 483 } 484 485 /** 486 * sets the content Type of the Request 487 * @param contentType 488 */ 489 public void setContentType(String contentType) { 490 this.contentType=contentType; 491 } 492 493 @Override 494 public ServletInputStream getInputStream() throws IOException { 495 return new ServletInputStreamDummy(inputData); 496 } 497 498 public void setParameter(String key,String value) { 499 parameters=ReqRspUtil.set(parameters,key,value); 500 rewriteQS(); 501 } 502 503 public void addParameter(String key,String value) { 504 parameters=ReqRspUtil.add(parameters,key,value); 505 rewriteQS(); 506 } 507 508 @Override 509 public String getParameter(String key) { 510 return ReqRspUtil.get(parameters,key); 511 } 512 513 @Override 514 public String[] getParameterValues(String key) { 515 ArrayList list=new ArrayList(); 516 for(int i=0;i<parameters.length;i++) { 517 if(parameters[i].getName().equalsIgnoreCase(key)) 518 list.add(Caster.toString(parameters[i].getValue(),null)); 519 } 520 return (String[]) list.toArray(new String[list.size()]); 521 } 522 523 @Override 524 public Enumeration getParameterNames() { 525 HashSet set=new HashSet(); 526 for(int i=0;i<parameters.length;i++) { 527 set.add(parameters[i].getName()); 528 } 529 return new EnumerationWrapper(set); 530 } 531 532 @Override 533 public Map getParameterMap() { 534 Map p=new HashTable(); 535 for(int i=0;i<parameters.length;i++) { 536 p.put(parameters[i].getName(), parameters[i].getValue()); 537 } 538 return p; 539 } 540 541 /** 542 * set the Protocol (Default "http") 543 * @param protocol 544 */ 545 public void setProtocol(String protocol) { 546 this.protocol=protocol; 547 } 548 549 @Override 550 public String getProtocol() { 551 return protocol; 552 } 553 554 @Override 555 public String getScheme() { 556 return scheme; 557 } 558 559 public void setScheme(String scheme) { 560 this.scheme=scheme; 561 } 562 563 @Override 564 public String getServerName() { 565 return serverName; 566 } 567 568 @Override 569 public int getServerPort() { 570 return port; 571 } 572 573 /** 574 * @param port The port to set. 575 */ 576 public void setServerPort(int port) { 577 this.port = port; 578 } 579 580 @Override 581 public BufferedReader getReader() throws IOException { 582 return IOUtil.toBufferedReader(IOUtil.getReader(getInputStream(),"ISO-8859-1")); 583 } 584 585 @Override 586 public String getRemoteAddr() { 587 return remoteAddr; 588 } 589 590 public void setRemoteAddr(String remoteAddr) { 591 this.remoteAddr=remoteAddr; 592 } 593 public void setRemoteHost(String remoteHost) { 594 this.remoteHost=remoteHost; 595 } 596 597 @Override 598 public String getRemoteHost() { 599 return remoteHost; 600 } 601 602 public void setRemoteInetAddress(InetAddress ia) { 603 setRemoteAddr(ia.getHostAddress()); 604 setRemoteHost(ia.getHostName()); 605 } 606 607 @Override 608 public Locale getLocale() { 609 return locale; 610 } 611 612 public void setLocale(Locale locale) { 613 this.locale=locale; 614 } 615 616 @Override 617 public Enumeration getLocales() { 618 return new EnumerationWrapper(Locale.getAvailableLocales()); 619 } 620 621 @Override 622 public boolean isSecure() { 623 return secure; 624 } 625 626 public void setSecure(boolean secure) { 627 this.secure=secure; 628 } 629 630 @Override 631 public RequestDispatcher getRequestDispatcher(String arg0) { 632 return new RequestDispatcherDummy(this); 633 } 634 635 @Override 636 public String getRealPath(String path) { 637 return contextRoot.getReal(path); 638 } 639 640 /** 641 * @return the inputData 642 */ 643 public byte[] getInputData() { 644 return inputData; 645 } 646 /** 647 * @param inputData the inputData to set 648 */ 649 public void setInputData(byte[] inputData) { 650 this.inputData = inputData; 651 } 652 653 private void rewriteQS() { 654 StringBuffer qs=new StringBuffer(); 655 Pair p; 656 for(int i=0;i<parameters.length;i++) { 657 if(i>0) qs.append('&'); 658 p=parameters[i]; 659 qs.append(p.getName()); 660 qs.append('='); 661 qs.append(Caster.toString(p.getValue(),"")); 662 } 663 queryString=qs.toString(); 664 } 665 public void setSession(HttpSession session) { 666 this.session=session; 667 } 668 public static HttpServletRequestDummy clone(Config config,Resource rootDirectory,HttpServletRequest req) { 669 670 HttpServletRequestDummy dest = new HttpServletRequestDummy( 671 rootDirectory, 672 req.getServerName(), 673 req.getRequestURI(), 674 req.getQueryString(), 675 HttpUtil.cloneCookies(config,req), 676 HttpUtil.cloneHeaders(req), 677 HttpUtil.cloneParameters(req), 678 HttpUtil.getAttributesAsStruct(req), 679 getSessionEL(req) 680 ); 681 682 683 try { 684 dest.setCharacterEncoding(req.getCharacterEncoding()); 685 } catch (Exception e) { 686 687 } 688 689 dest.setRemoteAddr(req.getRemoteAddr()); 690 dest.setRemoteHost(req.getRemoteHost()); 691 dest.setAuthType(req.getAuthType()); 692 dest.setContentType(req.getContentType()); 693 dest.setContextPath(req.getContextPath()); 694 dest.setLocale(req.getLocale()); 695 dest.setMethod(req.getMethod()); 696 dest.setPathInfo(req.getPathInfo()); 697 dest.setProtocol(req.getProtocol()); 698 dest.setRequestedSessionId(req.getRequestedSessionId()); 699 dest.setScheme(req.getScheme()); 700 dest.setServerPort(req.getServerPort()); 701 dest.setSession(getSessionEL(req)); 702 return dest; 703 } 704 private static HttpSession getSessionEL(HttpServletRequest req) { 705 try{ 706 return req.getSession(); 707 } 708 catch(Throwable t){} 709 return null; 710 } 711 public void setAttributes(Struct attributes) { 712 this.attributes=attributes; 713 } 714 715 }