1 45 package org.openejb.server.httpd; 46 47 import java.io.ByteArrayOutputStream ; 48 import java.io.DataOutput ; 49 import java.io.DataOutputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.OutputStream ; 53 import java.io.PrintWriter ; 54 import java.net.URL ; 55 import java.net.URLConnection ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.Map ; 59 import java.util.Properties ; 60 import java.util.StringTokenizer ; 61 62 import org.openejb.util.JarUtils; 63 64 66 public class HttpResponseImpl implements HttpResponse { 67 68 69 private String responseString = "OK"; 70 71 72 private int code = 200; 73 74 75 private HashMap headers; 76 77 78 private byte[] body = new byte[0]; 79 80 81 private transient PrintWriter writer; 82 83 private transient ByteArrayOutputStream baos; 84 85 86 public static final String HTTP_VERSION = "HTTP/1.1"; 87 88 public static final String CRLF = "\r\n"; 89 90 public static final String SP = " "; 91 92 public static final String CSP = ": "; 93 94 public static String server; 95 96 private HttpRequestImpl request; 97 private URLConnection content; 98 99 protected void setRequest(HttpRequestImpl request){ 100 this.request = request; 101 } 102 103 107 public void setHeader(String name, String value){ 108 headers.put(name, value); 109 } 110 111 115 public String getHeader(String name){ 116 return (String ) headers.get(name); 117 } 118 119 122 public PrintWriter getPrintWriter(){ 123 return writer; 124 } 125 126 129 public OutputStream getOutputStream(){ 130 return baos; 131 } 132 133 146 public void setCode(int code){ 147 this.code = code; 148 } 149 150 153 public int getCode(){ 154 return code; 155 } 156 157 160 public void setContentType(String type){ 161 setHeader("Content-Type", type); 162 } 163 164 167 public String getContentType(){ 168 return getHeader("Content-Type"); 169 } 170 171 174 public void setResponseString(String responseString){ 175 this.responseString = responseString; 176 } 177 178 179 public void reset(){ 180 initBody(); 181 } 182 183 188 public void reset(int code, String responseString){ 189 setCode(code); 190 setResponseString(responseString); 191 initBody(); 192 } 193 194 195 196 197 198 protected HttpResponseImpl(){ 199 this(200, "OK", "text/html"); 200 } 201 202 208 protected HttpResponseImpl(int code, String responseString, String contentType){ 209 this.responseString = responseString; 210 this.headers = new HashMap (); 211 this.code = code; 212 213 setHeader("Server", getServerName()); 215 setHeader("Connection","close"); 216 setHeader("Content-Type",contentType); 217 218 initBody(); 220 } 221 222 232 protected void writeMessage(OutputStream output) throws IOException { 233 DataOutput out = new DataOutputStream (output); 234 closeMessage(); 237 writeResponseLine(out); 241 writeHeaders(out); 242 writeBody(out); 243 } 244 245 246 private void initBody(){ 247 baos = new ByteArrayOutputStream (); 248 writer = new PrintWriter ( baos ); 249 } 250 251 256 public String toString(){ 257 StringBuffer buf = new StringBuffer (40); 258 259 buf.append(HTTP_VERSION); 260 buf.append(SP); 261 buf.append(code+""); 262 buf.append(SP); 263 buf.append(responseString); 264 265 return buf.toString(); 266 } 267 268 270 private void closeMessage() { 271 setContentLengthHeader(); 272 setCookieHeader(); 273 } 274 275 276 private void setContentLengthHeader() { 277 if (content == null){ 278 writer.flush(); 279 writer.close(); 280 body = baos.toByteArray(); 281 setHeader("Content-Length", body.length+""); 282 } else { 283 setHeader("Content-Length", content.getContentLength()+""); 284 } 285 } 286 287 private void setCookieHeader() { 288 if (request == null || request.getSession() == null) return; 289 290 HttpSession session = request.getSession(false); 291 292 if (session == null) return; 293 294 StringBuffer cookie = new StringBuffer (); 295 cookie.append(HttpRequestImpl.EJBSESSIONID); 296 cookie.append('='); 297 cookie.append(session.getId()); 298 cookie.append("; Path=/"); 299 300 headers.put(HttpRequest.HEADER_SET_COOKIE, cookie.toString()); 301 } 302 303 311 private void writeResponseLine(DataOutput out) throws IOException { 312 out.writeBytes(HTTP_VERSION); 313 out.writeBytes(SP); 314 out.writeBytes(code+""); 315 out.writeBytes(SP); 316 out.writeBytes(responseString); 317 out.writeBytes(CRLF); 318 } 319 320 324 private void writeHeaders(DataOutput out) throws IOException { 325 Iterator it = headers.entrySet().iterator(); 326 327 while (it.hasNext()){ 328 Map.Entry entry = (Map.Entry )it.next(); 329 out.writeBytes(""+entry.getKey()); 330 out.writeBytes(CSP); 331 out.writeBytes(""+entry.getValue()); 332 out.writeBytes(CRLF); 333 } 334 } 335 336 340 private void writeBody(DataOutput out) throws IOException { 341 out.writeBytes(CRLF); 342 if (content == null){ 343 out.write(body); 344 } else { 345 InputStream in = content.getInputStream(); 346 byte buf[] = new byte[1024]; 347 for(int i = 0; (i = in.read(buf)) != -1; out.write(buf, 0, i)); 348 } 349 } 350 351 354 public String getServerName(){ 355 if (server == null) { 356 String version = "???"; 357 String os = "(unknown os)"; 358 359 try { 360 Properties versionInfo = new Properties (); 361 JarUtils.setHandlerSystemProperty(); 362 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 363 version = versionInfo.getProperty( "version" ); 364 os = System.getProperty("os.name")+"/"+System.getProperty("os.version")+" ("+System.getProperty("os.arch")+")"; 365 } catch (IOException e) { 366 } 367 368 server = "OpenEJB/" +version+ " "+os; 369 } 370 return server; 371 } 372 373 374 379 protected static HttpResponseImpl createError(String message){ 380 return createError(message, null); 381 } 382 383 388 protected static HttpResponseImpl createError(String message, Throwable t){ 389 HttpResponseImpl res = new HttpResponseImpl(500, "Internal Server Error", "text/html"); 390 PrintWriter body = res.getPrintWriter(); 391 392 body.println("<html>"); 393 body.println("<body>"); 394 body.println("<h3>Internal Server Error</h3>"); 395 body.println("<br><br>"); 396 System.out.println("ERROR"); 397 if (message != null) { 398 StringTokenizer msg = new StringTokenizer (message, "\n\r"); 399 400 while (msg.hasMoreTokens()) { 401 body.print( msg.nextToken() ); 402 body.println("<br>"); 403 } 404 } 405 406 if (t != null) { 407 try{ 408 body.println("<br><br>"); 409 body.println("Stack Trace:<br>"); 410 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 411 PrintWriter writer = new PrintWriter ( baos ); 412 t.printStackTrace(writer); 413 writer.flush(); 414 writer.close(); 415 message = new String (baos.toByteArray()); 416 StringTokenizer msg = new StringTokenizer (message, "\n\r"); 417 418 while (msg.hasMoreTokens()) { 419 body.print( msg.nextToken() ); 420 body.println("<br>"); 421 } 422 } catch (Exception e){ 423 } 424 } 425 426 body.println("</body>"); 427 body.println("</html>"); 428 429 return res; 430 } 431 432 436 protected static HttpResponseImpl createForbidden(String ip){ 437 HttpResponseImpl res = new HttpResponseImpl(403, "Forbidden", "text/html"); 438 PrintWriter body = res.getPrintWriter(); 439 440 body.println("<html>"); 441 body.println("<body>"); 442 body.println("<h3>Forbidden</h3>"); 443 body.println("<br><br>"); 444 body.println("IP address: " + ip + " is not registered on this server, please contact your system administrator."); 447 body.println("</body>"); 448 body.println("</html>"); 449 450 return res; 451 } 452 453 457 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 458 459 out.writeObject( responseString ); 460 461 462 out.writeInt( code ); 463 464 465 out.writeObject( headers ); 466 467 468 writer.flush(); 469 body = baos.toByteArray(); 470 out.writeObject( body ); 472 } 473 474 479 private void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException { 480 481 this.responseString = (String )in.readObject(); 482 483 484 this.code = in.readInt(); 485 486 487 this.headers = (HashMap ) in.readObject(); 488 489 490 body = (byte[]) in.readObject(); 491 baos = new ByteArrayOutputStream (); 493 baos.write( body ); 494 writer = new PrintWriter ( baos ); 495 496 } 497 500 public void setContent(URLConnection content) { 501 this.content = content; 502 } 503 504 public void setStatusCode(int code) { 505 this.setCode(code); 506 } 507 508 public int getStatusCode() { 509 return this.getCode(); 510 } 511 512 public void setStatusMessage(String responseString) { 513 this.setResponseString(responseString); 514 } 515 516 } | Popular Tags |