001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.coyote.tomcat3;
018:
019: import java.io.IOException;
020: import java.util.Locale;
021:
022: import org.apache.coyote.ActionCode;
023: import org.apache.tomcat.core.Response;
024: import org.apache.tomcat.util.buf.ByteChunk;
025:
026: /** The Response to connect with Coyote.
027: * This class mostly handles the I/O between Tomcat and Coyte.
028: * @Author Bill Barker
029: */
030:
031: class Tomcat3Response extends Response {
032: String reportedname = null;
033:
034: org.apache.coyote.Response coyoteResponse = null;
035:
036: ByteChunk outputChunk = new ByteChunk();
037:
038: boolean acknowledged = false;
039:
040: public Tomcat3Response() {
041: super ();
042: }
043:
044: /** Attach a Coyote Request to this request.
045: */
046: public void setCoyoteResponse(org.apache.coyote.Response cRes) {
047: coyoteResponse = cRes;
048: headers = coyoteResponse.getMimeHeaders();
049: }
050:
051: public void init() {
052: super .init();
053: }
054:
055: public void recycle() {
056: super .recycle();
057: if (coyoteResponse != null)
058: coyoteResponse.recycle();
059: outputChunk.recycle();
060: acknowledged = false;
061: }
062:
063: // XXX What is this ? */
064: public void setReported(String reported) {
065: reportedname = reported;
066: }
067:
068: public void endHeaders() throws IOException {
069: super .endHeaders();
070: coyoteResponse.setStatus(getStatus());
071: // Check that the content-length has been set.
072: int cLen = getContentLength();
073: if (cLen >= 0) {
074: coyoteResponse.setContentLength(cLen);
075: }
076: // Calls a sendHeaders callback to the protocol
077: coyoteResponse.sendHeaders();
078: }
079:
080: public void clientFlush() throws IOException {
081: coyoteResponse.action(ActionCode.ACTION_CLIENT_FLUSH,
082: coyoteResponse);
083: }
084:
085: public void doWrite(byte buffer[], int pos, int count)
086: throws IOException {
087: if (count > 0) {
088: // XXX should be an explicit callback as well.
089: outputChunk.setBytes(buffer, pos, count);
090: coyoteResponse.doWrite(outputChunk);
091: }
092: }
093:
094: public void reset() throws IllegalStateException {
095: super .reset();
096: if (!included)
097: coyoteResponse.reset();
098: }
099:
100: public void finish() throws IOException {
101: super .finish();
102: coyoteResponse.finish();
103: }
104:
105: /**
106: * Send an acknowledgment of a request.
107: *
108: * @exception IOException if an input/output error occurs
109: */
110: public void sendAcknowledgement() throws IOException {
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(sm
122: .getString("hsrf.error.ise"));
123:
124: coyoteResponse.acknowledge();
125: acknowledged = true;
126: }
127:
128: public void setLocale(Locale 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 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: }
|