1 37 package org.webharvest.runtime.web; 38 39 import org.apache.commons.httpclient.Header; 40 import org.apache.commons.httpclient.HttpMethodBase; 41 42 import java.io.*; 43 import java.util.HashMap ; 44 import java.util.Map ; 45 46 49 public class HttpResponseWrapper { 50 51 String charset; 52 String mimeType; 53 byte[] body; 54 Map headers = new HashMap (); 55 56 60 public HttpResponseWrapper(HttpMethodBase method) { 61 this.charset = method.getResponseCharSet(); 62 63 try { 64 this.body = method.getResponseBody(); 65 } catch (IOException e) { 66 } 68 69 Header[] headerArray = method.getResponseHeaders(); 70 if (headerArray != null) { 71 for (int i = 0; i < headerArray.length; i++) { 72 String currName = headerArray[i].getName(); 73 String currValue = headerArray[i].getValue(); 74 headers.put(currName, currValue); 75 76 if ("content-type".equalsIgnoreCase(currName)) { 77 int index = currValue.indexOf(';'); 78 this.mimeType = index > 0 ? currValue.substring(0, index) : currValue; 79 } 80 } 81 } 82 } 83 84 public String getCharset() { 85 return this.charset; 86 } 87 88 public String getMimeType() { 89 return this.mimeType; 90 } 91 92 public byte[] getBody() { 93 return this.body; 94 } 95 96 public InputStream getBodyAsInputStream() { 97 return new ByteArrayInputStream(body); 98 } 99 100 public Map getHeaders() { 101 return this.headers; 102 } 103 104 public String getHeaderValue(String headerName) { 105 return (String ) this.headers.get(headerName); 106 } 107 108 } 109 | Popular Tags |