KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > server > MonitorRequestWrapper


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * @author Ana von Klopp
22  * @author Simran Gleason
23  */

24
25 package org.netbeans.modules.web.monitor.server;
26
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Stack JavaDoc;
33 import java.util.Vector JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
37 import javax.servlet.http.HttpUtils JavaDoc;
38 import org.netbeans.modules.web.monitor.data.RequestData;
39 import org.netbeans.modules.web.monitor.data.Param;
40
41 /**
42  * The MonitorRequestWrapper class is used by the MonitorFilter to
43  * wrap the request. It's main function is to ensure that the
44  * application receives the data from a replay request.
45  */

46 public class MonitorRequestWrapper extends HttpServletRequestWrapper JavaDoc {
47     
48     private boolean replay = false;
49
50     // These fields hold local variables during replays.
51
private String JavaDoc localMethod = null;
52     private String JavaDoc localProtocol = null;
53     private String JavaDoc localScheme = null;
54     private String JavaDoc localRemoteAddr = null;
55     private String JavaDoc localQueryString = null;
56     private Param[] localHeaders = null;
57     private Vector JavaDoc localCookies = null;
58     private Map JavaDoc oldParams = null;
59     private Map JavaDoc localParams = null;
60     private Stack JavaDoc extraParamStack = null;
61
62     // These fields are used to manage session replacement during
63
// replay, if the server supports it
64
public final static String JavaDoc JSESSIONID = "JSESSIONID"; // NOI18N
65
public final static String JavaDoc REPLACED =
66     "netbeans.replay.session-replaced"; //NOI18N
67

68     private static final boolean debug = false;
69     
70     MonitorRequestWrapper(HttpServletRequest JavaDoc req) {
71     super(req);
72     }
73
74     /**
75      * The filter will only wrap the request if it was a
76      * HttpServletRequest. This is a convenience method for
77      * accessing the request variable as such, for those methods that
78      * aren't available on the regular servlet request.
79      */

80     private HttpServletRequest JavaDoc getHRequest() {
81     return (HttpServletRequest JavaDoc)getRequest();
82     }
83
84     // GETTERS FROM THE HttpServletRequest
85
//
86
// The getters implement the decorator pattern, except for replays
87
// where we use local data.
88

89
90     /**
91      * During a replay, returns the local value. If not a replay,
92      * simply returns the value of invoking the method on the wrapped
93      * request.
94      */

95     public String JavaDoc getMethod() {
96     if (replay) {
97         return localMethod;
98     }
99     return getHRequest().getMethod();
100     }
101
102     // ********************** PARAMETERS *************************
103
// All the getParameter methods must refer to getParameterMap for
104
// locally set parameters. See pushExtraParameters and
105
// popExtraParameters for additional detail on local parameters.
106
//
107

108     /** getParameterMap returns<br>
109      * a) the parameter map from the request, if this is not a replay<br>
110      * b) the local parameter map, if no extra parameters have been
111      * set ("extra parameters" are parameters which are set through
112      * the <jsp:param> tag inside <jsp:forward/include>. <br>
113      * c) the local parameter map augmented with parameters so set. <br>
114      *
115      * @see pushExtraParameters
116      * @see popExtraParameters
117      */

118     public java.util.Map JavaDoc getParameterMap() {
119
120     if(debug) log("getParameterMap()"); //NOI18N
121

122     if (!replay) return getRequest().getParameterMap();
123
124     // Could cache the results of processing the parameters, but
125
// it is relatively unusual that this gets expensive. It will
126
// only get repeated on a replay where the request processing
127
// involves a dispatch made from a JSP using the param tag AND
128
// the file that got dispatched to accesses the parameter more
129
// than once.
130
if(extraParamStack == null || extraParamStack.empty())
131         return (java.util.Map JavaDoc)localParams;
132
133     Map JavaDoc map = (Map JavaDoc)extraParamStack.peek();
134
135     if(map.size() == 0) return (java.util.Map JavaDoc)localParams;
136
137     Hashtable JavaDoc ht = new Hashtable JavaDoc();
138
139     Iterator JavaDoc keys = localParams.keySet().iterator();
140     while(keys.hasNext()) {
141         Object JavaDoc o = keys.next();
142         if(map.containsKey(o)) {
143         String JavaDoc[] vals0 = (String JavaDoc[])localParams.get(o);
144         String JavaDoc[] vals1 = (String JavaDoc[])map.get(o);
145         String JavaDoc[] vals2 = new String JavaDoc[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 JavaDoc o = keys.next();
157         if(localParams.containsKey(o)) continue;
158         ht.put(o, map.get(o));
159     }
160     return (Map JavaDoc)ht;
161     }
162
163
164     /**
165     * If this is not a replay, getParameter(key) returns the value of
166     * invoking the method on the original request. If it is a replay,
167     * it returns the first string of the String array for the key, if
168     * such an array exists. Otherwise it returns null.
169     */

170     public String JavaDoc getParameter(String JavaDoc key) {
171
172     if(debug) log("getParameters()"); //NOI18N
173

174     if (!replay) return getRequest().getParameter(key);
175
176     String JavaDoc [] values = (String JavaDoc[])getParameterMap().get(key);
177     if (values != null && values.length > 0) {
178         return values[0];
179     }
180     return null;
181     }
182
183     /**
184     * If this is not a replay, getParameterNames returns the value of
185     * invoking the method on the original request. If it is a replay,
186     * it returns an Enumeration derived from the keyset of the
187     * parameter map.
188     */

189     public Enumeration JavaDoc getParameterNames() {
190     if(debug) log("getParameterNames"); //NOI18N
191
if (!replay)
192         return getRequest().getParameterNames();
193     if(debug) {
194         Enumeration JavaDoc e = new Vector JavaDoc(getParameterMap().keySet()).elements();
195         while(e.hasMoreElements())
196         log("\t" + String.valueOf(e.nextElement())); //NOI18N
197
}
198     return new Vector JavaDoc(getParameterMap().keySet()).elements();
199     }
200
201     /**
202     * If this is not a replay, getParameterValues returns the value of
203     * invoking the method on the original request. If it is a replay,
204     * it returns a String array matching the key in the local parameter
205     * map.
206     */

207     public String JavaDoc [] getParameterValues(String JavaDoc name) {
208     if(debug) log("getParameterValues"); //NOI18N
209
if (!replay)
210         return getRequest().getParameterValues(name);
211     return (String JavaDoc[])getParameterMap().get(name);
212     }
213
214
215     /**
216      * During a replay, returns the local value. If not a replay,
217      * simply returns the value of invoking the method on the wrapped
218      * request.
219      */

220     public String JavaDoc getQueryString() {
221     if (!replay)
222         return getHRequest().getQueryString();
223     return localQueryString;
224     }
225
226     /**
227      * During a replay, returns the local value. If not a replay,
228      * simply returns the value of invoking the method on the wrapped
229      * request.
230      */

231     public String JavaDoc getProtocol() {
232     if (!replay)
233         return getRequest().getProtocol();
234     return localProtocol;
235     }
236
237
238     /**
239      * During a replay, returns the local value. If not a replay,
240      * simply returns the value of invoking the method on the wrapped
241      * request.
242      */

243     public String JavaDoc getScheme() {
244     if (replay) return localScheme;
245     return getRequest().getScheme();
246     }
247
248     /**
249      * During a replay, returns the local value. If not a replay,
250      * simply returns the value of invoking the method on the wrapped
251      * request.
252      *
253      * According to the Servlet specification, this method must return
254      * null if there is no header of the specified name.
255      */

256     public String JavaDoc getHeader(String JavaDoc 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"); //NOI18N
265
return null;
266     }
267
268     if(debug) {
269         log("Headers not set locally"); //NOI18N
270
log(key + " " + getHRequest().getHeader(key)); //NOI18N
271
}
272     return getHRequest().getHeader(key);
273     }
274
275     /**
276      * During a replay, returns the local value. If not a replay,
277      * simply returns the value of invoking the method on the wrapped
278      * request.
279      */

280     public Enumeration JavaDoc getHeaderNames() {
281
282     if (replay) {
283         Vector JavaDoc v = new Vector JavaDoc();
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(); //NOI18N
290
}
291     return getHRequest().getHeaderNames();
292     }
293
294     /**
295      * During a replay, returns the local value. If not a replay,
296      * simply returns the value of invoking the method on the wrapped
297      * request.
298      */

299     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
300
301     if (replay) {
302
303         Vector JavaDoc v = new Vector JavaDoc();
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(); //NOI18N
311
}
312     return getHRequest().getHeaders(name);
313     }
314
315     /**
316      * During a replay, returns the local value. If not a replay,
317      * simply returns the value of invoking the method on the wrapped
318      * request.
319      */

320     public int getIntHeader(String JavaDoc name) {
321     int headerValue = -1;
322     String JavaDoc value = getHeader(name);
323     if (value != null)
324         headerValue = Integer.parseInt(value);
325     
326     return headerValue;
327     }
328
329     /**
330      * During a replay, returns the local value. If not a replay,
331      * simply returns the value of invoking the method on the wrapped
332      * request.
333      */

334     public long getDateHeader (String JavaDoc name) {
335     
336     long dateValue = -1;
337     String JavaDoc 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 JavaDoc e) {
347         }
348         if (dateValue == -1) {
349         // let it throw
350
throw new IllegalArgumentException JavaDoc ();
351         }
352     }
353     return dateValue;
354     }
355
356
357     /**
358      * During a replay, returns the local value. If not a replay,
359      * simply returns the value of invoking the method on the wrapped
360      * request.
361      */

