001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of Web-Harvest may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
035: subject line.
036: */
037: package org.webharvest.runtime.web;
038:
039: import org.apache.commons.httpclient.Header;
040: import org.apache.commons.httpclient.HttpMethodBase;
041:
042: import java.io.*;
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: /**
047: * Class defines http server response.
048: */
049: public class HttpResponseWrapper {
050:
051: String charset;
052: String mimeType;
053: byte[] body;
054: Map headers = new HashMap();
055:
056: /**
057: * Constructor - defines response result based on specified HttpMethodBase instance.
058: * @param method
059: */
060: public HttpResponseWrapper(HttpMethodBase method) {
061: this .charset = method.getResponseCharSet();
062:
063: try {
064: this .body = method.getResponseBody();
065: } catch (IOException e) {
066: // todo: handle exception
067: }
068:
069: Header[] headerArray = method.getResponseHeaders();
070: if (headerArray != null) {
071: for (int i = 0; i < headerArray.length; i++) {
072: String currName = headerArray[i].getName();
073: String currValue = headerArray[i].getValue();
074: headers.put(currName, currValue);
075:
076: if ("content-type".equalsIgnoreCase(currName)) {
077: int index = currValue.indexOf(';');
078: this .mimeType = index > 0 ? currValue.substring(0,
079: index) : currValue;
080: }
081: }
082: }
083: }
084:
085: public String getCharset() {
086: return this .charset;
087: }
088:
089: public String getMimeType() {
090: return this .mimeType;
091: }
092:
093: public byte[] getBody() {
094: return this .body;
095: }
096:
097: public InputStream getBodyAsInputStream() {
098: return new ByteArrayInputStream(body);
099: }
100:
101: public Map getHeaders() {
102: return this .headers;
103: }
104:
105: public String getHeaderValue(String headerName) {
106: return (String) this.headers.get(headerName);
107: }
108:
109: }
|