KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > web > HttpResponseWrapper


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

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 JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * Class defines http server response.
48  */

49 public class HttpResponseWrapper {
50     
51     String JavaDoc charset;
52     String JavaDoc mimeType;
53     byte[] body;
54     Map JavaDoc headers = new HashMap JavaDoc();
55     
56     /**
57      * Constructor - defines response result based on specified HttpMethodBase instance.
58      * @param method
59      */

60     public HttpResponseWrapper(HttpMethodBase method) {
61         this.charset = method.getResponseCharSet();
62         
63         try {
64             this.body = method.getResponseBody();
65         } catch (IOException e) {
66             // todo: handle exception
67
}
68         
69         Header[] headerArray = method.getResponseHeaders();
70         if (headerArray != null) {
71             for (int i = 0; i < headerArray.length; i++) {
72                 String JavaDoc currName = headerArray[i].getName();
73                 String JavaDoc 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 JavaDoc getCharset() {
85         return this.charset;
86     }
87     
88     public String JavaDoc 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 JavaDoc getHeaders() {
101         return this.headers;
102     }
103     
104     public String JavaDoc getHeaderValue(String JavaDoc headerName) {
105         return (String JavaDoc) this.headers.get(headerName);
106     }
107     
108 }
109
Popular Tags