001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.monitor.client;
043:
044: import java.util.*;
045: import javax.servlet.http.HttpUtils;
046: import org.netbeans.modules.web.monitor.data.*;
047:
048: /**
049: * Util.java
050: *
051: * For the next proper release of the monitor module, these methods
052: * should move in with their respective data objects. I can't do that
053: * for now because I would break compatibility with tomcat-monitor.jar
054: * which include copies of the data files.
055: *
056: * Created: Thu Aug 30 17:43:28 2001
057: *
058: * @author Ana von Klopp
059: * @version
060: */
061:
062: public class Util {
063:
064: private final static boolean debug = false;
065:
066: public Util() {
067: }
068:
069: /**
070: * We use this method to compose a query string from the
071: * parameters instead of using the query string we recorded. This
072: * is used by edit/replay, and also as a workaround for regular
073: * replay as getParameters() seems to be better implemented to
074: * deal with multibyte than getQueryString()... */
075:
076: public static void composeQueryString(RequestData rd) {
077:
078: if (debug)
079: System.out.println("Doing query string"); //NOI18N
080:
081: if (rd.sizeParam() == 0)
082: return;
083:
084: Param[] params = rd.getParam();
085: StringBuffer buf = new StringBuffer(512);
086: String name, value;
087:
088: for (int i = 0; i < params.length; i++) {
089:
090: try {
091: name = params[i].getName().trim();
092: if (debug)
093: System.out.println("name: " + name); //NOI18N
094: value = params[i].getValue();
095: if (debug)
096: System.out.println("value: " + value); //NOI18N
097: } catch (Exception ex) {
098: continue;
099: }
100: if (name.equals(""))
101: continue; //NOI18N
102:
103: if (value != null)
104: value = value.trim();
105: else
106: value = ""; //NOI18N
107:
108: if (i > 0)
109: buf.append('&'); // NOI18N
110: buf.append(name);
111: buf.append('='); //NOI18N
112: buf.append(value);
113: }
114: rd.setAttributeValue("queryString", buf.toString()); //NOI18N
115: rd.setParam(new Param[0]);
116:
117: if (debug)
118: System.out.println("EditPanel::composedQueryString: [" + //NOI18N
119: buf.toString() + "]"); //NOI18N
120: }
121:
122: static boolean removeParametersFromQuery(RequestData rd) {
123:
124: // Data wasn't parameterized
125: if (rd.sizeParam() == 0)
126: return false;
127:
128: String queryString = rd.getAttributeValue("queryString"); //NOI18N
129:
130: // MULTIBYTE - I think this isn't working...
131: Hashtable ht = null;
132: try {
133: ht = javax.servlet.http.HttpUtils
134: .parseQueryString(queryString);
135: } catch (IllegalArgumentException iae) {
136: // do nothing, that's OK
137: return false;
138: }
139: if (ht == null || ht.isEmpty())
140: return false;
141:
142: Enumeration e = ht.keys();
143:
144: while (e.hasMoreElements()) {
145: String name = (String) e.nextElement();
146: try {
147: String[] value = (String[]) (ht.get(name));
148: for (int i = 0; i < value.length; ++i) {
149: if (debug)
150: System.out.println("Removing " + //NOI18N
151: name + " " + //NOI18N
152: value);
153: Param p = findParam(rd.getParam(), name, value[i]);
154: rd.removeParam(p);
155: }
156: } catch (Exception ex) {
157: }
158: }
159: return true;
160: }
161:
162: static void addParametersToQuery(RequestData rd) {
163:
164: Hashtable ht = null;
165: String queryString = rd.getAttributeValue("queryString"); //NOI18N
166: try {
167: ht = javax.servlet.http.HttpUtils
168: .parseQueryString(queryString);
169: } catch (Exception ex) {
170: }
171:
172: if (ht != null && ht.size() > 0) {
173: Enumeration e = ht.keys();
174: while (e.hasMoreElements()) {
175: String name = (String) e.nextElement();
176: String[] value = (String[]) (ht.get(name));
177: for (int i = 0; i < value.length; ++i) {
178: if (debug)
179: System.out.println("Adding " + name + //NOI18N
180: " " + value); //NOI18N
181: Param p = new Param(name, value[i]);
182: rd.addParam(p);
183: }
184: }
185: }
186: }
187:
188: /**
189: * The session cookie and the actual session that was used might
190: * be out of synch. This method makes sure that if the request
191: * contained an incoming session cookie and a session was used
192: * then the IDs will match (the session ID will be the one from
193: * the session, not the one from the cookie).
194: */
195:
196: public static void setSessionCookieHeader(MonitorData md) {
197:
198: // First we check *whether* we have a session cookie at
199: // all...
200: Headers headers = md.getRequestData().getHeaders();
201: int numParams = headers.sizeParam();
202:
203: if (numParams == 0)
204: return;
205:
206: boolean sessionCookie = false;
207: Param[] params = headers.getParam();
208: StringBuffer cookiesOut = new StringBuffer(""); //NOI18N
209:
210: for (int i = 0; i < numParams; ++i) {
211:
212: Param p = params[i];
213:
214: if (p.getAttributeValue("name").equals("Cookie")) { //NOI18N
215:
216: String cookies = p.getAttributeValue("value"); //NOI18N
217:
218: StringTokenizer st = new StringTokenizer(cookies, ";"); //NOI18N
219:
220: while (st.hasMoreTokens()) {
221: String cookie = st.nextToken();
222: if (debug)
223: System.out.println("Now doing " + //NOI18N
224: cookie);
225: if (cookie.startsWith("JSESSIONID")) { //NOI18N
226: sessionCookie = true;
227: if (debug)
228: System.out.println("Found session cookie"); //NOI18N
229: if (debug)
230: System.out.println("Getting session ID"); //NOI18N
231: String sessionID = null;
232: try {
233: sessionID = md.getSessionData()
234: .getAttributeValue("id"); //NOI18N
235: if (debug)
236: System.out.println("..." + sessionID); //NOI18N
237: } catch (Exception ex) {
238: }
239: if (debug)
240: System.out
241: .println("Setting session cookie"); //NOI18N
242: cookiesOut.append("JSESSIONID="); //NOI18N
243: cookiesOut.append(sessionID);
244: cookiesOut.append(";"); //NOI18N
245: } else {
246: if (debug)
247: System.out.println("Appending " + cookie); //NOI18N
248: cookiesOut.append(cookie);
249: cookiesOut.append(";"); //NOI18N
250: }
251: }
252: if (debug)
253: System.out.println("Cookie string: " + //NOI18N
254: cookiesOut.toString());
255: if (sessionCookie) {
256: if (debug)
257: System.out.println("Found session cookie"); //NOI18N
258: p.setAttributeValue("value", //NOI18N
259: cookiesOut.toString());
260: }
261: }
262: }
263: }
264:
265: /**
266: * find the param with the given name and value from the list.
267: */
268: public static Param findParam(Param[] myParams, String name,
269: String value) {
270: for (int i = 0; i < myParams.length; i++) {
271: Param param = myParams[i];
272: if (name.equals(param.getName())
273: && value.equals(param.getValue())) {
274: return param;
275: }
276: }
277: return null;
278: }
279: }
|