001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.web.micro.base;
028:
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.io.OutputStreamWriter;
032: import java.io.PrintWriter;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: import javax.servlet.ServletOutputStream;
039:
040: /**
041: * A standard implementation of the {@link ResponseCallback} API.
042: */
043: public class ResponseCallbackImpl implements ResponseCallback {
044:
045: private final String protocol = "HTTP/1.0";
046: private final Map headers = new HashMap();
047: private int status = 200;
048: private String message = "OK";
049: private boolean error = false;
050:
051: private final OutputStream os;
052: private ServletOutputStream out;
053: private PrintWriter writer;
054:
055: public ResponseCallbackImpl(OutputStream os) {
056: this .os = os;
057: }
058:
059: public boolean isError() {
060: return error;
061: }
062:
063: public void setStatus(int status, String message) {
064: this .status = status;
065: this .message = message;
066: this .error = false;
067: }
068:
069: public void setError(int status, String message) {
070: this .status = status;
071: this .message = message;
072: this .error = true;
073: }
074:
075: public String getStatusLine() {
076: return protocol + " " + status
077: + (message == null ? "" : (" " + message)) + "\r\n";
078: }
079:
080: public Map getHeaders() {
081: return headers;
082: }
083:
084: public String getHeaderLines() {
085: StringBuffer buf = new StringBuffer();
086: for (Iterator iter = headers.entrySet().iterator(); iter
087: .hasNext();) {
088: Map.Entry me = (Map.Entry) iter.next();
089: String name = (String) me.getKey();
090: Object o = me.getValue();
091: if (o instanceof String) {
092: buf.append(name).append(": ").append(o).append("\r\n");
093: } else {
094: List l = (List) o;
095: for (int i = 0; i < l.size(); i++) {
096: buf.append(name).append(": ").append(l.get(i))
097: .append("\r\n");
098: }
099: }
100: }
101: buf.append("\r\n");
102: return buf.toString();
103: }
104:
105: public ServletOutputStream createOutputStream() throws IOException {
106: if (out != null || writer != null) {
107: throw new IllegalStateException("Already created "
108: + (writer == null ? "OutputStream" : "Writer"));
109: }
110: if (os instanceof ServletOutputStream) {
111: out = (ServletOutputStream) os;
112: } else {
113: out = new ServletOutputStream() {
114: public void write(int b) throws IOException {
115: os.write(b);
116: }
117:
118: public void write(byte b[]) throws IOException {
119: os.write(b);
120: }
121:
122: public void write(byte b[], int off, int len)
123: throws IOException {
124: os.write(b, off, len);
125: }
126:
127: public void flush() throws IOException {
128: os.flush();
129: }
130:
131: public void close() throws IOException {
132: os.close();
133: }
134: };
135: }
136: sendHeaders();
137: return out;
138: }
139:
140: public PrintWriter createWriter() throws IOException {
141: if (out != null || writer != null) {
142: throw new IllegalStateException("Already created "
143: + (writer == null ? "OutputStream" : "Writer"));
144: }
145: writer = new PrintWriter(new OutputStreamWriter(os));
146: sendHeaders();
147: return writer;
148: }
149:
150: private void sendHeaders() throws IOException {
151: if (out != null) {
152: out.print(getStatusLine());
153: out.print(getHeaderLines());
154: out.flush();
155: } else if (writer != null) {
156: writer.print(getStatusLine());
157: writer.print(getHeaderLines());
158: writer.flush();
159: } else {
160: throw new IllegalStateException("Missing stream");
161: }
162: }
163:
164: public void finishResponse() throws IOException {
165: // close streams, let the "close()" do the flush.
166: if (out == null && writer == null) {
167: // create stream to write our headers
168: out = createOutputStream();
169: out.close();
170: } else if (out != null) {
171: out.close();
172: } else {
173: writer.close();
174: }
175: }
176: }
|