001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.jsp;
006:
007: import java.util.Locale;
008:
009: import java.io.IOException;
010: import java.io.PrintWriter;
011: import java.io.StringWriter;
012: import java.io.UnsupportedEncodingException;
013:
014: import java.net.URL;
015: import java.net.MalformedURLException;
016:
017: import javax.servlet.ServletOutputStream;
018: import javax.servlet.http.HttpServletResponse;
019: import javax.servlet.http.Cookie;
020:
021: import com.sun.portal.providers.ProviderException;
022: import com.sun.portal.providers.context.ProviderContext;
023:
024: import com.sun.portal.desktop.util.NSStringWriter;
025: import com.sun.portal.desktop.util.NSPrintWriter;
026: import com.sun.portal.desktop.RequestThreadLocalizer;
027:
028: /*
029: * The Response is an internal representation for the response that is
030: * generated by the JSP servlet. This version of Response adds a
031: * getLocation method to read the Location header after the response
032: * is completed. This is used for the processEdit functionality.
033: **/
034:
035: class Response implements HttpServletResponse {
036: private static final String headerMsg = "headers cannot be modified by JSP providers";
037:
038: protected String characterEncoding = System.getProperty(
039: "file.encoding", "iso-8859-1");
040: protected PrintWriter writer = null;
041: protected StringWriter stringWriter = new NSStringWriter(4096);
042: protected Request request;
043: protected HttpServletResponse servletResponse;
044: protected boolean usingStream = false;
045: protected boolean usingWriter = false;
046: protected boolean started = false;
047: protected boolean committed = false;
048: private String location = null;
049: protected int status = 200;
050: protected ProviderContext context;
051:
052: public Response(Request req, HttpServletResponse res,
053: ProviderContext c) {
054: request = req;
055: servletResponse = res;
056: context = c;
057: }
058:
059: public URL getLocation() throws MalformedURLException {
060: if (location == null) {
061: return null;
062: }
063: URL loc = new URL(location);
064: return loc;
065: }
066:
067: public StringBuffer getBody() throws ProviderException {
068: return stringWriter.getBuffer();
069: }
070:
071: public void addCookie(Cookie cookie) {
072: servletResponse.addCookie(cookie);
073: }
074:
075: public String encodeRedirectURL(String url) {
076: // we don't support url rewriting yet!
077: return url;
078: }
079:
080: public void resetBuffer() {
081: //do nothing
082: }
083:
084: /**
085: * @deprecated
086: */
087:
088: public String encodeRedirectUrl(String location) {
089: return encodeRedirectURL(location);
090: }
091:
092: public String encodeURL(String url) {
093: // we don't support url rewriting yet!
094: return url;
095: }
096:
097: /**
098: * @deprecated
099: */
100: public String encodeUrl(String url) {
101: return encodeURL(url);
102: }
103:
104: public String getCharacterEncoding() {
105: return characterEncoding;
106: }
107:
108: // mark whether or not we are being used as a stream or writer
109: public ServletOutputStream getOutputStream() {
110: throw new IllegalStateException("stream output not supported");
111: }
112:
113: public PrintWriter getWriter() throws IOException {
114: if (writer == null) {
115: writer = new NSPrintWriter(stringWriter);
116: }
117:
118: return writer;
119: }
120:
121: public void sendError(int sc) throws IOException {
122: status = sc;
123: }
124:
125: public void sendError(int sc, String msg) throws IOException {
126: status = sc;
127: sendBodyText(msg);
128: }
129:
130: public void sendRedirect(String location) throws IOException {
131: setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
132: this .location = makeAbsolute(location);
133: }
134:
135: public void setContentLength(int len) {
136: throw new UnsupportedOperationException(
137: "setContentLength is not available to JSP providers");
138: }
139:
140: public void setContentType(String type) {
141: // We do not support setting the content type of a provider
142: // However, the JSP compiler generated code calls this, so we have
143: // to silently ignore it rather than throwing an exception
144: }
145:
146: public boolean containsHeader(String name) {
147: throw new UnsupportedOperationException(headerMsg);
148: }
149:
150: public void setDateHeader(String name, long date) {
151: throw new UnsupportedOperationException(headerMsg);
152: }
153:
154: public void addDateHeader(String name, long value) {
155: throw new UnsupportedOperationException(headerMsg);
156: }
157:
158: public void setHeader(String name, String value) {
159: throw new UnsupportedOperationException(headerMsg);
160: }
161:
162: public void addHeader(String name, String value) {
163: servletResponse.addHeader(name, value);
164: }
165:
166: public void setIntHeader(String name, int value) {
167: throw new UnsupportedOperationException(headerMsg);
168: }
169:
170: public void addIntHeader(String name, int value) {
171: throw new UnsupportedOperationException(headerMsg);
172: }
173:
174: public void setStatus(int status) {
175: this .status = status;
176: }
177:
178: public int getStatus() {
179: return status;
180: }
181:
182: public void setBufferSize(int size) {
183: return;
184: }
185:
186: public int getBufferSize() {
187: return -1;
188: }
189:
190: public void reset() throws IllegalStateException {
191: //out.reset();
192: stringWriter = new NSStringWriter(4096);
193: }
194:
195: public boolean isCommitted() {
196: return false;
197: }
198:
199: public void flushBuffer() throws IOException {
200: return;
201: }
202:
203: public void setLocale(Locale loc) {
204: return;
205: }
206:
207: public Locale getLocale() {
208: return null;
209: }
210:
211: /**
212: *
213: * @deprecated
214: */
215:
216: public void setStatus(int sc, String msg) {
217: setStatus(sc);
218: }
219:
220: private String makeAbsolute(String location) {
221: URL url = null;
222: try {
223: // Try making a URL out of the location
224: // Throws an exception if the location is relative
225: url = new URL(location);
226: } catch (MalformedURLException e) {
227: try {
228: url = null == location ? new URL(context
229: .getDesktopURL(request)) : new URL(new URL(
230: context.getRequestServer(request).toString()),
231: location);
232: } catch (MalformedURLException ignored) {
233: // Give up
234: return location;
235: }
236: }
237: return url.toString();
238: }
239:
240: private void sendBodyText(String s) throws IOException {
241: try {
242: PrintWriter out = getWriter();
243: out.print(s);
244: } catch (IllegalStateException ise) {
245: ServletOutputStream out = getOutputStream();
246: out.print(s);
247: }
248: }
249:
250: private void close() throws IOException {
251: try {
252: PrintWriter out = getWriter();
253: out.close();
254: } catch (IllegalStateException ise) {
255: ServletOutputStream out = getOutputStream();
256: out.close();
257: }
258: }
259: }
|