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.PrintWriter;
031: import java.text.DateFormat;
032: import java.text.ParseException;
033: import java.text.SimpleDateFormat;
034: import java.util.ArrayList;
035: import java.util.Date;
036: import java.util.List;
037: import java.util.Locale;
038: import java.util.Map;
039:
040: import javax.servlet.ServletOutputStream;
041: import javax.servlet.http.Cookie;
042: import javax.servlet.http.HttpServletResponse;
043:
044: /**
045: * An implementation of the http-servlet-response interface.
046: * <p>
047: * Note that some methods are not currently supported, e.g. cookies and
048: * buffer resets.
049: */
050: public class HttpServletResponseImpl implements HttpServletResponse {
051:
052: private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
053:
054: private final ResponseCallback callback;
055: private boolean committed = false;
056: private boolean made_writer = false;
057:
058: public HttpServletResponseImpl(ResponseCallback callback) {
059: this .callback = callback;
060: }
061:
062: public boolean isCommitted() {
063: return committed;
064: }
065:
066: // status code
067: public void setStatus(int sc) {
068: setStatus(sc, getStatusMessage(sc));
069: }
070:
071: public void setStatus(int sc, String sm) {
072: callback.setStatus(sc, sm);
073: }
074:
075: public void sendError(int sc) throws IOException {
076: sendError(sc, getStatusMessage(sc));
077: }
078:
079: public void sendError(int sc, String sm) throws IOException {
080: callback.setError(sc, sm);
081: }
082:
083: public void sendRedirect(String location) throws IOException {
084: try {
085: String absolute = location; // convert to absolute url?
086: setStatus(SC_MOVED_TEMPORARILY);
087: setHeader("Location", absolute);
088: } catch (IllegalArgumentException e) {
089: setStatus(SC_NOT_FOUND);
090: }
091: }
092:
093: // encode
094: public String encodeRedirectURL(String url) {
095: return url;
096: }
097:
098: public String encodeRedirectUrl(String url) {
099: return encodeRedirectURL(url);
100: }
101:
102: public String encodeURL(String url) {
103: return url;
104: }
105:
106: public String encodeUrl(String url) {
107: return encodeURL(url);
108: }
109:
110: // header info
111: public void addHeader(String name, String value) {
112: if (isCommitted())
113: return;
114: Map headers = callback.getHeaders();
115: Object o = headers.get(name);
116: if (o == null) {
117: headers.put(name, value);
118: } else {
119: if (!(o instanceof List)) {
120: List l = new ArrayList();
121: l.add(o);
122: headers.put(name, l);
123: o = l;
124: }
125: ((List) o).add(value);
126: }
127: }
128:
129: public void setHeader(String name, String value) {
130: if (isCommitted())
131: return;
132: Map headers = callback.getHeaders();
133: headers.put(name, value);
134: }
135:
136: public boolean containsHeader(String name) {
137: Map headers = callback.getHeaders();
138: return headers.containsKey(name);
139: }
140:
141: public void setContentType(String type) {
142: addHeader("Content-Type", type);
143: }
144:
145: public void setContentLength(int len) {
146: setIntHeader("Content-Length", len);
147: }
148:
149: public void setDateHeader(String name, long date) {
150: DateFormat format = new SimpleDateFormat(DATE_FORMAT);
151: setHeader(name, format.format(new Date(date)));
152: }
153:
154: public void addDateHeader(String name, long date) {
155: DateFormat format = new SimpleDateFormat(DATE_FORMAT);
156: addHeader(name, format.format(new Date(date)));
157: }
158:
159: public void setIntHeader(String name, int value) {
160: setHeader(name, Integer.toString(value));
161: }
162:
163: public void addIntHeader(String name, int value) {
164: addHeader(name, Integer.toString(value));
165: }
166:
167: // stream
168: public PrintWriter getWriter() throws IOException {
169: if (made_writer) {
170: throw new IllegalStateException("Already made writer");
171: }
172: made_writer = true;
173: return callback.createWriter();
174: }
175:
176: public ServletOutputStream getOutputStream() throws IOException {
177: if (made_writer) {
178: throw new IllegalStateException("Already made writer");
179: }
180: made_writer = true;
181: return callback.createOutputStream();
182: }
183:
184: private String getStatusMessage(int sc) {
185: switch (sc) {
186: case SC_OK:
187: return "OK";
188: case SC_MOVED_TEMPORARILY:
189: return "Moved Temporarily";
190: case SC_NOT_FOUND:
191: return "Not Found";
192: case SC_NOT_IMPLEMENTED:
193: return "Not Implemented";
194: case SC_NOT_MODIFIED:
195: return "Not Modified";
196: case SC_REQUEST_TIMEOUT:
197: return "Request Timeout";
198: case SC_SERVICE_UNAVAILABLE:
199: return "Service Unavailable";
200: default:
201: return "HTTP Response Status " + sc;
202: }
203: }
204:
205: // unsupported:
206: // HttpServletResponse:
207: public void addCookie(Cookie cookie) {
208: die();
209: }
210:
211: // ServletResponse:
212: public String getCharacterEncoding() {
213: die();
214: return null;
215: }
216:
217: public void setBufferSize(int size) {
218: die();
219: }
220:
221: public int getBufferSize() {
222: die();
223: return -1;
224: }
225:
226: public void flushBuffer() throws IOException {
227: die();
228: }
229:
230: public void resetBuffer() {
231: die();
232: }
233:
234: public void reset() {
235: die();
236: }
237:
238: public void setLocale(Locale loc) {
239: die();
240: }
241:
242: public Locale getLocale() {
243: die();
244: return null;
245: }
246:
247: private void die() {
248: throw new UnsupportedOperationException();
249: }
250: }
|