1 19 20 24 25 package org.netbeans.modules.web.monitor.server; 26 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Stack ; 33 import java.util.Vector ; 34 import javax.servlet.http.Cookie ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletRequestWrapper ; 37 import javax.servlet.http.HttpUtils ; 38 import org.netbeans.modules.web.monitor.data.RequestData; 39 import org.netbeans.modules.web.monitor.data.Param; 40 41 46 public class MonitorRequestWrapper extends HttpServletRequestWrapper { 47 48 private boolean replay = false; 49 50 private String localMethod = null; 52 private String localProtocol = null; 53 private String localScheme = null; 54 private String localRemoteAddr = null; 55 private String localQueryString = null; 56 private Param[] localHeaders = null; 57 private Vector localCookies = null; 58 private Map oldParams = null; 59 private Map localParams = null; 60 private Stack extraParamStack = null; 61 62 public final static String JSESSIONID = "JSESSIONID"; public final static String REPLACED = 66 "netbeans.replay.session-replaced"; 68 private static final boolean debug = false; 69 70 MonitorRequestWrapper(HttpServletRequest req) { 71 super(req); 72 } 73 74 80 private HttpServletRequest getHRequest() { 81 return (HttpServletRequest )getRequest(); 82 } 83 84 89 90 95 public String getMethod() { 96 if (replay) { 97 return localMethod; 98 } 99 return getHRequest().getMethod(); 100 } 101 102 108 118 public java.util.Map getParameterMap() { 119 120 if(debug) log("getParameterMap()"); 122 if (!replay) return getRequest().getParameterMap(); 123 124 if(extraParamStack == null || extraParamStack.empty()) 131 return (java.util.Map )localParams; 132 133 Map map = (Map )extraParamStack.peek(); 134 135 if(map.size() == 0) return (java.util.Map )localParams; 136 137 Hashtable ht = new Hashtable (); 138 139 Iterator keys = localParams.keySet().iterator(); 140 while(keys.hasNext()) { 141 Object o = keys.next(); 142 if(map.containsKey(o)) { 143 String [] vals0 = (String [])localParams.get(o); 144 String [] vals1 = (String [])map.get(o); 145 String [] vals2 = new String [vals0.length + vals1.length]; 146 System.arraycopy(vals0, 0, vals2, 0, vals0.length); 147 System.arraycopy(vals1, 0, vals2, vals0.length, vals1.length); 148 ht.put(o, vals2); 149 } 150 else 151 ht.put(o, localParams.get(o)); 152 } 153 154 keys = map.keySet().iterator(); 155 while(keys.hasNext()) { 156 Object o = keys.next(); 157 if(localParams.containsKey(o)) continue; 158 ht.put(o, map.get(o)); 159 } 160 return (Map )ht; 161 } 162 163 164 170 public String getParameter(String key) { 171 172 if(debug) log("getParameters()"); 174 if (!replay) return getRequest().getParameter(key); 175 176 String [] values = (String [])getParameterMap().get(key); 177 if (values != null && values.length > 0) { 178 return values[0]; 179 } 180 return null; 181 } 182 183 189 public Enumeration getParameterNames() { 190 if(debug) log("getParameterNames"); if (!replay) 192 return getRequest().getParameterNames(); 193 if(debug) { 194 Enumeration e = new Vector (getParameterMap().keySet()).elements(); 195 while(e.hasMoreElements()) 196 log("\t" + String.valueOf(e.nextElement())); } 198 return new Vector (getParameterMap().keySet()).elements(); 199 } 200 201 207 public String [] getParameterValues(String name) { 208 if(debug) log("getParameterValues"); if (!replay) 210 return getRequest().getParameterValues(name); 211 return (String [])getParameterMap().get(name); 212 } 213 214 215 220 public String getQueryString() { 221 if (!replay) 222 return getHRequest().getQueryString(); 223 return localQueryString; 224 } 225 226 231 public String getProtocol() { 232 if (!replay) 233 return getRequest().getProtocol(); 234 return localProtocol; 235 } 236 237 238 243 public String getScheme() { 244 if (replay) return localScheme; 245 return getRequest().getScheme(); 246 } 247 248 256 public String getHeader(String key) { 257 258 if (replay) { 259 int len = localHeaders.length; 260 for(int i=0; i<len; ++i) { 261 if(localHeaders[i].getName().equalsIgnoreCase(key)) 262 return localHeaders[i].getValue(); 263 } 264 if(debug) log("didn't find header"); return null; 266 } 267 268 if(debug) { 269 log("Headers not set locally"); log(key + " " + getHRequest().getHeader(key)); } 272 return getHRequest().getHeader(key); 273 } 274 275 280 public Enumeration getHeaderNames() { 281 282 if (replay) { 283 Vector v = new Vector (); 284 285 int len = localHeaders.length; 286 for(int i=0; i<len; ++i) 287 v.add(localHeaders[i].getName()); 288 289 return v.elements(); } 291 return getHRequest().getHeaderNames(); 292 } 293 294 299 public Enumeration getHeaders(String name) { 300 301 if (replay) { 302 303 Vector v = new Vector (); 304 305 int len = localHeaders.length; 306 for(int i=0; i<len; ++i) { 307 if(localHeaders[i].getName().equalsIgnoreCase(name)) 308 v.add(localHeaders[i].getValue()); 309 } 310 return v.elements(); } 312 return getHRequest().getHeaders(name); 313 } 314 315 320 public int getIntHeader(String name) { 321 int headerValue = -1; 322 String value = getHeader(name); 323 if (value != null) 324 headerValue = Integer.parseInt(value); 325 326 return headerValue; 327 } 328 329 334 public long getDateHeader (String name) { 335 336 long dateValue = -1; 337 String value = getHeader(name); 338 339 if (value != null) { 340 int el = value.indexOf (';'); 341 if (el != -1) 342 value = value.substring (0, el); 343 344 try { 345 dateValue = Date.parse (value); 346 } catch (Exception e) { 347 } 348 if (dateValue == -1) { 349 throw new IllegalArgumentException (); 351 } 352 } 353 return dateValue; 354 } 355 356 357 362 public Cookie [] getCookies() { 363 if(!replay) 364 return getHRequest().getCookies(); 365 366 if(localCookies == null) 367 return new Cookie [0]; 368 369 int numCookies = localCookies.size(); 370 Cookie [] cookieArray = new Cookie [numCookies]; 371 Enumeration e = localCookies.elements(); 372 int index = 0; 373 while(e.hasMoreElements()) { 374 cookieArray[index] = (Cookie )e.nextElement(); 375 ++index; 376 } 377 return cookieArray; 378 } 379 380 389 public Enumeration getAttributeNames() { 390 391 if(debug) log("getAttributeNames()"); 393 Enumeration e = getRequest().getAttributeNames(); 394 395 if(debug) return e; 398 399 Vector v = new Vector (); 400 while (e.hasMoreElements()) { 401 String name = (String )e.nextElement(); 402 if(name.startsWith(MonitorFilter.PREFIX)) { 405 if(debug) log("discarded " + name); continue; 407 } 408 if(debug) log("keeping " + name); v.add(name); 410 } 411 return v.elements(); 412 } 413 414 public String getRemoteAddr() { 416 return getRequest().getRemoteAddr(); 417 423 } 424 425 427 428 438 public void populate(RequestData rd, 439 boolean replaceSessionID) { 440 441 replay = true; 443 444 localMethod = rd.getAttributeValue("method"); 447 localProtocol = rd.getAttributeValue("protocol"); 450 localScheme = rd.getAttributeValue("scheme"); 453 457 localQueryString = rd.getAttributeValue("queryString"); 460 oldParams = getRequest().getParameterMap(); 461 462 if(localMethod.equals("GET")) { 465 try { 466 localParams = HttpUtils.parseQueryString(localQueryString); 467 } 468 catch(Exception ex) { 469 localParams = new Hashtable (); 472 } 473 } 474 475 else if(localMethod.equals("POST")) { 477 try { 478 localParams = HttpUtils.parseQueryString(localQueryString); 479 } 480 catch(Exception ex) { 481 localParams = new Hashtable (); 484 } 485 486 492 Param[] params = rd.getParam(); 493 int numParams = params.length; 494 495 for(int i=0; i<numParams; ++i) { 496 String name = params[i].getAttributeValue("name"); String [] values = null; 498 if(localParams.containsKey(name)) { 499 values = (String [])localParams.get(name); 500 String [] newvals = new String [values.length+1]; 501 int j; 502 for(j=0; j<values.length; ++j) 503 newvals[j] = values[j]; 504 newvals[j] = params[i].getAttributeValue("value"); localParams.put(name, newvals); 506 } 507 else { 508 values = new String [1]; 509 values[0] = params[i].getAttributeValue("value"); localParams.put(name, values); 511 } 512 } 513 } 514 else if(localMethod.equals("PUT")) { 516 localParams = new Hashtable (); 517 518 } 522 523 else { 524 localParams = new Hashtable (); 527 } 528 529 if(debug) { 531 log("CookieString from real req: " + String.valueOf(getHRequest().getHeader("cookie"))); log("CookieString from rd: " + String.valueOf(rd.getCookieString())); 535 536 } 537 538 StringBuffer cookieBuf = new StringBuffer (); 540 541 int numHeaders = rd.getHeaders().sizeParam(); 546 localHeaders = new Param[numHeaders]; 547 for(int i=0; i<numHeaders; ++i) 548 localHeaders[i] = rd.getHeaders().getParam(i); 549 550 if(debug) { 551 log("How many parameters do we have?"); log("param length is " + String.valueOf(localHeaders.length)); for(int i=0; i<localHeaders.length; ++i) 554 log(localHeaders[i].getName() + " " + localHeaders[i].getValue()); 556 } 557 558 localCookies = new Vector (); 561 562 String idFromRequest = null; 564 565 Param[] myCookies = rd.getCookiesAsParams(); 567 568 if(myCookies != null && myCookies.length > 0) { 570 571 if(debug) log("Now adding cookies"); 573 String ckname = null, ckvalue=null; 574 for(int i=0; i<myCookies.length; ++i) { 575 576 ckname = myCookies[i].getAttributeValue("name"); ckvalue = myCookies[i].getAttributeValue("value"); 579 if(ckname.equalsIgnoreCase(JSESSIONID)) { 582 idFromRequest = ckvalue; 583 continue; 584 } 585 localCookies.add(new Cookie (ckname, ckvalue)); 586 if(debug) log("Added " + ckname + "=" + ckvalue); 588 if(cookieBuf.length() > 0) cookieBuf.append("; "); cookieBuf.append(ckname); 590 cookieBuf.append("="); cookieBuf.append(ckvalue); 592 } 593 } 594 595 599 boolean sessionReplaced = false; 600 try { 601 String value = (String )getHRequest().getAttribute(REPLACED); 602 if(value.equals("true")) sessionReplaced = true; if(debug) log("replaced the session"); } 605 catch(Exception ex) { 606 if(debug) log("didn't replace the session"); } 608 609 if(sessionReplaced) { 610 611 617 if(idFromRequest != null) { 618 619 localCookies.add(new Cookie (JSESSIONID, idFromRequest)); 620 if(cookieBuf.length() > 0) cookieBuf.append("; "); cookieBuf.append(JSESSIONID); 622 cookieBuf.append("="); cookieBuf.append(idFromRequest); 624 } 625 } 627 else { 628 629 633 637 if(debug) log("Old request is " + getHRequest().toString()); 639 Cookie [] ck = getHRequest().getCookies(); 640 641 if(debug) log("Got the incoming cookies"); 643 if(ck != null && ck.length > 0) { 644 for(int i=0; i<ck.length; ++i) { 645 if(ck[i].getName().equals(JSESSIONID)) { 646 localCookies.add(ck[i]); 647 if(cookieBuf.length() > 0) cookieBuf.append("; "); cookieBuf.append(JSESSIONID); 649 cookieBuf.append("="); cookieBuf.append(ck[i].getValue()); 651 } 652 } 653 } 654 } 657 String cookieStr = cookieBuf.toString(); 658 if(cookieStr.equals("")) { if(debug) log("No cookies, deleting cookie header"); removeHeader("cookie"); } 662 else { 663 if(debug) log("Setting cookie header to " + cookieBuf.toString()); 665 setHeader("cookie", cookieBuf.toString()); } 667 } 668 669 670 674 675 695 void pushExtraParameters() { 696 697 if(!replay) return; 698 699 if(debug) log("pushExtraParameters"); 701 712 Map extraParams = new Hashtable (); 713 714 Map currentMap = getRequest().getParameterMap(); 715 Iterator keys = currentMap.keySet().iterator(); 716 717 while(keys.hasNext()) { 718 Object o = keys.next(); 719 if(debug) { 720 String [] value = (String [])currentMap.get(o); 722 StringBuffer buf = new StringBuffer (); 723 for(int k=0; k<value.length; ++k) { 724 buf.append(value[k]); 725 buf.append(" "); } 727 log("Value: " + buf.toString()); } 729 730 if(!oldParams.containsKey(o)) { 731 extraParams.put(o, currentMap.get(o)); 732 continue; 733 } 734 735 if(oldParams.get(o).equals(currentMap.get(o))) { 736 continue; 737 } 738 else { 739 extraParams.put(o, currentMap.get(o)); 740 } 741 } 742 if(extraParamStack == null) 743 extraParamStack = new Stack (); 744 745 extraParamStack.push(extraParams); 746 if(debug) 747 log("Param stack size: " + String.valueOf(extraParamStack.size())); } 749 750 757 void popExtraParameters() { 758 759 if(!replay) return; 760 761 if(debug) log("popExtraParameters"); if(extraParamStack == null || extraParamStack.empty()) { 763 log("ERROR - MonitorRequestWrapper empty param stack!"); return; 765 } 766 extraParamStack.pop(); 767 } 768 769 770 775 private void setHeader(String headerName, String headerValue) { 776 777 if(!replay) { 778 log("setHeader() must only be used from replay"); return; 780 } 781 782 boolean addedHeader = false; 783 784 if(debug) log("Headers were set locally"); for(int i=0; i<localHeaders.length; ++i) { 786 if(localHeaders[i].getName().equalsIgnoreCase(headerName)) { 787 localHeaders[i].setValue(headerValue); 788 addedHeader = true; 789 if(debug) log("Replaced existing header"); break; 791 } 792 } 793 if(addedHeader) return; 794 Param[] p = new Param[localHeaders.length + 1]; 795 int numHeaders = 0; 796 while(numHeaders < localHeaders.length) { 797 p[numHeaders] = localHeaders[numHeaders]; 798 ++numHeaders; 799 } 800 p[numHeaders] = new Param(headerName, headerValue); 801 localHeaders = p; 802 if(debug) log("Added new header"); return; 804 } 805 806 814 private void removeHeader(String headerName) { 815 816 if(!replay) { 817 log("removeHeader() must only be used from replay"); return; 819 } 820 821 if(debug) log("removeHeader()"); 823 Vector v = new Vector (); 824 825 for(int i=0; i<localHeaders.length; ++i) { 826 if(localHeaders[i].getName().equalsIgnoreCase(headerName)) 827 continue; 828 v.add(localHeaders[i]); 829 } 830 831 int size = v.size(); 832 localHeaders = new Param[size]; 833 for(int i=0; i< size; ++i) 834 localHeaders[i] = (Param)v.elementAt(i); 835 return; 836 } 837 838 839 840 843 public String toString() { 844 StringBuffer buf = new StringBuffer (); 845 buf.append("uri: "); buf.append(getRequestURI()); 847 buf.append("\n"); buf.append("method: "); buf.append(getMethod()); 850 buf.append("\n"); buf.append("QueryString: "); buf.append(getQueryString()); 853 buf.append("\n"); buf.append("Parameters:\n"); Enumeration e = getParameterNames(); 856 while(e.hasMoreElements()) { 857 String name = (String )e.nextElement(); 858 String value = getParameter(name); 859 buf.append("\tName: "); buf.append(name); 861 buf.append("\tValue: "); buf.append(value); 863 buf.append("\n"); } 865 866 buf.append("Headers:\n"); e = getHeaderNames(); 868 while(e.hasMoreElements()) { 869 String name = (String )e.nextElement(); 870 String value = getHeader(name); 871 buf.append("\tName: "); buf.append(name); 873 buf.append("\tValue: "); buf.append(value); 875 buf.append("\n"); } 877 return buf.toString(); 878 } 879 880 883 private void log(String s) { 884 System.out.println("MonitorRequestWrapper::" + s); } 886 887 } | Popular Tags |