001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/RequestContent.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.HttpEntityEnclosingRequest;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpRequest;
040: import org.apache.http.HttpRequestInterceptor;
041: import org.apache.http.HttpVersion;
042: import org.apache.http.ProtocolVersion;
043: import org.apache.http.ProtocolException;
044:
045: /**
046: * A request interceptor that decides about the transport encoding.
047: *
048: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
049: *
050: * @version $Revision: 573864 $
051: *
052: * @since 4.0
053: */
054: public class RequestContent implements HttpRequestInterceptor {
055:
056: public RequestContent() {
057: super ();
058: }
059:
060: public void process(final HttpRequest request,
061: final HttpContext context) throws HttpException,
062: IOException {
063: if (request == null) {
064: throw new IllegalArgumentException(
065: "HTTP request may not be null");
066: }
067: if (request instanceof HttpEntityEnclosingRequest) {
068: if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
069: throw new ProtocolException(
070: "Transfer-encoding header already present");
071: }
072: if (request.containsHeader(HTTP.CONTENT_LEN)) {
073: throw new ProtocolException(
074: "Content-Length header already present");
075: }
076: ProtocolVersion ver = request.getRequestLine()
077: .getProtocolVersion();
078: HttpEntity entity = ((HttpEntityEnclosingRequest) request)
079: .getEntity();
080: if (entity == null) {
081: request.addHeader(HTTP.CONTENT_LEN, "0");
082: return;
083: }
084: // Must specify a transfer encoding or a content length
085: if (entity.isChunked() || entity.getContentLength() < 0) {
086: if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
087: throw new ProtocolException(
088: "Chunked transfer encoding not allowed for "
089: + ver);
090: }
091: request.addHeader(HTTP.TRANSFER_ENCODING,
092: HTTP.CHUNK_CODING);
093: } else {
094: request.addHeader(HTTP.CONTENT_LEN, Long
095: .toString(entity.getContentLength()));
096: }
097: // Specify a content type if known
098: if (entity.getContentType() != null
099: && !request.containsHeader(HTTP.CONTENT_TYPE)) {
100: request.addHeader(entity.getContentType());
101: }
102: // Specify a content encoding if known
103: if (entity.getContentEncoding() != null
104: && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
105: request.addHeader(entity.getContentEncoding());
106: }
107: }
108: }
109:
110: }
|