Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletResponse.java, revision 1.3
1.1 abaird 1: // JigsawHttpServletReponse.java
1.3 ! abaird 2: // $Id: JigsawHttpServletResponse.java,v 1.2 1996/12/26 14:10:46 abaird Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package w3c.jigsaw.servlet;
7:
8: import java.io.*;
9: import java.servlet.*;
10: import java.servlet.http.*;
11:
12: import w3c.www.mime.*;
13: import w3c.www.http.*;
14: import w3c.jigsaw.http.*;
1.2 abaird 15:
16: /**
17: * @author Alexandre Rafalovitch <alex@access.com.au>
18: * @author Anselm Baird-Smith <abaird@w3.org>
19: */
1.1 abaird 20:
21: public class JigsawHttpServletResponse implements HttpServletResponse
22: {
23: private final static int STATE_INITIAL = 0;
24: private final static int STATE_HEADERS_DONE = 1;
25: private final static int STATE_ALL_DONE = 2;
26:
27: int state = STATE_INITIAL;
28: Reply reply = null;
29: Request request = null;
1.3 ! abaird 30: JigsawServletOutputStream out = null;
! 31: DataOutputStream dout = null;
1.1 abaird 32:
33: private final void checkState(int state) {
34: if ( this.state != state )
35: throw new RuntimeException("Invalid state: "+this.state);
36: }
37:
1.3 ! abaird 38: // Setup the DataOutputStream (for reply emitting) and ServletOutputStream
! 39:
! 40: private synchronized final void computeOutputStream() {
! 41: if ( out == null ) {
! 42: dout = new DataOutputStream(request.getClient().getOutputStream());
! 43: out = new JigsawServletOutputStream(this, dout);
! 44: }
! 45: }
! 46:
1.1 abaird 47: protected final void emitHeaders() {
48: if ( state == STATE_HEADERS_DONE ) {
49: return;
50: } else {
51: checkState(STATE_INITIAL);
52: }
53: state = STATE_HEADERS_DONE;
54: // Emit the reply itself:
55: try {
1.3 ! abaird 56: computeOutputStream();
1.1 abaird 57: reply.emit(out);
58: reply.setStatus(HTTP.DONE);
59: } catch (IOException ex) {
60: // Again in deeep trouble here !
61: ex.printStackTrace();
62: }
63: }
64:
65: JigsawHttpServletResponse(Request request, Reply reply)
66: {
67: this.request = request;
68: this.reply = reply;
69: // FIXME Remove this once Servlets Keep-Alive is cleared out:
70: reply.setKeepConnection(false);
71: }
72:
73: public void setContentLength(int i)
74: {
75: checkState(STATE_INITIAL);
76: reply.setContentLength(i);
77: }
78:
79: public void setContentType(String spec)
80: {
81: checkState(STATE_INITIAL);
82: try {
83: MimeType type= new MimeType(spec);
84: reply.setContentType(type);
85: } catch(MimeTypeFormatException ex) {
86: // FIXME what should I do?
87: }
88: }
89:
90: public ServletOutputStream getOutputStream()
91: {
1.3 ! abaird 92: computeOutputStream();
! 93: return out;
1.1 abaird 94: }
95:
96: public void setStatus(int i, String reason)
97: {
98: checkState(STATE_INITIAL);
99: reply.setStatus(i);
100: reply.setReason(reason);
101: }
102:
103: public void setStatus(int i)
104: {
105: setStatus(i, reply.getStandardReason(i));
106: }
107:
108: public void setHeader(String name, String value)
109: {
110: checkState(STATE_INITIAL);
111: reply.setValue(name, value);
112: }
113:
114: public void setIntHeader(String name, int value)
115: {
116: setHeader(name, String.valueOf(value));
117: }
118:
119: public void setDateHeader(String name, long date)
120: {
121: // FIX
122: // I see no way to convert this into proper
123: // http type date string. Leave it to Jigsaw?
124: setHeader(name, String.valueOf(date));
125: }
126:
127: public void unsetHeader(String name)
128: {
129: setHeader(name, null);
130: }
131:
132: public void sendError(int i, String msg)
133: throws IOException
134: {
135: setStatus(i);
136: reply.setContent(msg);
137: state = STATE_ALL_DONE;
138: }
139:
140: public void sendError(int i)
141: throws IOException
142: {
143: setStatus(i);
144: reply.setContent(reply.getStandardReason(i));
145: state = STATE_ALL_DONE;
146: }
147:
148: public void sendRedirect(String url)
149: throws IOException
150: {
151: setStatus(SC_MOVED_TEMPORARILY);
152: reply.setLocation(url);
153: state = STATE_ALL_DONE;
154: }
155: }
Webmaster