001 package org.shiftone.jrat.http; 002 003 import org.shiftone.jrat.util.log.Logger; 004 005 import java.io.IOException; 006 import java.io.OutputStream; 007 import java.io.OutputStreamWriter; 008 import java.io.Writer; 009 010 /** 011 * @author jeff@shiftone.org (Jeff Drost) 012 */ 013 public class Response { 014 015 private static final Logger LOG = Logger.getLogger(Response.class); 016 private final OutputStream outputStream; 017 private final Writer writer; 018 private final ResponseWriter responseWriter = new ResponseWriter(); 019 private final ResponseOutputStream responseOutputStream = new ResponseOutputStream(); 020 private boolean committed = false; 021 private Status status = Status.OK; 022 private String contentType; 023 024 025 public Response(OutputStream outputStream) { 026 this.outputStream = outputStream; 027 this.writer = new OutputStreamWriter(outputStream); 028 029 } 030 031 public Writer getWriter() { 032 return responseWriter; 033 } 034 035 036 public OutputStream getOutputStream() { 037 return responseOutputStream; 038 } 039 040 public void setStatus(Status status) { 041 this.status = status; 042 } 043 044 public void setContentType(String contentType) { 045 this.contentType = contentType; 046 } 047 048 public void flush() throws IOException { 049 commit(); 050 writer.flush(); 051 } 052 053 private void writeHeaders() throws IOException { 054 055 String statusLine = "HTTP/1.1 " + status.getCode() + " " + status.getMessage(); 056 LOG.info(statusLine); 057 058 writer.write(statusLine + "\n"); 059 writer.write("Content-Type: " + contentType + "\n"); 060 writer.write("Cache-Control: no-store, no-cache, must-revalidate\n"); // normal 061 writer.write("Cache-Control: post-check=0, pre-check=0"); // IE 062 writer.write("Pragma: no-cache\n"); // good luck 063 writer.write("Expires: Sat, 6 May 1995 12:00:00 GMT\n"); // more lock 064 065 writer.write("\n"); 066 writer.flush(); 067 068 } 069 070 private void commit() throws IOException { 071 072 if (!committed) { 073 writeHeaders(); 074 committed = true; 075 } 076 } 077 078 private class ResponseOutputStream extends OutputStream { 079 080 public void write(int b) throws IOException { 081 commit(); 082 outputStream.write(b); 083 } 084 085 public void write(byte b[]) throws IOException { 086 commit(); 087 outputStream.write(b); 088 } 089 090 public void write(byte b[], int off, int len) throws IOException { 091 commit(); 092 outputStream.write(b, off, len); 093 } 094 095 public void flush() throws IOException { 096 commit(); 097 outputStream.flush(); 098 } 099 100 public void close() throws IOException { 101 commit(); 102 outputStream.close(); 103 } 104 } 105 106 private class ResponseWriter extends Writer { 107 108 109 public void write(char cbuf[], int off, int len) throws IOException { 110 commit(); 111 writer.write(cbuf, off, len); 112 } 113 114 public void flush() throws IOException { 115 commit(); 116 writer.flush(); 117 } 118 119 public void close() throws IOException { 120 commit(); 121 writer.close(); 122 } 123 124 public void write(int c) throws IOException { 125 commit(); 126 super.write(c); 127 } 128 129 public void write(char cbuf[]) throws IOException { 130 commit(); 131 super.write(cbuf); 132 } 133 134 public void write(String str) throws IOException { 135 commit(); 136 super.write(str); 137 } 138 139 public void write(String str, int off, int len) throws IOException { 140 commit(); 141 super.write(str, off, len); 142 } 143 } 144 145 }