362     public Cookie JavaDoc[] getCookies() {
363     if(!replay)
364         return getHRequest().getCookies();
365     
366     if(localCookies == null)
367         return new Cookie JavaDoc[0];
368     
369     int numCookies = localCookies.size();
370     Cookie JavaDoc[] cookieArray = new Cookie JavaDoc[numCookies];
371     Enumeration JavaDoc e = localCookies.elements();
372     int index = 0;
373     while(e.hasMoreElements()) {
374         cookieArray[index] = (Cookie JavaDoc)e.nextElement();
375         ++index;
376     }
377     return cookieArray;
378     }
379
380     /**
381      * This always returns the attributes that are set on the wrapped
382      * request, regardless of whether this is a replay or
383      * not. (Attributes are set by the web components, not as a result
384      * of parsing the request). This method strips off any attributes
385      * names which are used by the HTTP Monitor itself (these start
386      * with "netbeans.monitor." so it's unlikely that they'll be used
387      * in a regular app.
388      */

389     public Enumeration JavaDoc getAttributeNames() {
390          
391     if(debug) log("getAttributeNames()"); //NOI18N
392

393     Enumeration JavaDoc e = getRequest().getAttributeNames();
394
395     // If we're debugging, check the monitor attributes while
396
// we're at it...
397
if(debug) return e;
398
399     Vector JavaDoc v = new Vector JavaDoc();
400     while (e.hasMoreElements()) {
401         String JavaDoc name = (String JavaDoc)e.nextElement();
402         // We don't record the request or the servlet attributes
403
// because we made those oursevles.
404
if(name.startsWith(MonitorFilter.PREFIX)) {
405         if(debug) log("discarded " + name); //NOI18N
406
continue;
407         }
408         if(debug) log("keeping " + name); //NOI18N
409
v.add(name);
410     }
411     return v.elements();
412     }
413
414     // Pending - not held as local data though it should be
415
public String JavaDoc getRemoteAddr() {
416     return getRequest().getRemoteAddr();
417     /*
418     if (!replay)
419         //return null; // not yet: getRequest().getRemoteAddr();
420         return getRequest().getRemoteAddr();
421     return localRemoteAddr;
422     */

423     }
424
425     //*********************** END GETTERS **********************
426

