1 16 package org.apache.cocoon; 17 18 import com.gargoylesoftware.htmlunit.WebResponse; 19 import com.gargoylesoftware.htmlunit.SubmitMethod; 20 21 import org.apache.commons.httpclient.HttpClient; 22 import org.apache.commons.httpclient.HttpMethodBase; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.util.List ; 28 29 39 class HttpClientResponse 40 implements WebResponse 41 { 42 private URL url; 43 private HttpMethodBase method; 44 private int statusCode; 45 private long loadTime; 46 47 HttpClientResponse(URL url, HttpMethodBase method) 48 throws IOException 49 { 50 long t0 = System.currentTimeMillis(); 51 this.url = url; 52 this.method = method; 53 HttpClient client = new HttpClient(); 54 statusCode = client.executeMethod(method); 55 long t1 = System.currentTimeMillis(); 56 this.loadTime = t1-t0; 57 } 58 59 public int getStatusCode() { 60 return statusCode; 61 } 62 63 public String getStatusMessage() { 64 return method.getStatusText(); 65 } 66 67 public String getContentType() { 68 return method.getResponseHeader("Content-type").getValue(); 69 } 70 71 public String getContentAsString() { 72 try { 73 return method.getResponseBodyAsString(); 74 } 75 catch(IOException ex) { 76 return null; 77 } 78 } 79 80 public InputStream getContentAsStream() throws IOException { 81 return method.getResponseBodyAsStream(); 82 } 83 84 public URL getUrl() { 85 return url; 86 } 87 88 public SubmitMethod getRequestMethod() { 89 throw new Error ("HttpClientResponse.getRequestMethod() is not implemented yet"); 91 } 92 93 public List getResponseHeaders() { 94 throw new Error ("HttpClientResponse.getResponseHeaders() is not implemented yet"); 96 } 97 98 public String getResponseHeaderValue(String headerName) { 99 return method.getResponseHeader(headerName).getValue(); 100 } 101 102 public long getLoadTimeInMilliSeconds() { 103 return loadTime; 104 } 105 106 public String getContentCharSet() { 107 return method.getResponseCharSet(); 108 } 109 110 public byte[] getResponseBody() { 111 try { 112 return method.getResponseBody(); 113 } 114 catch(IOException ex) { 115 return null; 116 } 117 } 118 } 119 | Popular Tags |