1 19 20 package org.netbeans.modules.web.monitor.client; 21 22 import java.util.*; 23 import javax.servlet.http.HttpUtils ; 24 import org.netbeans.modules.web.monitor.data.*; 25 26 27 40 41 public class Util { 42 43 private final static boolean debug = false; 44 45 public Util() {} 46 47 53 54 public static void composeQueryString(RequestData rd) { 55 56 if(debug) System.out.println("Doing query string"); 58 if(rd.sizeParam() == 0) return; 59 60 Param[] params = rd.getParam(); 61 StringBuffer buf = new StringBuffer (512); 62 String 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); value = params[i].getValue(); 70 if(debug) System.out.println("value: " + value); } 72 catch(Exception ex) { 73 continue; 74 } 75 if(name.equals("")) continue; 77 if (value != null) value = value.trim(); 78 else value = ""; 80 if(i>0) buf.append('&'); buf.append(name); 82 buf.append('='); buf.append(value); 84 } 85 rd.setAttributeValue("queryString", buf.toString()); rd.setParam(new Param[0]); 87 88 if (debug) 89 System.out.println("EditPanel::composedQueryString: [" + buf.toString() + "]"); } 92 93 static boolean removeParametersFromQuery(RequestData rd) { 94 95 if(rd.sizeParam() == 0) return false; 97 98 String queryString = 99 rd.getAttributeValue("queryString"); 101 Hashtable ht = null; 103 try { 104 ht = javax.servlet.http.HttpUtils.parseQueryString(queryString); 105 } 106 catch(IllegalArgumentException iae) { 107 return false; 109 } 110 if(ht == null || ht.isEmpty()) return false; 111 112 Enumeration e = ht.keys(); 113 114 while(e.hasMoreElements()) { 115 String name = (String )e.nextElement(); 116 try { 117 String [] value = (String [])(ht.get(name)); 118 for(int i=0; i<value.length; ++i) { 119 if(debug) System.out.println("Removing " + name + " " + value); 122 Param p = findParam(rd.getParam(), name, value[i]); 123 rd.removeParam(p); 124 } 125 } 126 catch(Exception ex) { 127 } 128 } 129 return true; 130 } 131 132 static void addParametersToQuery(RequestData rd) { 133 134 Hashtable ht = null; 135 String queryString = rd.getAttributeValue("queryString"); try { 137 ht = javax.servlet.http.HttpUtils.parseQueryString(queryString); 138 } 139 catch(Exception ex) { } 140 141 if(ht != null && ht.size() > 0) { 142 Enumeration e = ht.keys(); 143 while(e.hasMoreElements()) { 144 String name = (String )e.nextElement(); 145 String [] value = (String [])(ht.get(name)); 146 for(int i=0; i<value.length; ++i) { 147 if(debug) 148 System.out.println("Adding " + name + " " + value); Param p = new Param(name, value[i]); 151 rd.addParam(p); 152 } 153 } 154 } 155 } 156 157 164 165 public static void setSessionCookieHeader(MonitorData md) { 166 167 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 cookiesOut = new StringBuffer (""); 178 for(int i=0; i<numParams; ++i) { 179 180 Param p = params[i]; 181 182 if(p.getAttributeValue("name").equals("Cookie")) { 184 String cookies = p.getAttributeValue("value"); 186 StringTokenizer st = new StringTokenizer(cookies, ";"); 188 while(st.hasMoreTokens()) { 189 String cookie = st.nextToken(); 190 if(debug) System.out.println("Now doing "+ cookie); 192 if(cookie.startsWith("JSESSIONID")) { sessionCookie = true; 194 if(debug) System.out.println("Found session cookie"); if(debug) System.out.println("Getting session ID"); String sessionID = null; 197 try { 198 sessionID = 199 md.getSessionData().getAttributeValue("id"); if(debug) System.out.println("..." + sessionID); } 202 catch(Exception ex) {} 203 if(debug) System.out.println("Setting session cookie"); cookiesOut.append("JSESSIONID="); cookiesOut.append(sessionID); 206 cookiesOut.append(";"); } 208 else { 209 if(debug) System.out.println("Appending " + cookie); cookiesOut.append(cookie); 211 cookiesOut.append(";"); } 213 } 214 if(debug) 215 System.out.println("Cookie string: " + cookiesOut.toString()); 217 if(sessionCookie) { 218 if(debug) System.out.println("Found session cookie"); p.setAttributeValue("value", cookiesOut.toString()); 221 } 222 } 223 } 224 } 225 226 229 public static Param findParam(Param [] myParams, String name, 230 String 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 |