1 package com.quikj.server.framework; 2 3 import java.util.*; 4 5 public class AceHTTPMessage 6 { 7 public static final String CRLF = "\r\n"; 8 public static final int DEFAULT_HTTP_PORT = 80; 9 10 public static final int OK = 200; 12 public static final int CREATED = 201; 13 public static final int ACCEPTED = 202; 14 public static final int NO_CONTENT = 204; 15 16 public static final int MULTIPLE_CHOICES = 300; 17 public static final int MOVED_PERMANENTLY = 301; 18 public static final int MOVED_TEMPORARILY = 302; 19 public static final int NOT_MODIFIED = 304; 20 21 public static final int BAD_REQUEST = 400; 22 public static final int UNAUTHORIZED = 401; 23 public static final int FORBIDDEN = 403; 24 public static final int NOT_FOUND = 404; 25 26 public static final int INTERNAL_ERROR = 500; 27 public static final int NOT_IMPLEMENTED = 501; 28 public static final int BAD_GATEWAY = 502; 29 public static final int SERVICE_UNAVAILABLE = 503; 30 31 public AceHTTPMessage(boolean req_message, 34 String request, 35 String url, 36 boolean simple_req, 37 String http_version, 38 String http_status, 39 String http_reason) 40 { 41 requestMessage = req_message; 42 this.request = request; 43 this.url = url; 44 httpVersion = http_version; 45 httpStatus = http_status; 46 httpReason = http_reason; 47 simpleReq = simple_req; 48 49 headerFields = new Vector(); 50 } 51 52 public AceHTTPMessage(String request, 54 String url, 55 boolean simple_req, 56 String http_version) 57 { 58 this(true, request, url, simple_req, http_version, null, null); 59 } 60 61 62 public AceHTTPMessage(String http_version, 64 String http_status, 65 String http_reason) 66 { 67 this(false, null, null, false, http_version, http_status, http_reason); 68 } 69 70 71 public AceHTTPMessage(boolean req_message, 73 boolean simple_req, 74 String request, 75 String url, 76 String http_version, 77 String http_status, 78 String http_reason, 79 Vector header_fields, 80 char[] body, 81 int offset, 82 int length) 83 { 84 requestMessage = req_message; 85 simpleReq = simple_req; 86 this.request = request; 87 this.url = url; 88 httpVersion = http_version; 89 httpStatus = http_status; 90 httpReason = http_reason; 91 headerFields = header_fields; 92 this.body = body; 93 bodyOffset = offset; 94 bodyLength = length; 95 } 96 97 public AceHTTPMessage(boolean simple_req, 99 String request, 100 String url, 101 String http_version, 102 Vector header_fields, 103 char[] body, 104 int offset, 105 int length) 106 { 107 this(true, simple_req, request, url, http_version, null, null, header_fields, 108 body, offset, length); 109 } 110 111 public AceHTTPMessage(String http_version, 113 String http_status, 114 String http_reason, 115 Vector header_fields, 116 char[] body, 117 int offset, 118 int length) 119 { 120 this(false, false, null, null, http_version, http_status, http_reason, header_fields, 121 body, offset, length); 122 } 123 124 public void setURL(String url) 125 { 126 this.url = url; 127 } 128 129 public void addHeader(String header, String value) 130 { 131 headerFields.addElement(new AceHTTPHeader(header, value)); 132 } 133 134 public void addBody(char[] body, int offset, int length) 135 { 136 this.body = body; 137 bodyOffset = offset; 138 bodyLength = length; 139 } 140 141 public void addBody(char[] body) 142 { 143 this.body = body; 144 bodyOffset = 0; 145 bodyLength = body.length; 146 } 147 148 public void addBody(String body) 149 { 150 this.body = body.toCharArray(); 151 bodyOffset = 0; 152 bodyLength = this.body.length; 153 } 154 155 public String formatMessage() 156 { 157 StringBuffer buffer = null; 158 boolean to_cont = true; 159 160 if (requestMessage == true) 161 { 162 if (simpleReq == false) 163 { 164 if ((request == null) || (url == null) || (httpVersion == null)) 165 { 166 return null; 167 } 168 169 buffer = new StringBuffer (request 170 + ' ' 171 + url 172 + " HTTP/" 173 + httpVersion 174 + CRLF); 175 } 176 else 177 { 178 if ((request == null) || (url == null)) 179 { 180 return null; 181 } 182 183 buffer = new StringBuffer ("GET " 184 + url 185 + CRLF); 186 to_cont = false; 187 } 188 } 189 else 190 { 191 if ((httpVersion == null) || (httpStatus == null)) 192 { 193 return null; 194 } 195 196 buffer = new StringBuffer ("HTTP/" 197 + httpVersion 198 + ' ' 199 + httpStatus); 200 201 if (httpReason != null) 202 { 203 if (httpReason.length() > 0) 204 { 205 buffer.append(' ' 206 + httpReason); 207 } 208 } 209 buffer.append(CRLF); 210 } 211 212 if (to_cont == true) 213 { 214 if (headerFields != null) 215 { 216 if (headerFields.size() > 0) 217 { 218 Enumeration headers = headerFields.elements(); 219 220 while (headers.hasMoreElements() == true) 221 { 222 AceHTTPHeader header = (AceHTTPHeader)headers.nextElement(); 223 224 buffer.append(header.getKey() + ": " 225 + header.getValue() 226 + CRLF); 227 } 228 } 229 } 230 231 if (bodyLength > 0) 232 { 233 buffer.append("Content-Length:" 234 + bodyLength 235 + CRLF 236 + CRLF); 237 238 buffer.append(body, bodyOffset, bodyLength); 239 240 } 241 else 242 { 243 buffer.append(CRLF); 244 } 245 246 } 247 248 return buffer.toString(); 249 } 250 251 public String getRequestMethod() 252 { 253 if (request != null) 254 { 255 return new String (request); 256 } 257 else return null; 258 } 259 260 public String getURL() 261 { 262 if (url != null) 263 { 264 return new String (url); 265 } 266 else return null; 267 } 268 269 public String getHTTPVersion() 270 { 271 if (httpVersion != null) 272 { 273 return new String (httpVersion); 274 } 275 else return null; 276 } 277 278 public String getHTTPStatus() 279 { 280 if (httpStatus != null) 281 { 282 return new String (httpStatus); 283 } 284 else return null; 285 } 286 287 public String getHTTPReason() 288 { 289 if (httpReason != null) 290 { 291 return new String (httpReason); 292 } 293 else return null; 294 } 295 296 public String findHeaderField(String key) 297 { 298 int size = numHeaders(); 299 300 for (int i = 0; i < size; i++) 301 { 302 if (((AceHTTPHeader)headerFields.elementAt(i)).getKey().toUpperCase().equals(key.toUpperCase()) == true) 303 { 304 return ((AceHTTPHeader)headerFields.elementAt(i)).getValue(); 305 } 306 } 307 308 return null; 309 } 310 311 public AceHTTPHeader headerAt(int index) 312 { 313 return (AceHTTPHeader)headerFields.elementAt(index); 314 } 315 316 public int numHeaders() 317 { 318 return headerFields.size(); 319 } 320 321 public Vector getHeaderFields() 322 { 323 return headerFields; 324 } 325 326 public int getBodyLength() 327 { 328 return bodyLength; 329 } 330 331 public String getBody() 332 { 333 return new String (body, bodyOffset, bodyLength); 334 } 335 336 public char[] getBodyChar() 337 { 338 return body; 339 } 340 341 public boolean isHTTPRequest() 342 { 343 return requestMessage; 344 } 345 346 public boolean isSimpleReq() 347 { 348 return simpleReq; 349 } 350 351 private boolean requestMessage; 352 private String request; 353 private String url; 354 private String httpVersion; 355 private String httpStatus; 356 private String httpReason; 357 private Vector headerFields; 358 private char[] body; 359 private int bodyOffset = 0; 360 private int bodyLength = 0; 361 private boolean simpleReq; 362 } 363 364 | Popular Tags |