1 19 20 package org.netbeans.modules.web.monitor.data; 21 22 import org.w3c.dom.*; 23 import org.netbeans.modules.schema2beans.*; 24 import java.beans.*; 25 import java.util.*; 26 import java.io.*; 27 28 public class RequestData extends BaseBean { 29 30 static Vector comparators = new Vector(); 31 32 static public final String PARAM = "Param"; static public final String HEADERS = "Headers"; static public final String REQUESTATTRIBUTESIN = 35 "RequestAttributesIn"; static public final String REQUESTATTRIBUTESOUT = 37 "RequestAttributesOut"; static public final String REQUESTDATA = "RequestData"; 40 public final static String JSESSIONID = "JSESSIONID"; public final static String COOKIE = "cookie"; static private final boolean debug = false; 43 44 45 public RequestData() { 46 this(Common.USE_DEFAULT_VALUES); 47 } 48 49 50 public RequestData(Node doc, int options) { 51 super(RequestData.comparators, new org.netbeans.modules.schema2beans.Version(1, 0, 6)); 52 if (doc == null) { 53 doc = GraphManager.createRootElementNode(REQUESTDATA); 54 55 if (doc == null) 56 throw new RuntimeException ("failed to create a new DOM root!"); } 58 Node n = GraphManager.getElementNode(REQUESTDATA, doc); 59 if (n == null) 60 throw new RuntimeException ("doc root not found in the DOM graph!"); 62 this.graphManager.setXmlDocument(doc); 63 64 this.createBean(n, this.graphManager()); 66 this.initialize(options); 67 } 68 69 public RequestData(int options) { 70 super(RequestData.comparators, new org.netbeans.modules.schema2beans.Version(1, 0, 6)); 71 73 this.createProperty("Headers", HEADERS, Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY, 75 Headers.class); 76 77 78 this.createProperty("RequestAttributesIn", REQUESTATTRIBUTESIN, Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY, 80 RequestAttributesIn.class); 81 82 83 this.createProperty("RequestAttributesOut", REQUESTATTRIBUTESOUT, Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY, 85 RequestAttributesOut.class); 86 87 88 this.createProperty("Param", PARAM, Common.TYPE_0_N | Common.TYPE_BEAN | Common.TYPE_KEY, 90 Param.class); 91 this.createAttribute(PARAM, "name", "Name", AttrProp.CDATA | AttrProp.REQUIRED, 93 null, null); 94 95 this.createAttribute(PARAM, "value", "Value", AttrProp.CDATA | AttrProp.IMPLIED, 97 null, null); 98 99 this.initialize(options); 100 } 101 102 void initialize(int options) { 104 105 } 106 107 public void setHeaders(Headers value) { 109 this.setValue(HEADERS, value); 110 } 111 112 public Headers getHeaders() { 114 return (Headers)this.getValue(HEADERS); 115 } 116 117 118 public void setRequestAttributesIn(RequestAttributesIn value) { 120 this.setValue(REQUESTATTRIBUTESIN, value); 121 } 122 123 public RequestAttributesIn getRequestAttributesIn() { 125 return (RequestAttributesIn)this.getValue(REQUESTATTRIBUTESIN); 126 } 127 128 129 public void setRequestAttributesOut(RequestAttributesOut value) { 131 this.setValue(REQUESTATTRIBUTESOUT, value); 132 } 133 134 public RequestAttributesOut getRequestAttributesOut() { 136 return (RequestAttributesOut)this.getValue(REQUESTATTRIBUTESOUT); 137 } 138 139 140 public void setParam(int index, Param value) 142 { 143 this.setValue(PARAM, index, value); 144 } 145 146 public Param getParam(int index) 148 { 149 return (Param)this.getValue(PARAM, index); 150 } 151 152 public void setParam(Param[] value) 154 { 155 if(debug) log("setParam(Param[] value)"); try { 157 this.setValue(PARAM, value); 158 } 159 catch(Exception ex) { 160 ex.printStackTrace(); 161 } 162 } 163 164 public Param[] getParam() 166 { 167 return (Param[])this.getValues(PARAM); 168 } 169 170 public int sizeParam() 172 { 173 return this.size(PARAM); 174 } 175 176 public int addParam(Param value) { 178 return this.addValue(PARAM, value); 179 } 180 181 public int removeParam(Param value) 186 { 187 return this.removeValue(PARAM, value); 188 } 189 190 191 192 193 public void setReplaceSessionCookie(boolean value) { 194 this.setAttributeValue("replace", String.valueOf(value)); } 196 197 public boolean getReplaceSessionCookie() { 198 try { 199 if(this.getAttributeValue("replace").equals("true")) return true; 201 } 202 catch(NullPointerException npe) { 203 } 205 return false; 206 } 207 208 209 public String getSessionID() { 210 return findSessionID(getCookieString()); 211 } 212 213 214 public String getCookieString() { 215 Param[] headers = getHeaders().getParam(); 216 StringBuffer cookieStr = new StringBuffer (); 217 int len = headers.length; 218 for(int j=0; j<len; ++j) { 219 if(headers[j].getName().equalsIgnoreCase(COOKIE)) { 220 cookieStr.append(headers[j].getValue()); 221 cookieStr.append(";"); 222 } 223 } 224 return cookieStr.toString(); 225 } 226 227 228 229 static public String findSessionID(String cookieStr) { 230 231 if(cookieStr == null || cookieStr.equals("")) return ""; 234 StringTokenizer tok = new StringTokenizer(cookieStr, 235 ";", false); 237 while (tok.hasMoreTokens()) { 238 239 String token = tok.nextToken(); 240 int i = token.indexOf("="); if (i > -1) { 242 243 String name = token.substring(0, i).trim(); 244 if(name.equals(JSESSIONID)) { 245 String value = token.substring(i+1, token.length()).trim(); 246 return value=stripQuote(value); 247 } 248 } 249 } 250 return ""; } 252 253 258 public Param[] getCookiesAsParams() { 259 260 String cookieStr = getCookieString(); 261 if(debug) log("cookie string is " + cookieStr); 262 263 if(cookieStr == null || cookieStr.equals("")) return new Param[0]; 265 266 Vector cookies = new Vector(); 267 268 StringTokenizer tok = new StringTokenizer(cookieStr, 269 ";", false); 271 while (tok.hasMoreTokens()) { 272 273 String token = tok.nextToken(); 274 if(debug) log("token is " + token); 275 int i = token.indexOf("="); if (i > -1) { 277 278 String name = token.substring(0, i).trim(); 279 String value = token.substring(i+1, token.length()).trim(); 280 value=stripQuote(value); 281 cookies.addElement(new Param(name, value)); 282 if(debug) log(name + "=" + value); 283 } 284 } 285 int numCookies = cookies.size(); 286 Param[] params = new Param[numCookies]; 287 for(int k=0; k<numCookies; ++k) 288 params[k] = (Param)cookies.elementAt(k); 289 290 return params; 291 } 292 293 301 302 public int addCookie(String ckname, String ckvalue) { 303 304 if(debug) 306 log("Adding cookie: " + ckname + " " + ckvalue); 308 StringBuffer buf = new StringBuffer (); 310 311 Param[] headers = getHeaders().getParam(); 312 if(headers == null) headers = new Param[0]; 313 314 int len = headers.length; 315 316 if(len == 0) { 319 if(debug) log("We had a cookie header with no value"); 320 buf.append(ckname); 321 buf.append("="); buf.append(ckvalue); 323 if(debug) log("New cookie string is " + buf.toString()); setCookieHeader(buf.toString()); 325 return 1; 326 } 327 328 for(int i=0; i<len; ++i) { 329 if(!headers[i].getName().equalsIgnoreCase(COOKIE)) 330 continue; 331 332 if(debug) log("Found cookie header"); 333 String oldCookies = headers[i].getValue(); 334 335 if(oldCookies != null && !oldCookies.trim().equals("")) { buf.append(oldCookies.trim()); 337 buf.append(";"); } 339 340 if(debug) log("appended ; to cookie string"); 341 buf.append(ckname); 342 buf.append("="); buf.append(ckvalue); 344 headers[i].setValue(buf.toString()); 345 if(debug) log("New cookie string is " + buf.toString()); return 1; 347 } 348 349 if(debug) log("We had no cookie header"); 351 buf.append(ckname); 352 buf.append("="); buf.append(ckvalue); 354 if(debug) log("New cookie string is " + buf.toString()); setCookieHeader(buf.toString()); 356 return 0; 357 } 358 359 360 364 public int addCookie(String ckstr) { 365 int index = ckstr.indexOf("="); 366 if(index == -1) return addCookie(ckstr, ""); 367 else if(index == ckstr.length()-1) return addCookie(ckstr, ""); 368 return addCookie(ckstr.substring(0,index), ckstr.substring(index+1)); 369 } 370 371 public void setCookieHeader(String cookies) { 372 373 374 Param[] headers = getHeaders().getParam(); 375 if(headers == null) headers = new Param[0]; 376 377 int len = headers.length; 378 for(int i=0; i<len; ++i) { 379 if(!headers[i].getName().equalsIgnoreCase(COOKIE)) 380 continue; 381 382 headers[i].setValue(cookies); 383 return; 384 } 385 386 Param p = new Param(COOKIE, cookies); 388 getHeaders().addParam(p); 389 } 390 391 392 public void deleteCookie(String ckname, String ckvalue) { 393 394 if(debug) log("Deleting cookie: " + ckname + " " + ckvalue); 396 Param[] headers = getHeaders().getParam(); 397 boolean noCookie = false; 398 399 if(headers == null || headers.length == 0) return; 401 402 int len = headers.length; 403 for(int i=0; i<len; ++i) { 404 405 if(!headers[i].getName().equalsIgnoreCase(COOKIE)) 406 continue; 407 408 if(debug) log(" found cookie header"); 410 String oldCookies = headers[i].getValue(); 411 412 if(oldCookies == null || oldCookies.trim().equals("")) { if(debug) log(" no cookies!"); return; 415 } 416 417 if(debug) log(" old cookie string is " + oldCookies); 419 StringBuffer buf = new StringBuffer (); 420 StringTokenizer tok = 421 new StringTokenizer(headers[i].getValue(), 422 ";", false); 424 while (tok.hasMoreTokens()) { 425 426 String token = tok.nextToken(); 427 int j = token.indexOf("="); if (j > -1) { 429 430 String name = token.substring(0, j).trim(); 431 String value = token.substring(j+1, token.length()).trim(); 432 value=stripQuote(value); 433 434 if(debug) log("Processing cookie: " + name + " " + value); 437 if(name.equalsIgnoreCase(ckname) && value.equalsIgnoreCase(ckvalue)) 438 continue; 439 else { 440 if(debug) log("Keep this cookie"); buf.append(name); 442 buf.append("="); buf.append(value); 444 buf.append(";"); } 446 } 447 448 if(debug) log("New cookie string is: " + buf.toString()); 450 } 451 if(buf.toString().equals("")) getHeaders().removeParam(headers[i]); 452 else headers[i].setValue(buf.toString()); 453 break; 454 } 455 } 458 459 public void deleteCookie(String ckname) { 460 461 if(debug) log("Deleting cookie: " + ckname); 463 Param[] headers = getHeaders().getParam(); 464 if(headers == null || headers.length == 0) { 466 if(debug) log("No headers"); return; 468 } 469 int len = headers.length; 470 for(int i=0; i<len; ++i) { 471 472 if(debug) log("Examining header " + headers[i].getName()); 474 if(!headers[i].getName().equalsIgnoreCase(COOKIE)) 475 continue; 476 477 String oldCookies = headers[i].getValue(); 478 479 if(oldCookies == null || oldCookies.trim().equals("")) { if(debug) log("No cookies"); return; 482 } 483 484 StringBuffer buf = new StringBuffer (); 485 StringTokenizer tok = new StringTokenizer(headers[i].getValue(), 486 ";", false); 488 while (tok.hasMoreTokens()) { 489 490 String token = tok.nextToken(); 491 int j = token.indexOf("="); if (j > -1) { 493 494 String name = token.substring(0, j).trim(); 495 if(name.equalsIgnoreCase(ckname)) continue; 496 else { 497 if(debug) log("Keep this cookie"); String value = 499 token.substring(j+1, token.length()).trim(); 500 value=stripQuote(value); 501 buf.append(name); 502 buf.append("="); buf.append(value); 504 buf.append(";"); } 506 } 507 } 508 509 if(debug) 510 log("New cookie string is: " + buf.toString()); 512 if(buf.toString().equals("")) getHeaders().removeParam(headers[i]); 513 else headers[i].setValue(buf.toString()); 514 return; 515 } 516 } 519 520 521 525 public static String stripQuote( String value ) { 526 527 if (((value.startsWith("\"")) && (value.endsWith("\""))) || ((value.startsWith("'") && (value.endsWith("'"))))) { try { 530 return value.substring(1,value.length()-1); 531 } catch (Exception ex) { 532 } 533 } 534 return value; 535 } 536 537 public boolean verify() 539 { 540 return true; 541 } 542 543 static public void addComparator(BeanComparator c) 545 { 546 RequestData.comparators.add(c); 547 } 548 549 static public void removeComparator(BeanComparator c) 551 { 552 RequestData.comparators.remove(c); 553 } 554 public void addPropertyChangeListener(PropertyChangeListener l) 556 { 557 BeanProp p = this.beanProp(); 558 if (p != null) 559 p.addPCListener(l); 560 } 561 562 public void removePropertyChangeListener(PropertyChangeListener l) 564 { 565 BeanProp p = this.beanProp(); 566 if (p != null) 567 p.removePCListener(l); 568 } 569 570 public void addPropertyChangeListener(String n, PropertyChangeListener l) 572 { 573 BeanProp p = this.beanProp(n); 574 if (p != null) 575 p.addPCListener(l); 576 } 577 578 public void removePropertyChangeListener(String n, PropertyChangeListener l) 580 { 581 BeanProp p = this.beanProp(n); 582 if (p != null) 583 p.removePCListener(l); 584 } 585 586 public void dump(StringBuffer str, String indent) 588 { 589 String s; 590 BaseBean n; 591 592 str.append(indent); 593 str.append("Headers"); n = this.getHeaders(); 595 if (n != null) 596 n.dump(str, indent + "\t"); else 598 str.append(indent+"\tnull"); this.dumpAttributes(HEADERS, 0, str, indent); 600 601 str.append(indent); 602 str.append("Param["+this.sizeParam()+"]"); for(int i=0; i<this.sizeParam(); i++) 604 { 605 str.append(indent+"\t"); str.append("#"+i+":"); n = this.getParam(i); 608 if (n != null) 609 n.dump(str, indent + "\t"); else 611 str.append(indent+"\tnull"); this.dumpAttributes(PARAM, i, str, indent); 613 } 614 615 } 616 617 public String dumpBeanNode() { 618 StringBuffer str = new StringBuffer (); 619 str.append("RequestData\n"); this.dump(str, "\n "); return str.toString(); 622 } 623 624 public static RequestData createGraph(Node doc) { 629 return new RequestData(doc, Common.NO_DEFAULT_VALUES); 630 } 631 632 public static RequestData createGraph(InputStream in) { 633 return RequestData.createGraph(in, false); 634 } 635 636 public static RequestData createGraph(InputStream in, boolean validate) { 637 try { 638 Document doc = GraphManager.createXmlDocument(in, validate); 639 return RequestData.createGraph(doc); 640 } 641 catch (Throwable t) { 642 throw new RuntimeException ("DOM graph creation failed: "+ t.getMessage()); 644 } 645 } 646 647 public static RequestData createGraph() { 651 return new RequestData(); 652 } 653 654 public void log(String s) { 655 System.out.println("RequestData::" + s); } 657 658 } 659 660 | Popular Tags |