001: // Request.java
002: // $Id: Request.java,v 1.13 2003/04/01 16:45:49 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.OutputStream;
011:
012: import org.w3c.www.http.HttpMessage;
013: import org.w3c.www.http.HttpRequestMessage;
014:
015: /**
016: * The client side idea of a request.
017: * Requests are created only by the HttpManager, by cloning its template
018: * request that defines the default (application wide) request settings.
019: */
020:
021: public class Request extends HttpRequestMessage {
022: /**
023: * The manager that created this request.
024: */
025: protected HttpManager manager = null;
026: /**
027: * Are we allowed to interact with the user ?
028: */
029: protected boolean allowuserinteraction = false;
030: /**
031: * The request output stream, to PUT or POST data.
032: */
033: protected InputStream output = null;
034: /**
035: * The observer for the request, if any.
036: */
037: protected RequestObserver observer = null;
038: /**
039: * Can we pipeline that request, if appropriate support is detected ?
040: */
041: protected boolean pipeline = true;
042: /**
043: * Has this request been interrupted ?
044: */
045: protected boolean interrupted = false;
046: /**
047: * The server <em>currently</em> running the request, if any.
048: */
049: protected HttpServer server = null;
050:
051: /**
052: * Mark that request has being run by given server.
053: * @param server The server in charge for that request.
054: */
055:
056: protected synchronized void setServer(HttpServer server) {
057: this .server = server;
058: }
059:
060: /**
061: * Mark that request as no longer attached to a server object.
062: */
063:
064: protected synchronized void unsetServer() {
065: this .server = null;
066: }
067:
068: /**
069: * Enable/disable pipelining for that request.
070: * By default, this HTTP implementation tries it's best to use pipelining,
071: * if you take manual control over it, you're responsible for damages.
072: * @param onoff The pipelining toggle.
073: */
074:
075: public void setPipeline(boolean onoff) {
076: this .pipeline = onoff;
077: }
078:
079: /**
080: * End of header emiting, continue by sending optional output stream.
081: * @param out The output stream to write to.
082: */
083:
084: protected void endEmit(OutputStream out, int what)
085: throws IOException {
086: if ((what & EMIT_BODY) != EMIT_BODY)
087: return;
088: if (output != null) {
089: byte buf[] = new byte[1024];
090: int cnt = 0;
091: int total = 0;
092: while ((cnt = output.read(buf)) >= 0) {
093: total += cnt;
094: out.write(buf, 0, cnt);
095: }
096: // output.close();
097: }
098: return;
099: }
100:
101: /**
102: * Are we allowed to do some user interaction to run this request.
103: * @return A boolean, <strong>true</strong> if user interaction is allowed
104: * <strong>false</strong> otherwise.
105: */
106:
107: public boolean getAllowUserInteraction() {
108: return allowuserinteraction;
109: }
110:
111: /**
112: * Decide wether we are allowed to interact wit hthe user.
113: * @param onoff A boolean, <strong>true</strong> if interaction is allowed.
114: */
115:
116: public void setAllowUserInteraction(boolean onoff) {
117: allowuserinteraction = onoff;
118: }
119:
120: /**
121: * Interrupt that request processing.
122: * Do whatever it takes to interrupt that request processing, as soon
123: * as possible.
124: */
125:
126: public synchronized void interruptRequest() {
127: interrupted = true;
128: if (server != null)
129: server.interruptRequest(this );
130: }
131:
132: /**
133: * Has this request been interrupted ?
134: * @return A boolean.
135: */
136:
137: public boolean isInterrupted() {
138: return interrupted;
139: }
140:
141: /**
142: * Get this request's manager.
143: * @return The instance of the manager taking care of this request.
144: */
145:
146: public HttpManager getManager() {
147: return manager;
148: }
149:
150: /**
151: * Set this request output stream.
152: * As a side effect, setting the body of the request will disable
153: * pipelining. If you know what you're doing, you can turn it on again by
154: * using the <code>setPipeline</code> method.
155: * @param in The data to send to the server.
156: */
157:
158: public void setOutputStream(InputStream in) {
159: this .output = in;
160: this .pipeline = false;
161: }
162:
163: /**
164: * Does this request has an associated input stream ?
165: * @return A boolean <strong>true</strong> of it has.
166: */
167:
168: public boolean hasOutputStream() {
169: return output != null;
170: }
171:
172: /**
173: * Get the input stream to read that request body.
174: * <strong>Warning</strong> it is up to the caller to make sure to:
175: * <ul>
176: * <li>Reset the content length if any bytes is read out of the stream
177: * before the request is sent.
178: * <li>Reset the entire stream is the filter acting upon it just want to
179: * peek it (without consuming it).
180: * </ul>
181: * @return An InputStream instance, or <strong>null</strong> if the request
182: * has no body.
183: */
184:
185: public InputStream getOutputStream() {
186: return output;
187: }
188:
189: /**
190: * Create a Reply instance matching this request.
191: */
192:
193: public Reply makeReply(int status) {
194: return new Reply(major, minor, status);
195: }
196:
197: /**
198: * Set the observer for this request.
199: * @param observer The observer.
200: */
201:
202: public void setObserver(RequestObserver observer) {
203: this .observer = observer;
204: }
205:
206: /**
207: * Get the observer for this request.
208: * @return An instance of RequestObserver, or <strong>null</strong>
209: * if undefined.
210: */
211:
212: public RequestObserver getObserver() {
213: return observer;
214: }
215:
216: protected Request(HttpManager manager) {
217: super();
218: this.manager = manager;
219: }
220:
221: }
|