1 package com.ibm.staf.service.http; 2 3 /*****************************************************************************/ 4 /* Software Testing Automation Framework (STAF) */ 5 /* (C) Copyright IBM Corp. 2004 */ 6 /* */ 7 /* This software is licensed under the Eclipse Public License (EPL) V1.0. */ 8 /*****************************************************************************/ 9 10 import org.apache.commons.httpclient.*; 11 import org.apache.commons.httpclient.cookie.CookiePolicy; 12 13 import java.util.Vector; 14 import java.util.HashMap; 15 import java.util.Date; 16 17 /*****************************************************************************/ 18 /* */ 19 /* Class: CookieAccess */ 20 /* Description: This class gives the HTTP service access to package */ 21 /* restricted functionality of HTTPUnit for cookie manipulation.*/ 22 /* */ 23 /*****************************************************************************/ 24 25 public class CookieAccess 26 { 27 public static final String NAME="NAME"; 28 public static final String VALUE="VALUE"; 29 public static final String DOMAIN="DOMAIN"; 30 public static final String EXPIRATION="EXPIRATION"; 31 public static final String PATH="PATH"; 32 33 /*****************************************************************************/ 34 /* */ 35 /* Method: addCookie */ 36 /* Description: Add a new cookie to the cookie jar. */ 37 /* Parameters: theCookie - the cookie to be added */ 38 /* session - the session that contains this cookie */ 39 /* Returns: void */ 40 /* */ 41 /*****************************************************************************/ 42 addCookie( Cookie theCookie, HttpClient session)43 public static void addCookie ( Cookie theCookie, HttpClient session) 44 { 45 session.getState().addCookie(theCookie); 46 } 47 findCookie(String name, Cookie[] cookies)48 public static int findCookie (String name, Cookie[] cookies) 49 throws InvalidCookieIDException 50 { 51 for (int i = 0; i < cookies.length; i++) 52 if (cookies[i].getName().equals(name)) 53 return i; 54 55 throw new InvalidCookieIDException(name, ""); 56 } 57 58 /*****************************************************************************/ 59 /* */ 60 /* Method: deleteCookie */ 61 /* Description: Delete the specified cookie from the session. If the cookie */ 62 /* does not exist, no error is declared. */ 63 /* Parameters: name - name of the cookie */ 64 /* session - the session that contains this cookie */ 65 /* Returns: void */ 66 /* */ 67 /*****************************************************************************/ 68 deleteCookie(String name, HttpClient session)69 public static void deleteCookie(String name, HttpClient session) 70 throws InvalidCookieIDException 71 { 72 Cookie [] cookies = session.getState().getCookies(); 73 74 int indx = findCookie(name, cookies); 75 Date purgeDate = new Date (10); 76 cookies[indx].setExpiryDate(purgeDate); 77 78 session.getState().purgeExpiredCookies(purgeDate); 79 80 } 81 82 /*****************************************************************************/ 83 /* */ 84 /* Method: createCookie */ 85 /* Description: create a new cookie */ 86 /* Parameters: name - name of the new cookie */ 87 /* value - value for the new cookie */ 88 /* Returns: a new Cookie */ 89 /* */ 90 /*****************************************************************************/ 91 createCookie(String name, String value)92 public static Cookie createCookie (String name, String value) 93 { 94 95 Cookie newCookie = new Cookie (); 96 newCookie.setName(name); 97 newCookie.setValue(value); 98 99 return newCookie; 100 } 101 102 /*****************************************************************************/ 103 /* */ 104 /* Method: cookieSummary */ 105 /* Description: summarizes the state of a cookie */ 106 /* Parameters: theCookie - the cookie to be summarized */ 107 /* Returns: a summary of the cookie: name value path domain */ 108 /* */ 109 /*****************************************************************************/ 110 cookieSummary(Cookie theCookie)111 public static HashMap cookieSummary (Cookie theCookie) 112 { 113 HashMap summary = new HashMap(); 114 115 summary.put(NAME,theCookie.getName()); 116 summary.put(VALUE,theCookie.getValue()); 117 summary.put(PATH,theCookie.getPath()); 118 summary.put(DOMAIN,theCookie.getDomain()); 119 summary.put(EXPIRATION,theCookie.getExpiryDate()/*.toString()*/); 120 121 return summary; 122 } 123 124 /*****************************************************************************/ 125 /* */ 126 /* Method: setCookieValue */ 127 /* Description: sets the value of the specifed cookie to the specified value */ 128 /* Parameters: name - name of the cookie */ 129 /* value - new value for the cookie */ 130 /* session - the session that contains this cookie */ 131 /* Returns: void */ 132 /* Throws: InvalidCookieIDException if the cookie does not exist */ 133 /* */ 134 /*****************************************************************************/ 135 setCookieValue(String name, String value, HttpClient session)136 public static void setCookieValue(String name, String value, 137 HttpClient session) 138 throws InvalidCookieIDException 139 { 140 141 Cookie [] cookies = session.getState().getCookies(); 142 143 int indx = findCookie(name, cookies); 144 145 cookies[indx].setValue(value); 146 } 147 148 /*****************************************************************************/ 149 /* */ 150 /* Method: getCookieValue */ 151 /* Description: gets the value of the specifed cookie */ 152 /* Parameters: name - name of the cookie */ 153 /* session - the session that contains this cookie */ 154 /* Returns: cookie value */ 155 /* Throws: InvalidCookieIDException if the cookie does not exist */ 156 /* */ 157 /*****************************************************************************/ 158 getCookieValue(String name, HttpClient session)159 public static String getCookieValue(String name, HttpClient session) 160 throws InvalidCookieIDException 161 { 162 163 Cookie [] cookies = session.getState().getCookies(); 164 165 int indx = findCookie(name, cookies); 166 167 return cookies[indx].getValue(); 168 } 169 170 /*****************************************************************************/ 171 /* */ 172 /* Method: getCookieSummary */ 173 /* Description: gets the summary of the specifed cookie */ 174 /* Parameters: name - name of the cookie */ 175 /* session - the session that contains this cookie */ 176 /* Returns: summary of a cookie */ 177 /* Throws: InvalidCookieIDException if the cookie does not exist */ 178 /* */ 179 /*****************************************************************************/ 180 getCookieSummary(String name, HttpClient session)181 public static HashMap getCookieSummary(String name, HttpClient session) 182 throws InvalidCookieIDException 183 { 184 185 Cookie [] cookies = session.getState().getCookies(); 186 187 int indx = findCookie(name, cookies); 188 189 return cookieSummary(cookies[indx]); 190 } 191 192 /*****************************************************************************/ 193 /* */ 194 /* Method: getCookieNames */ 195 /* Description: get a list of cookies associated with the specified session. */ 196 /* Parameters: session - the session to get the cookielist from */ 197 /* Returns: a list of cookies associated with this session */ 198 /* */ 199 /*****************************************************************************/ 200 getCookieNames(HttpClient session)201 public static Vector getCookieNames(HttpClient session) 202 { 203 204 Cookie [] cookies = session.getState().getCookies(); 205 Vector names = new Vector(); 206 for (int i = 0; i < cookies.length; i++) 207 names.addElement(cookies[i].getName()); 208 209 return names; 210 } 211 212 /*****************************************************************************/ 213 /* */ 214 /* Method: setCookiePolicy */ 215 /* Description: sets the cookie policy for the specified session. */ 216 /* Parameters: session - the session to get the cookielist from */ 217 /* policy - the cookie policy for the session. This must be a */ 218 /* valid type for HttpClient */ 219 /* NETSCAPE, RFC2109, COMPATIBILITY, IGNORE */ 220 /* a request for another policy type will result in use */ 221 /* of HttpClient's default cookie handling policy */ 222 /* Returns: void */ 223 /* */ 224 /*****************************************************************************/ 225 setCookiePolicy(String policy, HttpClient session)226 public static void setCookiePolicy(String policy, HttpClient session) 227 { 228 String cp = null; 229 if (policy.equalsIgnoreCase("NETSCAPE")) 230 cp = CookiePolicy.NETSCAPE; 231 else if (policy.equalsIgnoreCase("IGNORE")) 232 cp = CookiePolicy.IGNORE_COOKIES; 233 else if (policy.equalsIgnoreCase("RFC2109")) 234 cp = CookiePolicy.RFC_2109; 235 else if (policy.equalsIgnoreCase("COMPATIBILITY")) 236 cp = CookiePolicy.BROWSER_COMPATIBILITY; 237 else 238 cp = CookiePolicy.DEFAULT; 239 240 session.getParams().setCookiePolicy(cp); 241 } 242 243 /*****************************************************************************/ 244 /* */ 245 /* Method: purgeExpiredCookies */ 246 /* Description: Purge all cookies in a session which expire before a given */ 247 /* date. */ 248 /* Parameters: purgeDate - the date before which to expire cookies */ 249 /* session - the session that contains this cookies */ 250 /* Returns: void */ 251 /* */ 252 /*****************************************************************************/ 253 purgeExpiredCookies(Date purgeDate, HttpClient session)254 public static void purgeExpiredCookies(Date purgeDate, HttpClient session) 255 { 256 session.getState().purgeExpiredCookies(purgeDate); 257 } 258 259 /*****************************************************************************/ 260 /* */ 261 /* Method: listCookies */ 262 /* Description: get a list of cookies in the session. */ 263 /* Parameters: none */ 264 /* Returns: a list of link details */ 265 /* */ 266 /*****************************************************************************/ 267 listCookies(HttpClient session)268 public static HashMap[] listCookies(HttpClient session) 269 { 270 Cookie [] cookies = session.getState().getCookies(); 271 272 HashMap [] list = new HashMap[cookies.length]; 273 274 for (int i = 0; i < list.length; i++) 275 list[i] = cookieSummary(cookies[i]); 276 277 return list; 278 } 279 280 }