427
428     /**
429      * Populates the request wrapper with local data during a replay.
430      *
431      * @param rd The RequestData object that we use to repopulate this
432      * request
433      * @param replaceSessionID true if the user requested for the
434      * session ID to be replaced
435      * @param canplaceSessionID true if the server can replace the
436      * session ID
437      **/

438     public void populate(RequestData rd,
439              boolean replaceSessionID) {
440
441     // Replay is true, values are local
442
replay = true;
443
444     // Set the request method
445
localMethod = rd.getAttributeValue("method"); // NOI18N
446

447     // Set the protocol
448
localProtocol = rd.getAttributeValue("protocol"); // NOI18N
449

450     // Set the scheme
451
localScheme = rd.getAttributeValue("scheme"); // NOI18N
452

453     // PENDING: Set the remote address
454
// localRemoteAddr =
455
// rd.getClientData().getAttributeValue("remoteAddress"); // NOI18N
456

457     // Set the query string
458
localQueryString = rd.getAttributeValue("queryString"); // NOI18N
459

460     oldParams = getRequest().getParameterMap();
461
462     // Do the parameters
463
if(localMethod.equals("GET")) { //NOI18N
464

465         try {
466         localParams = HttpUtils.parseQueryString(localQueryString);
467         }
468         catch(Exception JavaDoc ex) {
469         // This utility doesn't like query strings that aren't
470
// in parameter format
471
localParams = new Hashtable JavaDoc();
472         }
473     }
474
475     else if(localMethod.equals("POST")) { // NOI18N
476

477         try {
478         localParams = HttpUtils.parseQueryString(localQueryString);
479         }
480         catch(Exception JavaDoc ex) {
481         // This utility doesn't like query strings that aren't
482
// in parameter format
483
localParams = new Hashtable JavaDoc();
484         }
485
486         // PENDING - this assumes the stuff comes in as parameters,
487
// but it could come in as non-parameterized data and
488
// ideally we should deal with this case also (and
489
// multiforms). Right now we just make sure we don't break
490
// everything if there is a non-parametrized request.
491

492         Param[] params = rd.getParam();
493         int numParams = params.length;
494     
495         for(int i=0; i<numParams; ++i) {
496         String JavaDoc name = params[i].getAttributeValue("name"); // NOI18N
497
String JavaDoc[] values = null;
498         if(localParams.containsKey(name)) {
499             values = (String JavaDoc[])localParams.get(name);
500             String JavaDoc[] newvals = new String JavaDoc[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"); // NOI18N
505
localParams.put(name, newvals);
506         }
507         else {
508             values = new String JavaDoc[1];
509             values[0] = params[i].getAttributeValue("value"); // NOI18N
510
localParams.put(name, values);
511         }
512         }
513     }
514     else if(localMethod.equals("PUT")) { // NOI18N
515

516         localParams = new Hashtable JavaDoc();
517
518         // PENDING
519
// This method would normally come with a file passed in
520
// through the inputstream. I am ignoring that.
521
}
522     
523     else {
524         // The user shouldn't be able to set any parameters for
525
// other HTTP methods.
526
localParams = new Hashtable JavaDoc();
527     }
528
529     // Set the headers and cookies
530
if(debug) {
531         log("CookieString from real req: " + //NOI18N
532
String.valueOf(getHRequest().getHeader("cookie"))); //NOI18N
533
log("CookieString from rd: " + //NOI18N
534
String.valueOf(rd.getCookieString()));
535
536     }
537
538     // Used to create the cookie header
539
StringBuffer JavaDoc cookieBuf = new StringBuffer JavaDoc();
540
541     // Set the headers in the wrapper to what they were set to in
542
// the data record. It will have to be modified in case either
543
// a) The user wanted to use the browser's cookie
544
// b) The server can't replace the session cookie
545
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?"); //NOI18N
552
log("param length is " + String.valueOf(localHeaders.length)); //NOI18N
553
for(int i=0; i<localHeaders.length; ++i)
554         log(localHeaders[i].getName() + " " + //NOI18N
555
localHeaders[i].getValue());
556     }
557
558     // We're going to parse the cookies again so we'll start with
559
// an empty vector
560
localCookies = new Vector JavaDoc();
561
562     // Holds the session id from the data record
563
String JavaDoc idFromRequest = null;
564
565     // Cookies from the data record
566
Param[] myCookies = rd.getCookiesAsParams();
567
568     // Start by adding the recorded cookies, if there were any
569
if(myCookies != null && myCookies.length > 0) {
570
571         if(debug) log("Now adding cookies"); //NOI18N
572

573         String JavaDoc ckname = null, ckvalue=null;
574         for(int i=0; i<myCookies.length; ++i) {
575         
576         ckname = myCookies[i].getAttributeValue("name"); //NOI18N
577
ckvalue = myCookies[i].getAttributeValue("value"); //NOI18N
578

579         // We don't add the session cookie yet, but we keep
580
// the session id from the record in case
581
if(ckname.equalsIgnoreCase(JSESSIONID)) {
582             idFromRequest = ckvalue;
583             continue;
584         }
585         localCookies.add(new Cookie JavaDoc(ckname, ckvalue));
586         if(debug) log("Added " + ckname + "=" + ckvalue); //NOI18N
587

588         if(cookieBuf.length() > 0) cookieBuf.append("; "); //NOI18N
589
cookieBuf.append(ckname);
590         cookieBuf.append("="); //NOI18N
591
cookieBuf.append(ckvalue);
592         }
593     }
594
595     // Find out if the session id was replaced by checking whether
596
// the replaced attribute was set (by the MonitorValve, on
597
// Tomcat).
598

599     boolean sessionReplaced = false;
600     try {
601         String JavaDoc value = (String JavaDoc)getHRequest().getAttribute(REPLACED);
602         if(value.equals("true")) sessionReplaced = true; //NOI18N
603
if(debug) log("replaced the session"); //NOI18N
604
}
605     catch(Exception JavaDoc ex) {
606         if(debug) log("didn't replace the session"); //NOI18N
607
}
608
609     if(sessionReplaced) {
610
611         // If the session ID was replaced, this could mean either
612
// that we request a different session or that the
613
// reference to the session was removed. In the former
614
// case, we add the session cookie to the vector and to
615
// the cookie string, using the ID from the data record.
616

617         if(idFromRequest != null) {
618         
619         localCookies.add(new Cookie JavaDoc(JSESSIONID, idFromRequest));
620         if(cookieBuf.length() > 0) cookieBuf.append("; "); //NOI18N
621
cookieBuf.append(JSESSIONID);
622             cookieBuf.append("="); //NOI18N
623
cookieBuf.append(idFromRequest);
624         }
625         // In the latter case we do nothing.
626
}
627     else {
628
629         // The session ID was not replaced, and to adjust for this
630
// we add the session cookie from the incoming request, if
631
// there is one.
632

633         // PENDING!
634
// If the user wanted to replace the ID and it failed,
635
// this should be flagged here.
636

637         if(debug) log("Old request is " + getHRequest().toString()); //NOI18N
638

639         Cookie JavaDoc[] ck = getHRequest().getCookies();
640
641         if(debug) log("Got the incoming cookies"); //NOI18N
642

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("; "); //NOI18N
648
cookieBuf.append(JSESSIONID);
649             cookieBuf.append("="); //NOI18N
650
cookieBuf.append(ck[i].getValue());
651             }
652         }
653         }
654         // the entity that sent the request did not send any
655
// cookies, do nothing
656
}
657     String JavaDoc cookieStr = cookieBuf.toString();
658     if(cookieStr.equals("")) { //NOI18N
659
if(debug) log("No cookies, deleting cookie header"); //NOI18N
660
removeHeader("cookie"); //NOI18N
661
}
662     else {
663         if(debug) log("Setting cookie header to " + //NOI18N
664
cookieBuf.toString());
665         setHeader("cookie", cookieBuf.toString()); //NOI18N
666
}
667     }
668
669
670     // UTILITY METHODS
671
// These methods set local data. They are used by
672
// populate to set data during replays.
673

