001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java $
003: * $Revision: 573864 $
004: * $Date: 2007-09-08 17:53:25 +0200 (Sat, 08 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.HttpEntity;
037: import org.apache.http.HttpException;
038: import org.apache.http.HttpResponse;
039: import org.apache.http.HttpResponseInterceptor;
040: import org.apache.http.HttpStatus;
041: import org.apache.http.HttpVersion;
042: import org.apache.http.ProtocolVersion;
043: import org.apache.http.ProtocolException;
044:
045: /**
046: * A response interceptor that sets up entity-related headers.
047: * For use on the server side.
048: *
049: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
050: *
051: * @version $Revision: 573864 $
052: *
053: * @since 4.0
054: */
055: public class ResponseContent implements HttpResponseInterceptor {
056:
057: public ResponseContent() {
058: super ();
059: }
060:
061: public void process(final HttpResponse response,
062: final HttpContext context) throws HttpException,
063: IOException {
064: if (response == null) {
065: throw new IllegalArgumentException(
066: "HTTP request may not be null");
067: }
068: if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
069: throw new ProtocolException(
070: "Transfer-encoding header already present");
071: }
072: if (response.containsHeader(HTTP.CONTENT_LEN)) {
073: throw new ProtocolException(
074: "Content-Length header already present");
075: }
076: ProtocolVersion ver = response.getStatusLine()
077: .getProtocolVersion();
078: HttpEntity entity = response.getEntity();
079: if (entity != null) {
080: long len = entity.getContentLength();
081: if (entity.isChunked()
082: && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
083: response.addHeader(HTTP.TRANSFER_ENCODING,
084: HTTP.CHUNK_CODING);
085: } else if (len >= 0) {
086: response.addHeader(HTTP.CONTENT_LEN, Long
087: .toString(entity.getContentLength()));
088: }
089: // Specify a content type if known
090: if (entity.getContentType() != null
091: && !response.containsHeader(HTTP.CONTENT_TYPE)) {
092: response.addHeader(entity.getContentType());
093: }
094: // Specify a content encoding if known
095: if (entity.getContentEncoding() != null
096: && !response.containsHeader(HTTP.CONTENT_ENCODING)) {
097: response.addHeader(entity.getContentEncoding());
098: }
099: } else {
100: int status = response.getStatusLine().getStatusCode();
101: if (status != HttpStatus.SC_NO_CONTENT
102: && status != HttpStatus.SC_NOT_MODIFIED
103: && status != HttpStatus.SC_RESET_CONTENT) {
104: response.addHeader(HTTP.CONTENT_LEN, "0");
105: }
106: }
107: }
108:
109: }
|