001: /*
002: * $Id: XINSServletResponse.java,v 1.22 2007/09/18 08:45:08 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.servlet.container;
008:
009: import java.io.PrintWriter;
010: import java.io.StringWriter;
011: import java.util.Locale;
012: import javax.servlet.ServletOutputStream;
013: import javax.servlet.http.Cookie;
014: import javax.servlet.http.HttpServletResponse;
015: import org.xins.common.MandatoryArgumentChecker;
016: import org.xins.common.collections.BasicPropertyReader;
017: import org.xins.common.collections.PropertyReader;
018:
019: /**
020: * This class is an implementation of the HttpServletResponse that can be
021: * invoked locally.
022: *
023: * @version $Revision: 1.22 $ $Date: 2007/09/18 08:45:08 $
024: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
025: */
026: public class XINSServletResponse implements HttpServletResponse {
027:
028: /**
029: * The content type of the result. Initially <code>null</code>.
030: */
031: private String _contentType;
032:
033: /**
034: * The non-negative content length, or a negative number if unset.
035: * Initially a negative number.
036: */
037: private int _contentLength = -99;
038:
039: /**
040: * The status of the result.
041: */
042: private int _status;
043:
044: /**
045: * The encoding of the result. Must default to ISO-8859-1, according to the
046: * Java Servlet 2.4 Specification.
047: */
048: private String _encoding = "ISO-8859-1";
049:
050: /**
051: * The writer where to write the result.
052: */
053: private StringWriter _writer;
054:
055: /**
056: * The headers.
057: */
058: private BasicPropertyReader _headers = new BasicPropertyReader();
059:
060: /**
061: * Creates a new instance of <code>XINSServletResponse</code>.
062: */
063: public XINSServletResponse() {
064: }
065:
066: public void addDateHeader(String str, long param) {
067: throw new UnsupportedOperationException();
068: }
069:
070: public void setDateHeader(String str, long param) {
071: throw new UnsupportedOperationException();
072: }
073:
074: public String encodeUrl(String url) {
075: return url;
076: }
077:
078: public String encodeURL(String url) {
079: return url;
080: }
081:
082: public String encodeRedirectUrl(String str) {
083: throw new UnsupportedOperationException();
084: }
085:
086: public String encodeRedirectURL(String str) {
087: throw new UnsupportedOperationException();
088: }
089:
090: public boolean containsHeader(String str) {
091: return _headers.get(str) != null;
092: }
093:
094: public void sendRedirect(String location) {
095: setStatus(302);
096: setHeader("Location", location);
097: }
098:
099: /**
100: * Sets the content type.
101: *
102: * @param type
103: * the content type, cannot be <code>null</code>.
104: *
105: * @throws IllegalArgumentException
106: * if <code>type == null</code>.
107: */
108: public void setContentType(String type)
109: throws IllegalArgumentException {
110:
111: MandatoryArgumentChecker.check("type", type);
112:
113: setHeader("Content-Type", type);
114:
115: String search = "charset=";
116: int i = type.indexOf(search);
117: if (i >= 0) {
118: _encoding = type.substring(i + search.length());
119: }
120:
121: _contentType = type;
122: }
123:
124: public void setStatus(int sc) {
125: _status = sc;
126: }
127:
128: public void sendError(int sc) {
129: sendError(sc, null);
130: }
131:
132: public void setBufferSize(int param) {
133: throw new UnsupportedOperationException();
134: }
135:
136: /**
137: * Returns the content length.
138: *
139: * @return
140: * the (non-negative) content length if set, or a negative value if
141: * unset.
142: */
143: int getContentLength() {
144: return _contentLength;
145: }
146:
147: public void setContentLength(int param) {
148: _contentLength = param;
149: setIntHeader("Content-Length", param);
150: }
151:
152: public void addCookie(Cookie cookie) {
153: String cookieHeader = cookie.getName() + "="
154: + cookie.getValue();
155: if (cookie.getPath() != null) {
156: cookieHeader += "; path=" + cookie.getPath();
157: }
158: if (cookie.getDomain() != null) {
159: cookieHeader += "; domain=" + cookie.getDomain();
160: }
161: setHeader("Set-Cookie", cookieHeader);
162: }
163:
164: public void setLocale(Locale locale) {
165: throw new UnsupportedOperationException();
166: }
167:
168: public void setStatus(int param, String str) {
169: throw new UnsupportedOperationException();
170: }
171:
172: public void setIntHeader(String str, int param) {
173: setHeader(str, "" + param);
174: }
175:
176: public void addIntHeader(String str, int param) {
177: setHeader(str, "" + param);
178: }
179:
180: public void sendError(int sc, String msg) {
181: _status = sc;
182: }
183:
184: public void setHeader(String str, String str1) {
185: _headers.set(str, str1);
186: }
187:
188: public Locale getLocale() {
189: throw new UnsupportedOperationException();
190: }
191:
192: public String getCharacterEncoding() {
193: return _encoding;
194: }
195:
196: public int getBufferSize() {
197: throw new UnsupportedOperationException();
198: }
199:
200: public void flushBuffer() {
201: throw new UnsupportedOperationException();
202: }
203:
204: public void addHeader(String str, String str1) {
205: _headers.set(str, str1);
206: }
207:
208: public ServletOutputStream getOutputStream() {
209: throw new UnsupportedOperationException();
210: }
211:
212: public PrintWriter getWriter() {
213: _writer = new StringWriter();
214: return new PrintWriter(_writer);
215: }
216:
217: public boolean isCommitted() {
218: throw new UnsupportedOperationException();
219: }
220:
221: public void reset() {
222: throw new UnsupportedOperationException();
223: }
224:
225: public void resetBuffer() {
226: throw new UnsupportedOperationException();
227: }
228:
229: /**
230: * Gets the returned message from the servlet.
231: *
232: * @return
233: * the returned message or <code>null</code> if no message is returned.
234: */
235: public String getResult() {
236: if (_writer == null) {
237: return null;
238: }
239: return _writer.toString();
240: }
241:
242: /**
243: * Gets the status of the returned message.
244: *
245: * @return
246: * the HTTP status returned.
247: */
248: public int getStatus() {
249: return _status;
250: }
251:
252: /**
253: * Gets the type of the returned content.
254: *
255: * @return
256: * the content type, can be <code>null</code>.
257: *
258: * @see #setContentType(String)
259: */
260: public String getContentType() {
261: return _contentType;
262: }
263:
264: /**
265: * Gets the headers to return to the client.
266: *
267: * @return
268: * the headers, cannot be <code>null</code>.
269: *
270: * @since XINS 1.3.0
271: */
272: public PropertyReader getHeaders() {
273: return _headers;
274: }
275: }
|