674
675     /**
676      * This method MUST be invoked by the monitor filter whenever it
677      * receives a request to process a dispatched request, BEFORE it
678      * collects any data.
679      * We need to do this because the specs allow web components to
680      * add extra parameters on dispatch, e.g.
681      * <jsp:forward page="page.jsp">
682      * <jsp:param name="name" value="value"/>
683      * </jsp:forward>
684      * and these are only available to the dispatched-to resources
685      * (at least some servers remove them after the request dispatcher
686      * has terminated). So we must have a mechanism which guarantees
687      * that the resources in the web app (and the monitor itself) only
688      * sees the parameters when they would have been visible without
689      * the monitor in place.
690      * To deal with such parameters on a replay (where the request's
691      * "real" parameters have been replaced with the ones from the
692      * original request) I have to add them to the ones that the
693      * wrapper already knows about.
694      */

695     void pushExtraParameters() {
696     
697     if(!replay) return;
698
699     if(debug) log("pushExtraParameters"); //NOI18N
700

701     // If this is a replay and the request was dispatched using
702
// the following type of syntax
703
// <jsp:forward page="page.jsp">
704
// <jsp:param name="name" value="value"/>
705
// </jsp:forward>
706
// then the server will add a parameter to the request in the
707
// background (there are no API methods to do this). So we
708
// have to check if the parameters that the original request
709
// is aware of have grown. If so, we create an additional map
710
// with the extra parameters.
711

712     Map JavaDoc extraParams = new Hashtable JavaDoc();
713
714     Map JavaDoc currentMap = getRequest().getParameterMap();
715     Iterator JavaDoc keys = currentMap.keySet().iterator();
716
717     while(keys.hasNext()) {
718         Object JavaDoc o = keys.next();
719         if(debug) {
720         //log("Key: " + (String)o); //NOI18N
721
String JavaDoc[] value = (String JavaDoc[])currentMap.get(o);
722         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
723         for(int k=0; k<value.length; ++k) {
724             buf.append(value[k]);
725             buf.append(" "); //NOI18N
726
}
727         log("Value: " + buf.toString()); //NOI18N
728
}
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 JavaDoc();
744
745     extraParamStack.push(extraParams);
746     if(debug)
747         log("Param stack size: " + String.valueOf(extraParamStack.size())); //NOI18N
748
}
749
750     /**
751      * This method MUST be invoked by the monitor filter whenever it
752      * has finished processing a dispatched request, AFTER it has
753      * collected the data.
754      *
755      * @see pushExtraParameters
756      */

757     void popExtraParameters() {
758
759     if(!replay) return;
760
761     if(debug) log("popExtraParameters"); //NOI18N
762
if(extraParamStack == null || extraParamStack.empty()) {
763         log("ERROR - MonitorRequestWrapper empty param stack!"); //NOI18N
764
return;
765     }
766     extraParamStack.pop();
767     }
768
769
770     /**
771      * The setHeader method allows the request wrapper to modify
772      * the value of a header. This method is only used during replay,
773      * by the populate method.
774      */

775     private void setHeader(String JavaDoc headerName, String JavaDoc headerValue) {
776     
777     if(!replay) {
778         log("setHeader() must only be used from replay"); //NOI18N
779
return;
780     }
781
782     boolean addedHeader = false;
783
784     if(debug) log("Headers were set locally"); //NOI18N
785
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"); //NOI18N
790
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"); //NOI18N
803
return;
804     }
805
806     /**
807      * The removeHeader method allows the request wrapper to delete
808      * a header. This may be necessary during a replay, to remove the
809      * session cookie if the browser didn't send one. Note that using
810      * this method will result in the headers being local to the
811      * RequestWrapper, since we can't manipulate the headers on the
812      * actual request.
813      */

814     private void removeHeader(String JavaDoc headerName) {
815
816     if(!replay) {
817         log("removeHeader() must only be used from replay"); //NOI18N
818
return;
819     }
820
821     if(debug) log("removeHeader()"); //NOI18N
822

823     Vector JavaDoc v = new Vector JavaDoc();
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     /**
841      * toString prints out the main values of the request.
842      */

843     public String JavaDoc toString() {
844     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
845     buf.append("uri: "); // NOI18N
846
buf.append(getRequestURI());
847     buf.append("\n"); // NOI18N
848
buf.append("method: "); // NOI18N
849
buf.append(getMethod());
850     buf.append("\n"); // NOI18N
851
buf.append("QueryString: "); // NOI18N
852
buf.append(getQueryString());
853     buf.append("\n"); // NOI18N
854
buf.append("Parameters:\n"); // NOI18N
855
Enumeration JavaDoc e = getParameterNames();
856     while(e.hasMoreElements()) {
857         String JavaDoc name = (String JavaDoc)e.nextElement();
858         String JavaDoc value = getParameter(name);
859         buf.append("\tName: "); // NOI18N
860
buf.append(name);
861         buf.append("\tValue: "); // NOI18N
862
buf.append(value);
863         buf.append("\n"); // NOI18N
864
}
865
866     buf.append("Headers:\n"); // NOI18N
867
e = getHeaderNames();
868     while(e.hasMoreElements()) {
869         String JavaDoc name = (String JavaDoc)e.nextElement();
870         String JavaDoc value = getHeader(name);
871         buf.append("\tName: "); // NOI18N
872
buf.append(name);
873         buf.append("\tValue: "); // NOI18N
874
buf.append(value);
875         buf.append("\n"); // NOI18N
876
}
877     return buf.toString();
878     }
879     
880     /*
881      * log, used for debugging.
882      */

883     private void log(String JavaDoc s) {
884     System.out.println("MonitorRequestWrapper::" + s); // NOI18N
885
}
886
887 } // MonitorRequestWrapper
888
Popular Tags