001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.coyote.http11.filters;
019:
020: import java.io.IOException;
021:
022: import org.apache.tomcat.util.buf.ByteChunk;
023:
024: import org.apache.coyote.OutputBuffer;
025: import org.apache.coyote.Response;
026: import org.apache.coyote.http11.OutputFilter;
027:
028: /**
029: * Identity output filter.
030: *
031: * @author Remy Maucherat
032: */
033: public class IdentityOutputFilter implements OutputFilter {
034:
035: // -------------------------------------------------------------- Constants
036:
037: protected static final String ENCODING_NAME = "identity";
038: protected static final ByteChunk ENCODING = new ByteChunk();
039:
040: // ----------------------------------------------------- Static Initializer
041:
042: static {
043: ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME
044: .length());
045: }
046:
047: // ----------------------------------------------------- Instance Variables
048:
049: /**
050: * Content length.
051: */
052: protected long contentLength = -1;
053:
054: /**
055: * Remaining bytes.
056: */
057: protected long remaining = 0;
058:
059: /**
060: * Next buffer in the pipeline.
061: */
062: protected OutputBuffer buffer;
063:
064: // ------------------------------------------------------------- Properties
065:
066: /**
067: * Get content length.
068: */
069: public long getContentLength() {
070: return contentLength;
071: }
072:
073: /**
074: * Get remaining bytes.
075: */
076: public long getRemaining() {
077: return remaining;
078: }
079:
080: // --------------------------------------------------- OutputBuffer Methods
081:
082: /**
083: * Write some bytes.
084: *
085: * @return number of bytes written by the filter
086: */
087: public int doWrite(ByteChunk chunk, Response res)
088: throws IOException {
089:
090: int result = -1;
091:
092: if (contentLength >= 0) {
093: if (remaining > 0) {
094: result = chunk.getLength();
095: if (result > remaining) {
096: // The chunk is longer than the number of bytes remaining
097: // in the body; changing the chunk length to the number
098: // of bytes remaining
099: chunk.setBytes(chunk.getBytes(), chunk.getStart(),
100: (int) remaining);
101: result = (int) remaining;
102: remaining = 0;
103: } else {
104: remaining = remaining - result;
105: }
106: buffer.doWrite(chunk, res);
107: } else {
108: // No more bytes left to be written : return -1 and clear the
109: // buffer
110: chunk.recycle();
111: result = -1;
112: }
113: } else {
114: // If no content length was set, just write the bytes
115: buffer.doWrite(chunk, res);
116: result = chunk.getLength();
117: }
118:
119: return result;
120:
121: }
122:
123: // --------------------------------------------------- OutputFilter Methods
124:
125: /**
126: * Some filters need additional parameters from the response. All the
127: * necessary reading can occur in that method, as this method is called
128: * after the response header processing is complete.
129: */
130: public void setResponse(Response response) {
131: contentLength = response.getContentLengthLong();
132: remaining = contentLength;
133: }
134:
135: /**
136: * Set the next buffer in the filter pipeline.
137: */
138: public void setBuffer(OutputBuffer buffer) {
139: this .buffer = buffer;
140: }
141:
142: /**
143: * End the current request. It is acceptable to write extra bytes using
144: * buffer.doWrite during the execution of this method.
145: */
146: public long end() throws IOException {
147:
148: if (remaining > 0)
149: return remaining;
150: return 0;
151:
152: }
153:
154: /**
155: * Make the filter ready to process the next request.
156: */
157: public void recycle() {
158: contentLength = -1;
159: remaining = 0;
160: }
161:
162: /**
163: * Return the name of the associated encoding; Here, the value is
164: * "identity".
165: */
166: public ByteChunk getEncodingName() {
167: return ENCODING;
168: }
169:
170: }
|