001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.brokers.http;
021    
022    import java.io.ByteArrayOutputStream;
023    import java.io.IOException;
024    import java.io.OutputStreamWriter;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import org.dom4j.Document;
029    
030    /**
031     * Represents a HTTP Reponse
032     * 
033     * @author Lars Ivar Almli
034     */
035    public class HttpResponse {
036            byte[] data;
037            Map<String, String> headers = new HashMap<String, String>();
038            public String getHeader(String headerName) {
039                    return headers.get(headerName);
040            }
041            public void addHeader(String name, String value) {
042                    headers.put(name, value);
043            }
044            public void setData(String data) throws IOException {
045                    if (data == null)
046                            data = "";
047                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
048                    OutputStreamWriter w = new OutputStreamWriter(bos);
049                    w.write(data);
050                    w.flush();
051                    setData(bos.toByteArray());
052            }
053            public void setData(Document doc) throws IOException {
054                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
055                    OutputStreamWriter w = new OutputStreamWriter(bos);
056                    doc.write(w);
057                    w.flush();
058                    setData(bos.toByteArray());
059            }
060            public void setData(byte[] data) {
061                    this.data = data;
062                    headers.put("Content-Length", this.data.length + "");
063            }
064            public Map<String, String> getHeaders() {
065                    return headers;
066            }
067            public byte[] getData() {
068                    return data;
069            }
070    }