1 16 package org.mortbay.http; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Modifier ; 22 import java.util.HashMap ; 23 24 import javax.servlet.http.Cookie ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 import org.mortbay.util.LogSupport; 29 import org.mortbay.util.StringUtil; 30 import org.mortbay.util.TypeUtil; 31 import org.mortbay.util.UrlEncoded; 32 33 34 35 47 public class HttpResponse extends HttpMessage 48 { 49 private static Log log = LogFactory.getLog(HttpResponse.class); 50 51 public final static int 52 __100_Continue = 100, 53 __101_Switching_Protocols = 101, 54 __102_Processing = 102, 55 __200_OK = 200, 56 __201_Created = 201, 57 __202_Accepted = 202, 58 __203_Non_Authoritative_Information = 203, 59 __204_No_Content = 204, 60 __205_Reset_Content = 205, 61 __206_Partial_Content = 206, 62 __207_Multi_Status = 207, 63 __300_Multiple_Choices = 300, 64 __301_Moved_Permanently = 301, 65 __302_Moved_Temporarily = 302, 66 __302_Found = 302, 67 __303_See_Other = 303, 68 __304_Not_Modified = 304, 69 __305_Use_Proxy = 305, 70 __400_Bad_Request = 400, 71 __401_Unauthorized = 401, 72 __402_Payment_Required = 402, 73 __403_Forbidden = 403, 74 __404_Not_Found = 404, 75 __405_Method_Not_Allowed = 405, 76 __406_Not_Acceptable = 406, 77 __407_Proxy_Authentication_Required = 407, 78 __408_Request_Timeout = 408, 79 __409_Conflict = 409, 80 __410_Gone = 410, 81 __411_Length_Required = 411, 82 __412_Precondition_Failed = 412, 83 __413_Request_Entity_Too_Large = 413, 84 __414_Request_URI_Too_Large = 414, 85 __415_Unsupported_Media_Type = 415, 86 __416_Requested_Range_Not_Satisfiable = 416, 87 __417_Expectation_Failed = 417, 88 __422_Unprocessable_Entity = 422, 89 __423_Locked = 423, 90 __424_Failed_Dependency = 424, 91 __500_Internal_Server_Error = 500, 92 __501_Not_Implemented = 501, 93 __502_Bad_Gateway = 502, 94 __503_Service_Unavailable = 503, 95 __504_Gateway_Timeout = 504, 96 __505_HTTP_Version_Not_Supported = 505, 97 __507_Insufficient_Storage = 507; 98 99 100 101 public final static HashMap __statusMsg = new HashMap (); 102 static 103 { 104 try 106 { 107 Field [] fields = org.mortbay.http.HttpResponse.class 108 .getDeclaredFields(); 109 for (int f=fields.length; f-->0 ;) 110 { 111 int m = fields[f].getModifiers(); 112 String name=fields[f].getName(); 113 if (Modifier.isFinal(m) && 114 Modifier.isStatic(m) && 115 fields[f].getType().equals(Integer.TYPE) && 116 name.startsWith("__") && 117 Character.isDigit(name.charAt(2))) 118 { 119 String message = name.substring(6); 120 message = message.replace('_',' '); 121 __statusMsg.put(fields[f].get(null),message); 122 } 123 } 124 } 125 catch (Exception e) 126 { 127 log.warn(LogSupport.EXCEPTION,e); 128 } 129 } 130 131 132 static byte[] __Continue; 133 static 134 { 135 try{ 136 __Continue="HTTP/1.1 100 Continue\015\012\015\012".getBytes(StringUtil.__ISO_8859_1); 137 } 138 catch (Exception e){log.fatal(e); System.exit(1);} 139 } 140 141 142 private int _status= __200_OK; 143 private String _reason; 144 private HttpContext _httpContext; 145 146 147 149 public HttpResponse() 150 { 151 _version=__HTTP_1_1; 152 _dotVersion=1; 153 _state=__MSG_EDITABLE; 154 } 155 156 157 160 public HttpResponse(HttpConnection connection) 161 { 162 super(connection); 163 _version=__HTTP_1_1; 164 _dotVersion=1; 165 _state=__MSG_EDITABLE; 166 } 167 168 169 172 public HttpContext getHttpContext() 173 { 174 return _httpContext; 175 } 176 177 178 181 public void setHttpContext(HttpContext context) 182 { 183 _httpContext=context; 184 } 185 186 187 190 public boolean isDirty() 191 { 192 return _status!=__200_OK || super.isDirty(); 193 } 194 195 196 204 public void reset() 205 { 206 if (isCommitted()) 207 throw new IllegalStateException ("Already committed"); 208 209 try 210 { 211 ((HttpOutputStream)getOutputStream()).resetBuffer(); 212 _status= __200_OK; 213 _reason=null; 214 super.reset(); 215 216 setField(HttpFields.__Date,getRequest().getTimeStampStr()); 217 if (!Version.isParanoid()) 218 setField(HttpFields.__Server,Version.getDetail()); 219 } 220 catch(Exception e) 221 { 222 log.warn(LogSupport.EXCEPTION,e); 223 throw new IllegalStateException (e.toString()); 224 } 225 } 226 227 228 229 232 public HttpRequest getRequest() 233 { 234 return getHttpRequest(); 235 } 236 237 238 242 public HttpRequest getHttpRequest() 243 { 244 if (_connection==null) 245 return null; 246 return _connection.getRequest(); 247 } 248 249 250 254 public void readHeader(HttpInputStream in) 255 throws IOException 256 { 257 _state=__MSG_BAD; 258 log.warn(LogSupport.NOT_IMPLEMENTED); 259 } 260 261 262 263 public void writeHeader(Writer writer) 264 throws IOException 265 { 266 if (_state!=__MSG_EDITABLE) 267 throw new IllegalStateException (__state[_state]+ 268 " is not EDITABLE"); 269 if (_header==null) 270 throw new IllegalStateException ("Response is destroyed"); 271 272 if (getHttpRequest().getDotVersion()>=0) 273 { 274 _state=__MSG_BAD; 275 writer.write(_version); 276 writer.write(' '); 277 writer.write('0'+((_status/100)%10)); 278 writer.write('0'+((_status/10)%10)); 279 writer.write('0'+(_status%10)); 280 writer.write(' '); 281 writer.write(getReason()); 282 writer.write(HttpFields.__CRLF); 283 _header.write(writer); 284 } 285 _state=__MSG_SENDING; 286 } 287 288 289 public int getStatus() 290 { 291 return _status; 292 } 293 294 295 public void setStatus(int status) 296 { 297 _status=status; 298 } 299 300 301 public String getReason() 302 { 303 if (_reason!=null) 304 return _reason; 305 _reason=(String )__statusMsg.get(TypeUtil.newInteger(_status)); 306 if (_reason==null) 307 _reason="unknown"; 308 return _reason; 309 } 310 311 312 public void setReason(String reason) 313 { 314 _reason=reason; 315 } 316 317 318 319 public void setStatus(int code,String message) 320 { 321 setStatus(code); 322 Integer code_integer=TypeUtil.newInteger(code); 323 if (message == null) 324 { 325 message = (String )__statusMsg.get(code_integer); 326 if (message==null) 327 message=""+code; 328 setReason(message); 329 } 330 else 331 setReason(UrlEncoded.encodeString(message)); 332 } 333 334 335 336 338 public void sendError(int code,String message) 339 throws IOException 340 { 341 setStatus(code,message); 342 343 HttpRequest request=getHttpRequest(); 345 346 if (code!=__204_No_Content && 348 code!=__304_Not_Modified && 349 code!=__206_Partial_Content && 350 code>=200) 351 { 352 if (getHttpContext()!=null) 353 { 354 Object o= 355 getHttpContext().getAttribute(HttpContext.__ErrorHandler); 356 if (o!=null && o instanceof HttpHandler) 357 ((HttpHandler)o).handle(request.getPath(), null, request, this); 358 } 359 } 360 else if (code!=__206_Partial_Content) 361 { 362 _header.remove(HttpFields.__ContentType); 363 _header.remove(HttpFields.__ContentLength); 364 _characterEncoding=null; 365 _mimeType=null; 366 } 367 368 commit(); 369 } 370 371 372 373 374 380 public void sendError(int code) 381 throws IOException 382 { 383 sendError(code,null); 384 } 385 386 387 393 public void sendRedirect(String location) 394 throws IOException 395 { 396 if (isCommitted()) 397 throw new IllegalStateException ("Commited"); 398 _header.put(HttpFields.__Location,location); 399 setStatus(__302_Moved_Temporarily); 400 commit(); 401 } 402 403 404 406 public void addSetCookie(String name, 407 String value) 408 { 409 _header.addSetCookie(new Cookie (name,value)); 410 } 411 412 413 415 public void addSetCookie(Cookie cookie) 416 { 417 _header.addSetCookie(cookie); 418 } 419 420 421 422 public void completing() 423 { 424 getHttpConnection().completing(); 425 } 426 427 428 431 public void commit() 432 throws IOException 433 { 434 if (!isCommitted()) 435 getOutputStream().flush(); 436 getHttpConnection().commit(); 437 } 438 439 440 442 void recycle(HttpConnection connection) 443 { 444 super.recycle(connection); 445 _status=__200_OK; 446 _reason=null; 447 _httpContext=null; 448 } 449 450 451 454 public void destroy() 455 { 456 _reason=null; 457 super.destroy(); 458 } 459 460 } 461 462 463 464 | Popular Tags |