1 45 package org.openejb.webadmin.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.webadmin.HttpRequest; 63 import org.openejb.webadmin.HttpResponse; 64 import org.openejb.webadmin.HttpSession; 65 import org.openejb.util.JarUtils; 66 67 71 public class HttpResponseImpl implements HttpResponse { 72 73 74 private String responseString = "OK"; 75 76 77 private int code = 200; 78 79 80 private HashMap headers; 81 82 83 private byte[] body = new byte[0]; 84 85 86 private transient PrintWriter writer; 87 88 private transient ByteArrayOutputStream baos; 89 90 91 public static final String HTTP_VERSION = "HTTP/1.1"; 92 93 public static final String CRLF = "\r\n"; 94 95 public static final String SP = " "; 96 97 public static final String CSP = ": "; 98 99 public static String server; 100 101 private HttpRequestImpl request; 102 private URLConnection content; 103 104 protected void setRequest(HttpRequestImpl request){ 105 this.request = request; 106 } 107 108 112 public void setHeader(String name, String value){ 113 headers.put(name, value); 114 } 115 116 120 public String getHeader(String name){ 121 return (String ) headers.get(name); 122 } 123 124 127 public PrintWriter getPrintWriter(){ 128 return writer; 129 } 130 131 134 public OutputStream getOutputStream(){ 135 return baos; 136 } 137 138 151 public void setCode(int code){ 152 this.code = code; 153 } 154 155 158 public int getCode(){ 159 return code; 160 } 161 162 165 public void setContentType(String type){ 166 setHeader("Content-Type", type); 167 } 168 169 172 public String getContentType(){ 173 return getHeader("Content-Type"); 174 } 175 176 179 public void setResponseString(String responseString){ 180 this.responseString = responseString; 181 } 182 183 186 public String getResponseString(){ 187 return responseString; 188 } 189 190 191 public void reset(){ 192 initBody(); 193 } 194 195 200 public void reset(int code, String responseString){ 201 setCode(code); 202 setResponseString(responseString); 203 initBody(); 204 } 205 206 207 208 209 210 protected HttpResponseImpl(){ 211 this(200, "OK", "text/html"); 212 } 213 214 220 protected HttpResponseImpl(int code, String responseString, String contentType){ 221 this.responseString = responseString; 222 this.headers = new HashMap (); 223 this.code = code; 224 225 setHeader("Server", getServerName()); 227 setHeader("Connection","close"); 228 setHeader("Content-Type",contentType); 229 230 initBody(); 232 } 233 234 244 protected void writeMessage(OutputStream output) throws IOException { 245 DataOutput out = new DataOutputStream (output); 246 DataOutput log = new DataOutputStream (System.out); 247 closeMessage(); 249 writeResponseLine(out); 253 writeHeaders(out); 254 writeBody(out); 255 } 256 257 258 private void initBody(){ 259 baos = new ByteArrayOutputStream (); 260 writer = new PrintWriter ( baos ); 261 } 262 263 268 public String toString(){ 269 StringBuffer buf = new StringBuffer (40); 270 271 buf.append(HTTP_VERSION); 272 buf.append(SP); 273 buf.append(code+""); 274 buf.append(SP); 275 buf.append(responseString); 276 277 return buf.toString(); 278 } 279 280 283 private void closeMessage() throws IOException { 284 setContentLengthHeader(); 285 setCookieHeader(); 286 } 287 288 289 private void setContentLengthHeader() { 290 if (content == null){ 291 writer.flush(); 292 writer.close(); 293 body = baos.toByteArray(); 294 setHeader("Content-Length", body.length+""); 295 } else { 296 setHeader("Content-Length", content.getContentLength()+""); 297 } 298 } 299 300 private void setCookieHeader() { 301 if (request == null || request.getSession() == null) return; 302 303 HttpSession session = request.getSession(false); 304 305 if (session == null) return; 306 307 StringBuffer cookie = new StringBuffer (); 308 cookie.append(HttpRequestImpl.EJBSESSIONID); 309 cookie.append('='); 310 cookie.append(session.getId()); 311 cookie.append("; Path=/"); 312 313 headers.put(HttpRequest.HEADER_SET_COOKIE, cookie.toString()); 314 } 315 316 324 private void writeResponseLine(DataOutput out) throws IOException { 325 out.writeBytes(HTTP_VERSION); 326 out.writeBytes(SP); 327 out.writeBytes(code+""); 328 out.writeBytes(SP); 329 out.writeBytes(responseString); 330 out.writeBytes(CRLF); 331 } 332 333 337 private void writeHeaders(DataOutput out) throws IOException { 338 Iterator it = headers.entrySet().iterator(); 339 340 while (it.hasNext()){ 341 Map.Entry entry = (Map.Entry )it.next(); 342 out.writeBytes(""+entry.getKey()); 343 out.writeBytes(CSP); 344 out.writeBytes(""+entry.getValue()); 345 out.writeBytes(CRLF); 346 } 347 } 348 349 353 private void writeBody(DataOutput out) throws IOException { 354 out.writeBytes(CRLF); 355 if (content == null){ 356 out.write(body); 357 } else { 358 InputStream in = content.getInputStream(); 359 byte buf[] = new byte[1024]; 360 for(int i = 0; (i = in.read(buf)) != -1; out.write(buf, 0, i)); 361 } 362 } 363 364 367 public String getServerName(){ 368 if (server == null) { 369 String version = "???"; 370 String os = "(unknown os)"; 371 372 try { 373 Properties versionInfo = new Properties (); 374 JarUtils.setHandlerSystemProperty(); 375 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 376 version = versionInfo.getProperty( "version" ); 377 os = System.getProperty("os.name")+"/"+System.getProperty("os.version")+" ("+System.getProperty("os.arch")+")"; 378 } catch (java.io.IOException e) { 379 } 380 381 server = "OpenEJB/" +version+ " "+os; 382 } 383 return server; 384 } 385 386 387 392 protected static HttpResponseImpl createError(String message){ 393 return createError(message, null); 394 } 395 396 401 protected static HttpResponseImpl createError(String message, Throwable t){ 402 HttpResponseImpl res = new HttpResponseImpl(500, "Internal Server Error", "text/html"); 403 java.io.PrintWriter body = res.getPrintWriter(); 404 405 body.println("<html>"); 406 body.println("<body>"); 407 body.println("<h3>Internal Server Error</h3>"); 408 body.println("<br><br>"); 409 410 if (message != null) { 411 StringTokenizer msg = new StringTokenizer (message, "\n\r"); 412 413 while (msg.hasMoreTokens()) { 414 body.print( msg.nextToken() ); 415 body.println("<br>"); 416 } 417 } 418 419 if (t != null) { 420 try{ 421 body.println("<br><br>"); 422 body.println("Stack Trace:<br>"); 423 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 424 PrintWriter writer = new PrintWriter ( baos ); 425 t.printStackTrace(writer); 426 writer.flush(); 427 writer.close(); 428 message = new String (baos.toByteArray()); 429 StringTokenizer msg = new StringTokenizer (message, "\n\r"); 430 431 while (msg.hasMoreTokens()) { 432 body.print( msg.nextToken() ); 433 body.println("<br>"); 434 } 435 } catch (Exception e){ 436 } 437 } 438 439 body.println("</body>"); 440 body.println("</html>"); 441 442 return res; 443 } 444 445 449 protected static HttpResponseImpl createForbidden(String ip){ 450 HttpResponseImpl res = new HttpResponseImpl(403, "Forbidden", "text/html"); 451 java.io.PrintWriter body = res.getPrintWriter(); 452 453 body.println("<html>"); 454 body.println("<body>"); 455 body.println("<h3>Forbidden</h3>"); 456 body.println("<br><br>"); 457 body.println("IP address: " + ip + " is not registered on this server, please contact your system administrator."); 460 body.println("</body>"); 461 body.println("</html>"); 462 463 return res; 464 } 465 466 470 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 471 472 out.writeObject( responseString ); 473 474 475 out.writeInt( code ); 476 477 478 out.writeObject( headers ); 479 480 481 writer.flush(); 482 body = baos.toByteArray(); 483 out.writeObject( body ); 485 } 486 487 492 private void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException { 493 494 this.responseString = (String )in.readObject(); 495 496 497 this.code = in.readInt(); 498 499 500 this.headers = (HashMap ) in.readObject(); 501 502 503 body = (byte[]) in.readObject(); 504 baos = new ByteArrayOutputStream (); 506 baos.write( body ); 507 writer = new PrintWriter ( baos ); 508 509 } 510 513 public void setContent(URLConnection content) { 514 this.content = content; 515 } 516 517 } | Popular Tags |