001: package vicazh.hyperpool.stream.net.http;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: /**
007: * This class implements an http stream from server
008: *
009: * @author Victor Zhigunov
010: * @version 0.4.0
011: */
012: public class ServerStream extends Stream {
013:
014: public static final int ACCEPTED = 202;
015:
016: public static final int BAD_GATEWAY = 502;
017:
018: public static final int BAD_REQUEST = 400;
019:
020: public static final int CONFLICT = 409;
021:
022: public static final int CONTINUE = 100;
023:
024: public static final int CREATED = 201;
025:
026: public static final int EXPECTATION_FAILED = 417;
027:
028: public static final int FORBIDDEN = 403;
029:
030: public static final int FOUND = 302;
031:
032: public static final int GATEWAY_TIMEOUT = 504;
033:
034: public static final int GONE = 410;
035:
036: public static final int HTTP_VERSION_NOT_SUPPORTED = 505;
037:
038: public static final int INTERNAL_SERVER_ERROR = 500;
039:
040: public static final int LENGTH_REQUIRED = 411;
041:
042: public static final int METHOD_NOT_ALLOWED = 405;
043:
044: public static final int MOVED_PERMANENTLY = 301;
045:
046: public static final int MOVED_TEMPORARILY = 302;
047:
048: public static final int MULTIPLE_CHOICES = 300;
049:
050: public static final int NO_CONTENT = 204;
051:
052: public static final int NON_AUTHORITATIVE_INFORMATION = 203;
053:
054: public static final int NOT_ACCEPTABLE = 406;
055:
056: public static final int NOT_FOUND = 404;
057:
058: public static final int NOT_IMPLEMENTED = 501;
059:
060: public static final int NOT_MODIFIED = 304;
061:
062: public static final int OK = 200;
063:
064: public static final int PARTIAL_CONTENT = 206;
065:
066: public static final int PAYMENT_REQUIRED = 402;
067:
068: public static final int PRECONDITION_FAILED = 412;
069:
070: public static final int PROXY_AUTHENTICATION_REQUIRED = 407;
071:
072: public static final int REQUEST_ENTITY_TOO_LARGE = 413;
073:
074: public static final int REQUEST_TIMEOUT = 408;
075:
076: public static final int REQUEST_URI_TOO_LONG = 414;
077:
078: public static final int REQUESTED_RANGE_NOT_SATISFIABLE = 416;
079:
080: public static final int RESET_CONTENT = 205;
081:
082: public static final int SEE_OTHER = 303;
083:
084: public static final int SERVICE_UNAVAILABLE = 503;
085:
086: public static final int SWITCHING_PROTOCOLS = 101;
087:
088: public static final int TEMPORARY_REDIRECT = 307;
089:
090: public static final int UNAUTHORIZED = 401;
091:
092: public static final int UNSUPPORTED_MEDIA_TYPE = 415;
093:
094: public static final int USE_PROXY = 305;
095:
096: public ServerStream() {
097: }
098:
099: private static final char SP = ' ';
100:
101: private String version;
102:
103: /**
104: * Set the version
105: */
106: public void setVersion(String version) {
107: this .version = version;
108: }
109:
110: /**
111: * Get the version
112: */
113: public String getVersion() {
114: return version;
115: }
116:
117: private int code;
118:
119: /**
120: * Set the code
121: */
122: public void setCode(int code) {
123: this .code = code;
124: }
125:
126: /**
127: * Get the code
128: */
129: public int getCode() {
130: return code;
131: }
132:
133: private String message;
134:
135: /**
136: * Set the message
137: */
138: public void setMessage(String message) {
139: this .message = message;
140: }
141:
142: /**
143: * Get the message
144: */
145: public String getMessage() {
146: return message;
147: }
148:
149: /**
150: * @param session
151: * parent session
152: * @param outputstream
153: * linked output stream
154: */
155: public ServerStream(Session session, OutputStream outputstream) {
156: super (session, outputstream);
157: }
158:
159: private final static String START = "HTTP/";
160:
161: public void write(int b) throws IOException {
162: super .write(b);
163: if (mode != HEAD || stream.size() != START.length())
164: return;
165: byte[] a = stream.toByteArray();
166: if (new String(a).equalsIgnoreCase(START))
167: return;
168: head("HTTP/1.0", OK, "");
169: header();
170: for (int i = 0; i < a.length; i++)
171: content(a[i]);
172: }
173:
174: public void head(String s) throws IOException {
175: StringTokenizer stringtokenizer = new StringTokenizer(s);
176: version = stringtokenizer.nextToken();
177: code = Integer.parseInt(stringtokenizer.nextToken());
178: if (stringtokenizer.hasMoreTokens())
179: message = stringtokenizer.nextToken();
180: else
181: message = "";
182: while (stringtokenizer.hasMoreTokens())
183: message += SP + stringtokenizer.nextToken();
184: head(version, code, message);
185: }
186:
187: /**
188: * Writes the http parameters to this stream
189: *
190: * @param version
191: * the http version
192: * @param code
193: * the http return code
194: * @param message
195: * the http return message
196: */
197: public void head(String version, int code, String message)
198: throws IOException {
199: this .version = version;
200: this .code = code;
201: this .message = message;
202: super .head(version + SP + String.valueOf(code) + SP + message);
203: }
204:
205: public void header() throws IOException {
206: super .header();
207: if (code == CONTINUE)
208: mode = HEAD;
209: }
210:
211: protected boolean isContent() {
212: return code == CONTINUE
213: || ((code == OK || getFields().containsKey(
214: "content-type"))
215: && length == -1
216: || code == PARTIAL_CONTENT
217: || length > 0
218: || version.equalsIgnoreCase("http/1.0") || isChunked)
219: && (session.getClient().getMethod() == null || !session
220: .getClient().getMethod().equalsIgnoreCase(
221: "head"));
222: }
223:
224: public void end() {
225: super .end();
226: session.close();
227: }
228:
229: protected void set() throws IOException {
230: synchronized (session) {
231: if (((Connection) connection).getSessions().size() == 0)
232: try {
233: session.wait();
234: } catch (InterruptedException e) {
235: }
236: }
237: Session s = ((Connection) connection).getSessions().get(0);
238: s.setServer(outputstream);
239: connection.getServer().outputstream = s.getServer();
240: }
241: }
|