KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > data > RequestData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc PARAM = "Param"; //NOI18N
33
static public final String JavaDoc HEADERS = "Headers"; //NOI18N
34
static public final String JavaDoc REQUESTATTRIBUTESIN =
35     "RequestAttributesIn"; //NOI18N
36
static public final String JavaDoc REQUESTATTRIBUTESOUT =
37     "RequestAttributesOut"; //NOI18N
38
static public final String JavaDoc REQUESTDATA = "RequestData"; //NOI18N
39

40     public final static String JavaDoc JSESSIONID = "JSESSIONID"; // NOI18N
41
public final static String JavaDoc COOKIE = "cookie"; // NOI18N
42
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 JavaDoc("failed to create a new DOM root!"); //NOI18N
57
}
58     Node n = GraphManager.getElementNode(REQUESTDATA, doc);
59     if (n == null)
60         throw new RuntimeException JavaDoc("doc root not found in the DOM graph!"); //NOI18N
61

62     this.graphManager.setXmlDocument(doc);
63
64     // Entry point of the createBeans() recursive calls
65
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     // Properties (see root bean comments for the bean graph)
72

73     this.createProperty("Headers", HEADERS, //NOI18N
74
Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
75                 Headers.class);
76
77
78     this.createProperty("RequestAttributesIn", REQUESTATTRIBUTESIN, //NOI18N
79
Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
80                 RequestAttributesIn.class);
81
82
83     this.createProperty("RequestAttributesOut", REQUESTATTRIBUTESOUT, //NOI18N
84
Common.TYPE_1 | Common.TYPE_BEAN | Common.TYPE_KEY,
85                 RequestAttributesOut.class);
86
87
88     this.createProperty("Param", PARAM, //NOI18N
89
Common.TYPE_0_N | Common.TYPE_BEAN | Common.TYPE_KEY,
90                 Param.class);
91     this.createAttribute(PARAM, "name", "Name", //NOI18N
92
AttrProp.CDATA | AttrProp.REQUIRED,
93                  null, null);
94
95     this.createAttribute(PARAM, "value", "Value", //NOI18N
96
AttrProp.CDATA | AttrProp.IMPLIED,
97                  null, null);
98
99     this.initialize(options);
100     }
101
102     // Setting the default values of the properties
103
void initialize(int options) {
104
105     }
106
107     // This attribute is mandatory
108
public void setHeaders(Headers value) {
109     this.setValue(HEADERS, value);
110     }
111
112     //
113
public Headers getHeaders() {
114     return (Headers)this.getValue(HEADERS);
115     }
116
117
118     // This attribute is mandatory
119
public void setRequestAttributesIn(RequestAttributesIn value) {
120     this.setValue(REQUESTATTRIBUTESIN, value);
121     }
122
123     //
124
public RequestAttributesIn getRequestAttributesIn() {
125     return (RequestAttributesIn)this.getValue(REQUESTATTRIBUTESIN);
126     }
127
128
129     // This attribute is mandatory
130
public void setRequestAttributesOut(RequestAttributesOut value) {
131     this.setValue(REQUESTATTRIBUTESOUT, value);
132     }
133
134     //
135
public RequestAttributesOut getRequestAttributesOut() {
136     return (RequestAttributesOut)this.getValue(REQUESTATTRIBUTESOUT);
137     }
138
139
140     // This attribute is an array, possibly empty
141
public void setParam(int index, Param value)
142     {
143     this.setValue(PARAM, index, value);
144     }
145
146     //
147
public Param getParam(int index)
148     {
149     return (Param)this.getValue(PARAM, index);
150     }
151
152     // This attribute is an array, possibly empty
153
public void setParam(Param[] value)
154     {
155     if(debug) log("setParam(Param[] value)"); //NOI18N
156
try {
157         this.setValue(PARAM, value);
158     }
159     catch(Exception JavaDoc ex) {
160         ex.printStackTrace();
161     }
162     }
163
164     //
165
public Param[] getParam()
166     {
167     return (Param[])this.getValues(PARAM);
168     }
169
170     // Return the number of properties
171
public int sizeParam()
172     {
173     return this.size(PARAM);
174     }
175
176     // Add a new element returning its index in the list
177
public int addParam(Param value) {
178     return this.addValue(PARAM, value);
179     }
180
181     //
182
// Remove an element using its reference
183
// Returns the index the element had in the list
184
//
185
public int removeParam(Param value)
186     {
187     return this.removeValue(PARAM, value);
188     }
189
190
191     /* Methods for manipulating the session cookie */
192
193     public void setReplaceSessionCookie(boolean value) {
194     this.setAttributeValue("replace", String.valueOf(value)); // NOI18N
195
}
196
197     public boolean getReplaceSessionCookie() {
198     try {
199         if(this.getAttributeValue("replace").equals("true")) // NOI18N
200
return true;
201     }
202     catch(NullPointerException JavaDoc npe) {
203         // do nothing
204
}
205     return false;
206     }
207
208
209     public String JavaDoc getSessionID() {
210     return findSessionID(getCookieString());
211     }
212     
213
214     public String JavaDoc getCookieString() {
215     Param[] headers = getHeaders().getParam();
216     StringBuffer JavaDoc cookieStr = new StringBuffer JavaDoc();
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 JavaDoc findSessionID(String JavaDoc cookieStr) {
230
231     if(cookieStr == null || cookieStr.equals("")) //NOI18N
232
return ""; //NOI18N
233

234     StringTokenizer tok = new StringTokenizer(cookieStr,
235                           ";", false); // NOI18N
236

237     while (tok.hasMoreTokens()) {
238         
239         String JavaDoc token = tok.nextToken();
240         int i = token.indexOf("="); // NOI18N
241
if (i > -1) {
242             
243         String JavaDoc name = token.substring(0, i).trim();
244         if(name.equals(JSESSIONID)) {
245             String JavaDoc value = token.substring(i+1, token.length()).trim();
246             return value=stripQuote(value);
247         }
248         }
249     }
250     return ""; //NOI18N
251
}
252     
253     /**
254      * Gets the cookies as an array of Param from the cookie string in
255      * the header. If there is no cookie header we return an empty
256      * array.
257      */

258     public Param[] getCookiesAsParams() {
259
260     String JavaDoc cookieStr = getCookieString();
261     if(debug) log("cookie string is " + cookieStr);
262
263     if(cookieStr == null || cookieStr.equals("")) //NOI18N
264
return new Param[0];
265         
266     Vector cookies = new Vector();
267         
268     StringTokenizer tok = new StringTokenizer(cookieStr,
269                           ";", false); // NOI18N
270

271     while (tok.hasMoreTokens()) {
272         
273         String JavaDoc token = tok.nextToken();
274         if(debug) log("token is " + token);
275         int i = token.indexOf("="); // NOI18N
276
if (i > -1) {
277
278         String JavaDoc name = token.substring(0, i).trim();
279         String JavaDoc 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     /**
294      * This method is used by EditPanelCookies to add cookies to the
295      * request data prior to a replay. We add the cookie to the
296      * CookiesIn array, as well as adding the corresponding string to
297      * the header. It is the latter that is used for the replay - the
298      * former is only for display purposes while the user is modifying
299      * the request.
300      */

301      
302     public int addCookie(String JavaDoc ckname, String JavaDoc ckvalue) {
303
304         // Do we have to check for duplicates?
305
if(debug)
306         log("Adding cookie: " + ckname + " " + ckvalue); //NOI18N
307

308     // Holds the cookie header
309
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
310     
311     Param[] headers = getHeaders().getParam();
312     if(headers == null) headers = new Param[0];
313
314     int len = headers.length;
315
316     // No headers (this should not happen)
317
// Create a set of headers and add a cookie header
318
if(len == 0) {
319         if(debug) log("We had a cookie header with no value");
320         buf.append(ckname);
321         buf.append("="); //NOI18N
322
buf.append(ckvalue);
323         if(debug) log("New cookie string is " + buf.toString()); //NOI18N
324
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 JavaDoc oldCookies = headers[i].getValue();
334
335         if(oldCookies != null && !oldCookies.trim().equals("")) { //NOI18N
336
buf.append(oldCookies.trim());
337         buf.append(";"); //NOI18N
338
}
339         
340         if(debug) log("appended ; to cookie string");
341         buf.append(ckname);
342         buf.append("=");//NOI18N
343
buf.append(ckvalue);
344         headers[i].setValue(buf.toString());
345         if(debug) log("New cookie string is " + buf.toString()); //NOI18N
346
return 1;
347     }
348      
349     // There were no cookies, create a new header
350
if(debug) log("We had no cookie header");
351     buf.append(ckname);
352     buf.append("="); //NOI18N
353
buf.append(ckvalue);
354     if(debug) log("New cookie string is " + buf.toString()); //NOI18N
355
setCookieHeader(buf.toString());
356     return 0;
357     }
358
359
360     /**
361      * This method is used by EditPanelHeader in case the added header
362      * turns out to be a cookie.
363      */

364     public int addCookie(String JavaDoc 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 JavaDoc 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     // We didn't find a cookie header - create one
387
Param p = new Param(COOKIE, cookies);
388     getHeaders().addParam(p);
389     }
390
391
392     public void deleteCookie(String JavaDoc ckname, String JavaDoc ckvalue) {
393
394     if(debug) log("Deleting cookie: " + ckname + " " + ckvalue); //NOI18N
395

396     Param[] headers = getHeaders().getParam();
397     boolean noCookie = false;
398
399     // No headers (this should not happen)
400
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");//NOI18N
409

410         String JavaDoc oldCookies = headers[i].getValue();
411         
412         if(oldCookies == null || oldCookies.trim().equals("")) { //NOI18N
413
if(debug) log(" no cookies!");//NOI18N
414
return;
415         }
416
417         if(debug) log(" old cookie string is " + oldCookies);//NOI18N
418

419         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
420         StringTokenizer tok =
421         new StringTokenizer(headers[i].getValue(),
422                     ";", false); // NOI18N
423

424         while (tok.hasMoreTokens()) {
425             
426         String JavaDoc token = tok.nextToken();
427         int j = token.indexOf("="); // NOI18N
428
if (j > -1) {
429
430             String JavaDoc name = token.substring(0, j).trim();
431             String JavaDoc value = token.substring(j+1, token.length()).trim();
432             value=stripQuote(value);
433
434             if(debug) log("Processing cookie: " + //NOI18N
435
name + " " + value); //NOI18N
436

437             if(name.equalsIgnoreCase(ckname) && value.equalsIgnoreCase(ckvalue))
438             continue;
439             else {
440             if(debug) log("Keep this cookie"); //NOI18N
441
buf.append(name);
442             buf.append("=");//NOI18N
443
buf.append(value);
444             buf.append(";"); //NOI18N
445
}
446         }
447             
448         if(debug) log("New cookie string is: " + //NOI18N
449
buf.toString());
450         }
451         if(buf.toString().equals("")) getHeaders().removeParam(headers[i]);
452         else headers[i].setValue(buf.toString());
453         break;
454     }
455     // In this case if we don't find the cookie string, we don't
456
// need to do anything
457
}
458
459     public void deleteCookie(String JavaDoc ckname) {
460
461     if(debug) log("Deleting cookie: " + ckname); //NOI18N
462

463     Param[] headers = getHeaders().getParam();
464     // No headers (this should not happen)
465
if(headers == null || headers.length == 0) {
466         if(debug) log("No headers"); //NOI18N
467
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()); //NOI18N
473

474         if(!headers[i].getName().equalsIgnoreCase(COOKIE))
475         continue;
476
477         String JavaDoc oldCookies = headers[i].getValue();
478         
479         if(oldCookies == null || oldCookies.trim().equals("")) { //NOI18N
480
if(debug) log("No cookies"); //
481
return;
482         }
483         
484         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
485         StringTokenizer tok = new StringTokenizer(headers[i].getValue(),
486                               ";", false); // NOI18N
487

488         while (tok.hasMoreTokens()) {
489             
490         String JavaDoc token = tok.nextToken();
491         int j = token.indexOf("="); // NOI18N
492
if (j > -1) {
493
494             String JavaDoc name = token.substring(0, j).trim();
495             if(name.equalsIgnoreCase(ckname)) continue;
496             else {
497             if(debug) log("Keep this cookie");//NOI18N
498
String JavaDoc value =
499                 token.substring(j+1, token.length()).trim();
500             value=stripQuote(value);
501             buf.append(name);
502             buf.append("=");//NOI18N
503
buf.append(value);
504             buf.append(";"); //NOI18N
505
}
506         }
507         }
508
509         if(debug)
510         log("New cookie string is: " + buf.toString()); //NOI18N
511

512         if(buf.toString().equals("")) getHeaders().removeParam(headers[i]);
513         else headers[i].setValue(buf.toString());
514         return;
515     }
516     // If we never find a cookie header we don't need to do
517
// anything
518
}
519     
520
521     /**
522      * @param value a <code>String</code> specifying the cookie value
523      * (possibly quoted).
524      */

525     public static String JavaDoc stripQuote( String JavaDoc value ) {
526     
527     if (((value.startsWith("\"")) && (value.endsWith("\""))) || // NOI18N
528
((value.startsWith("'") && (value.endsWith("'"))))) { // NOI18N
529
try {
530         return value.substring(1,value.length()-1);
531         } catch (Exception JavaDoc ex) {
532         }
533     }
534     return value;
535     }
536
537     // This method verifies that the mandatory properties are set
538
public boolean verify()
539     {
540     return true;
541     }
542
543     //
544
static public void addComparator(BeanComparator c)
545     {
546     RequestData.comparators.add(c);
547     }
548
549     //
550
static public void removeComparator(BeanComparator c)
551     {
552     RequestData.comparators.remove(c);
553     }
554     //
555
public void addPropertyChangeListener(PropertyChangeListener l)
556     {
557     BeanProp p = this.beanProp();
558     if (p != null)
559         p.addPCListener(l);
560     }
561
562     //
563
public void removePropertyChangeListener(PropertyChangeListener l)
564     {
565     BeanProp p = this.beanProp();
566     if (p != null)
567         p.removePCListener(l);
568     }
569
570     //
571
public void addPropertyChangeListener(String JavaDoc n, PropertyChangeListener l)
572     {
573     BeanProp p = this.beanProp(n);
574     if (p != null)
575         p.addPCListener(l);
576     }
577
578     //
579
public void removePropertyChangeListener(String JavaDoc n, PropertyChangeListener l)
580     {
581     BeanProp p = this.beanProp(n);
582     if (p != null)
583         p.removePCListener(l);
584     }
585
586     // Dump the content of this bean returning it as a String
587
public void dump(StringBuffer JavaDoc str, String JavaDoc indent)
588     {
589     String JavaDoc s;
590     BaseBean n;
591
592     str.append(indent);
593     str.append("Headers"); //NOI18N
594
n = this.getHeaders();
595     if (n != null)
596         n.dump(str, indent + "\t"); //NOI18N
597
else
598         str.append(indent+"\tnull"); //NOI18N
599
this.dumpAttributes(HEADERS, 0, str, indent);
600
601     str.append(indent);
602     str.append("Param["+this.sizeParam()+"]"); //NOI18N
603
for(int i=0; i<this.sizeParam(); i++)
604         {
605         str.append(indent+"\t"); //NOI18N
606
str.append("#"+i+":"); //NOI18N
607
n = this.getParam(i);
608         if (n != null)
609             n.dump(str, indent + "\t"); //NOI18N
610
else
611             str.append(indent+"\tnull"); //NOI18N
612
this.dumpAttributes(PARAM, i, str, indent);
613         }
614
615     }
616
617     public String JavaDoc dumpBeanNode() {
618     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
619     str.append("RequestData\n"); //NOI18N
620
this.dump(str, "\n "); //NOI18N
621
return str.toString();
622     }
623     
624     //
625
// This method returns the root of the bean graph
626
// Each call creates a new bean graph from the specified DOM graph
627
//
628
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 JavaDoc t) {
642         throw new RuntimeException JavaDoc("DOM graph creation failed: "+ //NOI18N
643
t.getMessage());
644     }
645     }
646
647     //
648
// This method returns the root for a new empty bean graph
649
//
650
public static RequestData createGraph() {
651     return new RequestData();
652     }
653
654     public void log(String JavaDoc s) {
655     System.out.println("RequestData::" + s); //NOI18N
656
}
657
658 }
659
660
Popular Tags