1 20 package org.apache.cactus; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.StringReader ; 26 27 import java.net.HttpURLConnection ; 28 29 import java.util.Vector ; 30 31 import org.apache.cactus.internal.util.CookieUtil; 32 import org.apache.cactus.internal.util.IoUtil; 33 import org.apache.cactus.util.ChainedRuntimeException; 34 import org.apache.commons.httpclient.Header; 35 import org.apache.commons.httpclient.HttpException; 36 import org.apache.commons.httpclient.cookie.CookiePolicy; 37 import org.apache.commons.httpclient.cookie.CookieSpec; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 49 public class WebResponse 50 { 51 54 private static final Log LOGGER = LogFactory.getLog(WebResponse.class); 55 56 59 private HttpURLConnection connection; 60 61 64 private WebRequest request; 65 66 69 private String content; 70 71 77 public WebResponse(WebRequest theRequest, HttpURLConnection theConnection) 78 { 79 this.request = theRequest; 80 this.connection = theConnection; 81 } 82 83 87 public HttpURLConnection getConnection() 88 { 89 return this.connection; 90 } 91 92 96 public WebRequest getWebRequest() 97 { 98 return this.request; 99 } 100 101 104 public String getText() 105 { 106 if (this.content == null) 109 { 110 try 111 { 112 this.content = IoUtil.getText(this.connection.getInputStream()); 113 } 114 catch (IOException e) 115 { 116 throw new ChainedRuntimeException(e); 117 } 118 } 119 120 return this.content; 121 } 122 123 127 public String [] getTextAsArray() 128 { 129 Vector lines = new Vector (); 130 131 try 132 { 133 if (this.content == null) 135 { 136 getText(); 137 } 138 139 BufferedReader input = new BufferedReader ( 140 new StringReader (this.content)); 141 String str; 142 143 while (null != (str = input.readLine())) 144 { 145 lines.addElement(str); 146 } 147 148 input.close(); 149 } 150 catch (IOException e) 151 { 152 throw new ChainedRuntimeException(e); 153 } 154 155 String [] dummy = new String [lines.size()]; 157 158 return (String []) (lines.toArray(dummy)); 159 } 160 161 164 public InputStream getInputStream() 165 { 166 try 167 { 168 return this.connection.getInputStream(); 169 } 170 catch (IOException e) 171 { 172 throw new ChainedRuntimeException(e); 173 } 174 } 175 176 183 public Cookie getCookie(String theName) 184 { 185 Cookie result = null; 186 187 Cookie[] cookies = getCookies(); 188 189 for (int i = 0; i < cookies.length; i++) 190 { 191 if (cookies[i].getName().equals(theName)) 192 { 193 result = cookies[i]; 194 195 break; 196 } 197 } 198 199 return result; 200 } 201 202 210 public Cookie getCookieIgnoreCase(String theName) 211 { 212 Cookie result = null; 213 214 Cookie[] cookies = getCookies(); 215 216 for (int i = 0; i < cookies.length; i++) 217 { 218 if (cookies[i].getName().equalsIgnoreCase(theName)) 219 { 220 result = cookies[i]; 221 222 break; 223 } 224 } 225 226 return result; 227 } 228 229 232 public Cookie[] getCookies() 233 { 234 Cookie[] returnCookies = null; 235 236 String headerName = this.connection.getHeaderFieldKey(0); 239 String headerValue = this.connection.getHeaderField(0); 240 241 Vector cookieVector = new Vector (); 242 CookieSpec cookieSpec = CookiePolicy.getDefaultSpec(); 243 244 for (int i = 1; (headerName != null) || (headerValue != null); i++) 245 { 246 LOGGER.debug("Header name = [" + headerName + "]"); 247 LOGGER.debug("Header value = [" + headerValue + "]"); 248 249 if ((headerName != null) 250 && (headerName.toLowerCase().equals("set-cookie") 251 || headerName.toLowerCase().equals("set-cookie2"))) 252 { 253 org.apache.commons.httpclient.Cookie[] cookies; 255 try 256 { 257 cookies = cookieSpec.parse( 258 CookieUtil.getCookieDomain(getWebRequest(), 259 getConnection().getURL().getHost()), 260 CookieUtil.getCookiePort(getWebRequest(), 261 getConnection().getURL().getPort()), 262 CookieUtil.getCookiePath(getWebRequest(), 263 getConnection().getURL().getFile()), 264 false, new Header(headerName, headerValue)); 265 } 266 catch (HttpException e) 267 { 268 throw new ChainedRuntimeException( 269 "Error parsing cookies", e); 270 } 271 272 for (int j = 0; j < cookies.length; j++) 275 { 276 Cookie cookie = new Cookie(cookies[j].getDomain(), 277 cookies[j].getName(), cookies[j].getValue()); 278 279 cookie.setComment(cookies[j].getComment()); 280 cookie.setExpiryDate(cookies[j].getExpiryDate()); 281 cookie.setPath(cookies[j].getPath()); 282 cookie.setSecure(cookies[j].getSecure()); 283 284 cookieVector.addElement(cookie); 285 } 286 } 287 288 headerName = this.connection.getHeaderFieldKey(i); 289 headerValue = this.connection.getHeaderField(i); 290 } 291 292 returnCookies = new Cookie[cookieVector.size()]; 293 cookieVector.copyInto(returnCookies); 294 295 return returnCookies; 296 } 297 298 304 public int getStatusCode() 305 { 306 try 307 { 308 return this.connection.getResponseCode(); 309 } 310 catch (IOException e) 311 { 312 throw new ChainedRuntimeException(e); 313 } 314 } 315 316 } 317 | Popular Tags |