01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.brokers.http;
20:
21: import java.io.ByteArrayOutputStream;
22: import java.io.IOException;
23: import java.io.OutputStreamWriter;
24: import java.util.HashMap;
25: import java.util.Map;
26: import org.dom4j.Document;
27:
28: /**
29: * Represents a HTTP Reponse
30: *
31: * @author Lars Ivar Almli
32: */
33: public class HttpResponse {
34: byte[] data;
35: Map<String, String> headers = new HashMap<String, String>();
36:
37: public String getHeader(String headerName) {
38: return headers.get(headerName);
39: }
40:
41: public void addHeader(String name, String value) {
42: headers.put(name, value);
43: }
44:
45: public void setData(String data) throws IOException {
46: if (data == null)
47: data = "";
48: ByteArrayOutputStream bos = new ByteArrayOutputStream();
49: OutputStreamWriter w = new OutputStreamWriter(bos);
50: w.write(data);
51: w.flush();
52: setData(bos.toByteArray());
53: }
54:
55: public void setData(Document doc) throws IOException {
56: ByteArrayOutputStream bos = new ByteArrayOutputStream();
57: OutputStreamWriter w = new OutputStreamWriter(bos);
58: doc.write(w);
59: w.flush();
60: setData(bos.toByteArray());
61: }
62:
63: public void setData(byte[] data) {
64: this .data = data;
65: headers.put("Content-Length", this .data.length + "");
66: }
67:
68: public Map<String, String> getHeaders() {
69: return headers;
70: }
71:
72: public byte[] getData() {
73: return data;
74: }
75: }
|