KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > Util


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.client;
21
22 import java.util.*;
23 import javax.servlet.http.HttpUtils JavaDoc;
24 import org.netbeans.modules.web.monitor.data.*;
25
26
27 /**
28  * Util.java
29  *
30  * For the next proper release of the monitor module, these methods
31  * should move in with their respective data objects. I can't do that
32  * for now because I would break compatibility with tomcat-monitor.jar
33  * which include copies of the data files.
34  *
35  * Created: Thu Aug 30 17:43:28 2001
36  *
37  * @author Ana von Klopp
38  * @version
39  */

40
41 public class Util {
42
43     private final static boolean debug = false;
44
45     public Util() {}
46
47     /**
48      * We use this method to compose a query string from the
49      * parameters instead of using the query string we recorded. This
50      * is used by edit/replay, and also as a workaround for regular
51      * replay as getParameters() seems to be better implemented to
52      * deal with multibyte than getQueryString()... */

53
54     public static void composeQueryString(RequestData rd) {
55     
56     if(debug) System.out.println("Doing query string"); //NOI18N
57

58     if(rd.sizeParam() == 0) return;
59     
60     Param[] params = rd.getParam();
61     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(512);
62     String JavaDoc name, value;
63      
64     for(int i=0; i < params.length; i++) {
65
66         try {
67         name = params[i].getName().trim();
68         if(debug) System.out.println("name: " + name); //NOI18N
69
value = params[i].getValue();
70         if(debug) System.out.println("value: " + value); //NOI18N
71
}
72         catch(Exception JavaDoc ex) {
73         continue;
74         }
75         if(name.equals("")) continue; //NOI18N
76

77         if (value != null) value = value.trim();
78         else value = ""; //NOI18N
79

80         if(i>0) buf.append('&'); // NOI18N
81
buf.append(name);
82         buf.append('='); //NOI18N
83
buf.append(value);
84     }
85     rd.setAttributeValue("queryString", buf.toString()); //NOI18N
86
rd.setParam(new Param[0]);
87     
88     if (debug)
89         System.out.println("EditPanel::composedQueryString: [" + //NOI18N
90
buf.toString() + "]"); //NOI18N
91
}
92
93     static boolean removeParametersFromQuery(RequestData rd) {
94          
95     // Data wasn't parameterized
96
if(rd.sizeParam() == 0) return false;
97     
98     String JavaDoc queryString =
99         rd.getAttributeValue("queryString"); //NOI18N
100

101     // MULTIBYTE - I think this isn't working...
102
Hashtable ht = null;
103     try {
104         ht = javax.servlet.http.HttpUtils.parseQueryString(queryString);
105     }
106     catch(IllegalArgumentException JavaDoc iae) {
107         // do nothing, that's OK
108
return false;
109     }
110     if(ht == null || ht.isEmpty()) return false;
111     
112     Enumeration e = ht.keys();
113
114     while(e.hasMoreElements()) {
115         String JavaDoc name = (String JavaDoc)e.nextElement();
116         try {
117         String JavaDoc[] value = (String JavaDoc[])(ht.get(name));
118         for(int i=0; i<value.length; ++i) {
119             if(debug) System.out.println("Removing " + //NOI18N
120
name + " " + //NOI18N
121
value);
122             Param p = findParam(rd.getParam(), name, value[i]);
123             rd.removeParam(p);
124         }
125         }
126         catch(Exception JavaDoc ex) {
127         }
128     }
129     return true;
130     }
131
132     static void addParametersToQuery(RequestData rd) {
133
134     Hashtable ht = null;
135     String JavaDoc queryString = rd.getAttributeValue("queryString"); //NOI18N
136
try {
137         ht = javax.servlet.http.HttpUtils.parseQueryString(queryString);
138     }
139     catch(Exception JavaDoc ex) { }
140                 
141     if(ht != null && ht.size() > 0) {
142         Enumeration e = ht.keys();
143         while(e.hasMoreElements()) {
144         String JavaDoc name = (String JavaDoc)e.nextElement();
145         String JavaDoc[] value = (String JavaDoc[])(ht.get(name));
146         for(int i=0; i<value.length; ++i) {
147             if(debug)
148             System.out.println("Adding " + name + //NOI18N
149
" " + value); //NOI18N
150
Param p = new Param(name, value[i]);
151             rd.addParam(p);
152         }
153         }
154     }
155     }
156
157     /**
158      * The session cookie and the actual session that was used might
159      * be out of synch. This method makes sure that if the request
160      * contained an incoming session cookie and a session was used
161      * then the IDs will match (the session ID will be the one from
162      * the session, not the one from the cookie).
163      */

164       
165     public static void setSessionCookieHeader(MonitorData md) {
166     
167     // First we check *whether* we have a session cookie at
168
// all...
169
Headers headers = md.getRequestData().getHeaders();
170     int numParams = headers.sizeParam();
171     
172     if(numParams == 0) return;
173
174     boolean sessionCookie = false;
175     Param[] params = headers.getParam();
176     StringBuffer JavaDoc cookiesOut = new StringBuffer JavaDoc(""); //NOI18N
177

178     for(int i=0; i<numParams; ++i) {
179
180         Param p = params[i];
181         
182         if(p.getAttributeValue("name").equals("Cookie")) { //NOI18N
183

184         String JavaDoc cookies = p.getAttributeValue("value"); //NOI18N
185

186         StringTokenizer st = new StringTokenizer(cookies, ";"); //NOI18N
187

188         while(st.hasMoreTokens()) {
189             String JavaDoc cookie = st.nextToken();
190             if(debug) System.out.println("Now doing "+ //NOI18N
191
cookie);
192             if(cookie.startsWith("JSESSIONID")) { //NOI18N
193
sessionCookie = true;
194             if(debug) System.out.println("Found session cookie"); //NOI18N
195
if(debug) System.out.println("Getting session ID"); //NOI18N
196
String JavaDoc sessionID = null;
197             try {
198                 sessionID =
199                 md.getSessionData().getAttributeValue("id"); //NOI18N
200
if(debug) System.out.println("..." + sessionID); //NOI18N
201
}
202             catch(Exception JavaDoc ex) {}
203             if(debug) System.out.println("Setting session cookie"); //NOI18N
204
cookiesOut.append("JSESSIONID="); //NOI18N
205
cookiesOut.append(sessionID);
206             cookiesOut.append(";"); //NOI18N
207
}
208             else {
209             if(debug) System.out.println("Appending " + cookie); //NOI18N
210
cookiesOut.append(cookie);
211             cookiesOut.append(";"); //NOI18N
212
}
213         }
214         if(debug)
215             System.out.println("Cookie string: " + //NOI18N
216
cookiesOut.toString());
217         if(sessionCookie) {
218             if(debug) System.out.println("Found session cookie"); //NOI18N
219
p.setAttributeValue("value", //NOI18N
220
cookiesOut.toString());
221         }
222         }
223     }
224     }
225
226     /**
227      * find the param with the given name and value from the list.
228      */

229     public static Param findParam(Param [] myParams, String JavaDoc name,
230                   String JavaDoc value) {
231     for (int i=0; i < myParams.length; i++) {
232         Param param = myParams[i];
233         if (name.equals(param.getName()) &&
234         value.equals(param.getValue()) ) {
235         return param;
236         }
237     }
238     return null;
239     }
240 }
241
Popular Tags