KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > SimpleHTTPOutputStream


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Runtime state of the engine
17  */

18 package org.apache.axis2.transport.http;
19
20 import org.apache.axis2.engine.AxisFault;
21
22 import java.io.FilterOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 public class SimpleHTTPOutputStream extends FilterOutputStream JavaDoc {
27     private boolean written = false;
28     private boolean chuncked = false;
29     private String JavaDoc contentType = null;
30     
31     public SimpleHTTPOutputStream(
32         OutputStream JavaDoc out,boolean chuncked)
33         throws AxisFault {
34         super(out);
35         this.chuncked = chuncked;
36     }
37
38     public void write(byte[] b) throws IOException JavaDoc {
39         if(!written){
40             writeHeader();
41         }
42         out.write(b);
43     }
44
45     /**
46      * @param b
47      * @param off
48      * @param len
49      * @throws java.io.IOException
50      */

51     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
52         if(!written){
53             writeHeader();
54         }
55         out.write(b, off, len);
56     }
57
58     /**
59      * @param b
60      * @throws java.io.IOException
61      */

62     public void write(int b) throws IOException JavaDoc {
63         if(!written){
64             writeHeader();
65         }
66         out.write(b);
67     }
68
69     public void writeHeader() throws IOException JavaDoc{
70         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
71         if(chuncked){
72             buf.append(new String JavaDoc(HTTPConstants.HEADER_PROTOCOL_11)).append(" ");
73             buf.append(new String JavaDoc(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 JavaDoc(HTTPConstants.HTTP));
83             buf.append(new String JavaDoc(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 JavaDoc {
100         if (!written) {
101             out.write(new String JavaDoc(HTTPConstants.NOCONTENT).getBytes());
102             written = true;
103         } else{
104             out.flush();
105         }
106         if(chuncked){
107             //TODO sometimes the out stream is closed by the client
108
try {
109                 ((ChunkedOutputStream)out).eos();
110             } catch (IOException JavaDoc e) {
111             }
112         }
113     }
114     
115     /* (non-Javadoc)
116      * @see java.io.OutputStream#close()
117      */

118     public void close() throws IOException JavaDoc {
119         if(!written){
120             finalize();
121         }
122         super.close();
123     }
124
125     /**
126      * @param string
127      */

128     public void setContentType(String JavaDoc string) {
129         contentType = string;
130     }
131
132 }
133
Popular Tags