Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletResponse.java, revision 1.2
1.1 abaird 1: // JigsawHttpServletReponse.java
1.2 ! abaird 2: // $Id: JigsawHttpServletResponse.java,v 1.1 1996/12/17 12:25:57 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;
30:
31: private final void checkState(int state) {
32: if ( this.state != state )
33: throw new RuntimeException("Invalid state: "+this.state);
34: }
35:
36: protected final void emitHeaders() {
37: if ( state == STATE_HEADERS_DONE ) {
38: return;
39: } else {
40: checkState(STATE_INITIAL);
41: }
42: state = STATE_HEADERS_DONE;
43: // Emit the reply itself:
44: try {
45: DataOutputStream out = request.getClient().getOutputStream();
46: reply.emit(out);
47: reply.setStatus(HTTP.DONE);
48: } catch (IOException ex) {
49: // Again in deeep trouble here !
50: ex.printStackTrace();
51: }
52: }
53:
54: JigsawHttpServletResponse(Request request, Reply reply)
55: {
56: this.request = request;
57: this.reply = reply;
58: // FIXME Remove this once Servlets Keep-Alive is cleared out:
59: reply.setKeepConnection(false);
60: }
61:
62: public void setContentLength(int i)
63: {
64: checkState(STATE_INITIAL);
65: reply.setContentLength(i);
66: }
67:
68: public void setContentType(String spec)
69: {
70: checkState(STATE_INITIAL);
71: try {
72: MimeType type= new MimeType(spec);
73: reply.setContentType(type);
74: } catch(MimeTypeFormatException ex) {
75: // FIXME what should I do?
76: }
77: }
78:
79: protected JigsawServletOutputStream os = null;
80: public ServletOutputStream getOutputStream()
81: {
82: if ( os != null )
83: return os;
84: // Emit the reply itself if needed:
85: emitHeaders();
86: DataOutputStream out = request.getClient().getOutputStream();
87: return (os = new JigsawServletOutputStream(this, out));
88: }
89:
90: public void setStatus(int i, String reason)
91: {
92: checkState(STATE_INITIAL);
93: reply.setStatus(i);
94: reply.setReason(reason);
95: }
96:
97: public void setStatus(int i)
98: {
99: setStatus(i, reply.getStandardReason(i));
100: }
101:
102: public void setHeader(String name, String value)
103: {
104: checkState(STATE_INITIAL);
105: reply.setValue(name, value);
106: }
107:
108: public void setIntHeader(String name, int value)
109: {
110: setHeader(name, String.valueOf(value));
111: }
112:
113: public void setDateHeader(String name, long date)
114: {
115: // FIX
116: // I see no way to convert this into proper
117: // http type date string. Leave it to Jigsaw?
118: setHeader(name, String.valueOf(date));
119: }
120:
121: public void unsetHeader(String name)
122: {
123: setHeader(name, null);
124: }
125:
126: public void sendError(int i, String msg)
127: throws IOException
128: {
129: setStatus(i);
130: reply.setContent(msg);
131: state = STATE_ALL_DONE;
132: }
133:
134: public void sendError(int i)
135: throws IOException
136: {
137: setStatus(i);
138: reply.setContent(reply.getStandardReason(i));
139: state = STATE_ALL_DONE;
140: }
141:
142: public void sendRedirect(String url)
143: throws IOException
144: {
145: setStatus(SC_MOVED_TEMPORARILY);
146: reply.setLocation(url);
147: state = STATE_ALL_DONE;
148: }
149: }
Webmaster