KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat3 > Tomcat3Response


1 /*
2  * Copyright 1999-2004 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
17 package org.apache.coyote.tomcat3;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.coyote.ActionCode;
23 import org.apache.tomcat.core.Response;
24 import org.apache.tomcat.util.buf.ByteChunk;
25
26 /** The Response to connect with Coyote.
27  * This class mostly handles the I/O between Tomcat and Coyte.
28  * @Author Bill Barker
29  */

30
31 class Tomcat3Response extends Response {
32     String JavaDoc reportedname=null;
33
34     org.apache.coyote.Response coyoteResponse=null;
35
36     ByteChunk outputChunk = new ByteChunk();
37
38     boolean acknowledged=false;
39     
40     public Tomcat3Response() {
41         super();
42     }
43
44     /** Attach a Coyote Request to this request.
45      */

46     public void setCoyoteResponse(org.apache.coyote.Response cRes) {
47     coyoteResponse = cRes;
48     headers = coyoteResponse.getMimeHeaders();
49     }
50
51     public void init() {
52     super.init();
53     }
54
55     public void recycle() {
56     super.recycle();
57     if(coyoteResponse != null) coyoteResponse.recycle();
58     outputChunk.recycle();
59     acknowledged=false;
60     }
61
62     // XXX What is this ? */
63
public void setReported(String JavaDoc reported) {
64         reportedname = reported;
65     }
66
67     public void endHeaders() throws IOException JavaDoc {
68     super.endHeaders();
69     coyoteResponse.setStatus(getStatus());
70     // Check that the content-length has been set.
71
int cLen = getContentLength();
72     if( cLen >= 0 ) {
73         coyoteResponse.setContentLength(cLen);
74     }
75         // Calls a sendHeaders callback to the protocol
76
coyoteResponse.sendHeaders();
77     }
78
79     public void clientFlush() throws IOException JavaDoc {
80         coyoteResponse.action( ActionCode.ACTION_CLIENT_FLUSH, coyoteResponse );
81     }
82     
83     public void doWrite( byte buffer[], int pos, int count)
84     throws IOException JavaDoc
85     {
86     if( count > 0 ) {
87             // XXX should be an explicit callback as well.
88
outputChunk.setBytes(buffer, pos, count);
89         coyoteResponse.doWrite( outputChunk );
90     }
91     }
92
93     public void reset() throws IllegalStateException JavaDoc {
94     super.reset();
95     if( ! included )
96         coyoteResponse.reset();
97     }
98     
99     public void finish() throws IOException JavaDoc {
100     super.finish();
101     coyoteResponse.finish();
102     }
103
104     /**
105      * Send an acknowledgment of a request.
106      *
107      * @exception IOException if an input/output error occurs
108      */

109     public void sendAcknowledgement()
110         throws IOException JavaDoc {
111
112     if( status >= 300 ) // Don't ACK on errors.
113
acknowledged = true;
114         // Don't ACK twice on the same request. (e.g. on a forward)
115
if(acknowledged)
116         return;
117         // Ignore any call from an included servlet
118
if (isIncluded())
119             return;
120         if (isBufferCommitted())
121             throw new IllegalStateException JavaDoc
122                 (sm.getString("hsrf.error.ise"));
123
124     coyoteResponse.acknowledge();
125     acknowledged=true;
126     }
127
128     public void setLocale(Locale JavaDoc locale) {
129         if (locale == null || included) {
130             return; // throw an exception?
131
}
132         this.locale = locale;
133         coyoteResponse.setLocale(locale);
134         contentLanguage = coyoteResponse.getContentLanguage();
135         // maintain Tomcat 3.3 behavior by setting the header too
136
// and by not trying to guess the characterEncoding
137
headers.setValue("Content-Language").setString(contentLanguage);
138     }
139
140     public void setContentType(String JavaDoc contentType) {
141         if (included) {
142             return;
143         }
144         coyoteResponse.setContentType(contentType);
145         this.contentType = coyoteResponse.getContentType();
146         this.characterEncoding = coyoteResponse.getCharacterEncoding();
147         this.haveCharacterEncoding = true;
148         // maintain Tomcat 3.3 behavior by setting the header too
149
headers.setValue("Content-Type").setString(contentType);
150     }
151
152 }
153
Popular Tags