1 18 package org.apache.axis2.transport.http; 19 20 import org.apache.axis2.engine.AxisFault; 21 22 import java.io.FilterOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 public class SimpleHTTPOutputStream extends FilterOutputStream { 27 private boolean written = false; 28 private boolean chuncked = false; 29 private String contentType = null; 30 31 public SimpleHTTPOutputStream( 32 OutputStream out,boolean chuncked) 33 throws AxisFault { 34 super(out); 35 this.chuncked = chuncked; 36 } 37 38 public void write(byte[] b) throws IOException { 39 if(!written){ 40 writeHeader(); 41 } 42 out.write(b); 43 } 44 45 51 public void write(byte[] b, int off, int len) throws IOException { 52 if(!written){ 53 writeHeader(); 54 } 55 out.write(b, off, len); 56 } 57 58 62 public void write(int b) throws IOException { 63 if(!written){ 64 writeHeader(); 65 } 66 out.write(b); 67 } 68 69 public void writeHeader() throws IOException { 70 StringBuffer buf = new StringBuffer (); 71 if(chuncked){ 72 buf.append(new String (HTTPConstants.HEADER_PROTOCOL_11)).append(" "); 73 buf.append(new String (HTTPConstants.OK)).append("\n"); 74 buf.append(HTTPConstants.HEADER_TRANSFER_ENCODING).append(": "); 75 buf.append(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED).append("\n"); 76 if(contentType != null){ 77 buf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(": "); 78 buf.append(contentType).append("\n"); 79 } 80 buf.append("\n"); 81 }else{ 82 buf.append(new String (HTTPConstants.HTTP)); 83 buf.append(new String (HTTPConstants.OK)).append("\n"); 84 if(contentType != null){ 85 buf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(": "); 86 buf.append(contentType).append("\n"); 87 } 88 buf.append("\n"); 89 } 90 out.write(buf.toString().getBytes()); 91 written = true; 92 if(chuncked){ 93 out.flush(); 94 out = new ChunkedOutputStream(out); 95 } 96 97 } 98 99 public void finalize() throws IOException { 100 if (!written) { 101 out.write(new String (HTTPConstants.NOCONTENT).getBytes()); 102 written = true; 103 } else{ 104 out.flush(); 105 } 106 if(chuncked){ 107 try { 109 ((ChunkedOutputStream)out).eos(); 110 } catch (IOException e) { 111 } 112 } 113 } 114 115 118 public void close() throws IOException { 119 if(!written){ 120 finalize(); 121 } 122 super.close(); 123 } 124 125 128 public void setContentType(String string) { 129 contentType = string; 130 } 131 132 } 133 | Popular Tags |