KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > HttpOutputStream


1 /*
2
3  * Copyright (C) The MX4J Contributors.
4
5  * All rights reserved.
6
7  *
8
9  * This software is distributed under the terms of the MX4J License version 1.0.
10
11  * See the terms of the MX4J License in the documentation provided with this software.
12
13  */

14
15 package mx4j.tools.adaptor.http;
16
17
18 import java.io.BufferedOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26
27 /**
28  * HttpAdaptor sets the basic adaptor listening for HTTP requests
29  *
30  * @version $Revision: 1.3 $
31  */

32
33 public class HttpOutputStream extends BufferedOutputStream JavaDoc
34
35 {
36
37    /**
38     * Answer code
39     */

40
41    protected int code;
42
43
44    /**
45     * whether to send the headers
46     */

47
48    protected boolean sendHeaders;
49
50
51    /**
52     * Headers to be sent
53     */

54
55    protected Map JavaDoc headers = new HashMap JavaDoc(7);
56
57
58    /**
59     * Creates a new HttpOutputStream with a given OutputStream and an InputStream
60     *
61     * @param out The OutputStream normally associated with the output socket
62     * <p/>
63     * stream of the incoming connection
64     * @param in HttpInputStream containing the incoming request
65     */

66
67    public HttpOutputStream(OutputStream JavaDoc out, HttpInputStream in)
68
69    {
70
71       super(out);
72
73       code = HttpConstants.STATUS_OKAY;
74
75       setHeader("Server", HttpConstants.SERVER_INFO);
76
77       sendHeaders = (in.getVersion() >= 1.0);
78
79    }
80
81
82    /**
83     * Sets the answer code
84     *
85     * @param code The new code value
86     */

87
88    public void setCode(int code)
89
90    {
91
92       this.code = code;
93
94    }
95
96
97    /**
98     * Sets a given header code
99     *
100     * @param attr The new header name
101     * @param value The new header value
102     */

103
104    public void setHeader(String JavaDoc attr, String JavaDoc value)
105
106    {
107
108       headers.put(attr, value);
109
110    }
111
112
113    /**
114     * Sends the headers
115     *
116     * @return Description of the Returned Value
117     * @throws IOException Description of Exception
118     */

119
120    public boolean sendHeaders() throws IOException JavaDoc
121
122    {
123
124       if (sendHeaders)
125
126       {
127
128          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(512);
129
130          buffer.append(HttpConstants.HTTP_VERSION);
131
132          buffer.append(code);
133
134          buffer.append(" ");
135
136          buffer.append(HttpUtil.getCodeMessage(code));
137
138          buffer.append("\r\n");
139
140          Iterator JavaDoc attrs = headers.keySet().iterator();
141
142          int size = headers.size();
143
144
145          for (int i = 0; i < size; i++)
146
147          {
148
149             String JavaDoc attr = (String JavaDoc)attrs.next();
150
151             buffer.append(attr);
152
153             buffer.append(": ");
154
155             buffer.append(headers.get(attr));
156
157             buffer.append("\r\n");
158
159          }
160
161          buffer.append("\r\n");
162
163          write(buffer.toString());
164
165       }
166
167       return sendHeaders;
168
169    }
170
171
172    /**
173     * Writes a given message line
174     *
175     * @param msg The message to be written
176     * @throws IOException
177     */

178
179    public void write(String JavaDoc msg) throws IOException JavaDoc
180
181    {
182
183       write(msg.getBytes("latin1"));
184
185    }
186
187
188    /**
189     * Writes the content of the input stream to the output stream
190     *
191     * @param in The input stream
192     * @throws IOException
193     */

194
195    public void write(InputStream JavaDoc in) throws IOException JavaDoc
196
197    {
198
199       int n;
200
201       int length = buf.length;
202
203       while ((n = in.read(buf, count, length - count)) >= 0)
204
205       {
206
207          if ((count += n) >= length)
208
209          {
210
211             out.write(buf, count = 0, length);
212
213          }
214
215       }
216
217    }
218
219 }
220
221
Popular Tags