1 10 11 package org.mule.providers.http; 12 13 import org.apache.commons.httpclient.ChunkedInputStream; 14 import org.apache.commons.httpclient.ContentLengthInputStream; 15 import org.apache.commons.httpclient.Header; 16 import org.apache.commons.httpclient.HeaderElement; 17 import org.apache.commons.httpclient.HeaderGroup; 18 import org.apache.commons.httpclient.HttpStatus; 19 import org.apache.commons.httpclient.HttpVersion; 20 import org.apache.commons.httpclient.NameValuePair; 21 import org.apache.commons.httpclient.StatusLine; 22 import org.apache.commons.io.IOUtils; 23 import org.apache.commons.io.output.ByteArrayOutputStream; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.UnsupportedEncodingException ; 29 import java.util.Iterator ; 30 31 34 public class HttpResponse 35 { 36 37 public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; 38 39 private HttpVersion ver = HttpVersion.HTTP_1_1; 40 private int statusCode = HttpStatus.SC_OK; 41 private String phrase = HttpStatus.getStatusText(HttpStatus.SC_OK); 42 private HeaderGroup headers = new HeaderGroup(); 43 private InputStream entity = null; 44 private boolean keepAlive = false; 45 private boolean disableKeepAlive = false; 46 private String fallbackCharset = DEFAULT_CONTENT_CHARSET; 47 48 public HttpResponse() 49 { 50 super(); 51 } 52 53 public HttpResponse(final StatusLine statusline, final Header[] headers, final InputStream content) 54 throws IOException 55 { 56 super(); 57 if (statusline == null) 58 { 59 throw new IllegalArgumentException ("Status line may not be null"); 60 } 61 setStatusLine(HttpVersion.parse(statusline.getHttpVersion()), statusline.getStatusCode(), 62 statusline.getReasonPhrase()); 63 setHeaders(headers); 64 if (content != null) 65 { 66 InputStream in = content; 67 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH); 68 Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); 69 70 if (transferEncoding != null) 71 { 72 if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) 73 { 74 in = new ChunkedInputStream(in); 75 } 76 } 77 else if (contentLength != null) 78 { 79 long len = getContentLength(); 80 if (len >= 0) 81 { 82 in = new ContentLengthInputStream(in, len); 83 } 84 } 85 this.entity = in; 86 } 87 } 88 89 public void setStatusLine(final HttpVersion ver, int statuscode, final String phrase) 90 { 91 if (ver == null) 92 { 93 throw new IllegalArgumentException ("HTTP version may not be null"); 94 } 95 if (statuscode <= 0) 96 { 97 throw new IllegalArgumentException ("Status code may not be negative or zero"); 98 } 99 this.ver = ver; 100 this.statusCode = statuscode; 101 if (phrase != null) 102 { 103 this.phrase = phrase; 104 } 105 else 106 { 107 this.phrase = HttpStatus.getStatusText(statuscode); 108 } 109 } 110 111 public void setStatusLine(final HttpVersion ver, int statuscode) 112 { 113 setStatusLine(ver, statuscode, null); 114 } 115 116 public String getPhrase() 117 { 118 return this.phrase; 119 } 120 121 125 public int getStatuscode() 126 { 127 return this.getStatusCode(); 128 } 129 130 public int getStatusCode() 131 { 132 return this.statusCode; 133 } 134 135 public HttpVersion getHttpVersion() 136 { 137 return this.ver; 138 } 139 140 public String getStatusLine() 141 { 142 StringBuffer buffer = new StringBuffer (64); 143 buffer.append(this.ver); 144 buffer.append(' '); 145 buffer.append(this.statusCode); 146 if (this.phrase != null) 147 { 148 buffer.append(' '); 149 buffer.append(this.phrase); 150 } 151 return buffer.toString(); 152 } 153 154 public boolean containsHeader(final String name) 155 { 156 return this.headers.containsHeader(name); 157 } 158 159 public Header[] getHeaders() 160 { 161 return this.headers.getAllHeaders(); 162 } 163 164 public Header getFirstHeader(final String name) 165 { 166 return this.headers.getFirstHeader(name); 167 } 168 169 public void removeHeaders(final String s) 170 { 171 if (s == null) 172 { 173 return; 174 } 175 Header[] headers = this.headers.getHeaders(s); 176 for (int i = 0; i < headers.length; i++) 177 { 178 this.headers.removeHeader(headers[i]); 179 } 180 } 181 182 public void addHeader(final Header header) 183 { 184 if (header == null) 185 { 186 return; 187 } 188 this.headers.addHeader(header); 189 } 190 191 public void setHeader(final Header header) 192 { 193 if (header == null) 194 { 195 return; 196 } 197 removeHeaders(header.getName()); 198 addHeader(header); 199 } 200 201 public void setHeaders(final Header[] headers) 202 { 203 if (headers == null) 204 { 205 return; 206 } 207 this.headers.setHeaders(headers); 208 } 209 210 public Iterator getHeaderIterator() 211 { 212 return this.headers.getIterator(); 213 } 214 215 public String getCharset() 216 { 217 String charset = getFallbackCharset(); 218 Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE); 219 if (contenttype != null) 220 { 221 HeaderElement values[] = contenttype.getElements(); 222 if (values.length == 1) 223 { 224 NameValuePair param = values[0].getParameterByName("charset"); 225 if (param != null) 226 { 227 charset = param.getValue(); 228 } 229 } 230 } 231 return charset; 232 } 233 234 public long getContentLength() 235 { 236 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH); 237 if (contentLength != null) 238 { 239 try 240 { 241 return Long.parseLong(contentLength.getValue()); 242 } 243 catch (NumberFormatException e) 244 { 245 return -1; 246 } 247 } 248 else 249 { 250 return -1; 251 } 252 } 253 254 public void setBodyString(final String string) 255 { 256 if (string != null) 257 { 258 byte[] raw = null; 259 try 260 { 261 raw = string.getBytes(getCharset()); 262 } 263 catch (UnsupportedEncodingException e) 264 { 265 raw = string.getBytes(); 266 } 267 this.entity = new ByteArrayInputStream (raw); 268 if (!containsHeader(HttpConstants.HEADER_CONTENT_TYPE)) 269 { 270 setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE)); 271 } 272 setHeader(new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(raw.length))); 273 } 274 else 275 { 276 this.entity = null; 277 } 278 } 279 280 public void setBody(final InputStream instream) 281 { 282 this.entity = instream; 283 } 284 285 public InputStream getBody() 286 { 287 return this.entity; 288 } 289 290 public byte[] getBodyBytes() throws IOException 291 { 292 InputStream in = getBody(); 293 if (in != null) 294 { 295 ByteArrayOutputStream buffer = new ByteArrayOutputStream(8192); 296 IOUtils.copy(in, buffer); 297 return buffer.toByteArray(); 298 } 299 else 300 { 301 return null; 302 } 303 } 304 305 public String getBodyString() throws IOException 306 { 307 byte[] raw = getBodyBytes(); 308 if (raw != null) 309 { 310 return new String (raw, getCharset()); 311 } 312 else 313 { 314 return null; 315 } 316 } 317 318 public boolean isKeepAlive() 319 { 320 return !disableKeepAlive && keepAlive; 321 } 322 323 public void setKeepAlive(boolean keepAlive) 324 { 325 this.keepAlive = keepAlive; 326 } 327 328 public void disableKeepAlive(boolean keepalive) 329 { 330 disableKeepAlive = keepalive; 331 } 332 333 public String getFallbackCharset() 334 { 335 return fallbackCharset; 336 } 337 338 public void setFallbackCharset(String overrideCharset) 339 { 340 this.fallbackCharset = overrideCharset; 341 } 342 343 } 344 | Popular Tags |