1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseConnControl.java $
3 * $Revision: 618017 $
4 * $Date: 2008-02-03 08:42:22 -0800 (Sun, 03 Feb 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.protocol;
33
34import java.io.IOException;
35
36import org.apache.http.Header;
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpException;
39import org.apache.http.HttpRequest;
40import org.apache.http.HttpResponse;
41import org.apache.http.HttpResponseInterceptor;
42import org.apache.http.HttpStatus;
43import org.apache.http.HttpVersion;
44import org.apache.http.ProtocolVersion;
45
46/**
47 * A response interceptor that suggests connection keep-alive to the client.
48 * For use on the server side.
49 *
50 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
51 *
52 * @version $Revision: 618017 $
53 *
54 * @since 4.0
55 *
56 * @deprecated Please use {@link java.net.URL#openConnection} instead.
57 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
58 *     for further details.
59 */
60@Deprecated
61public class ResponseConnControl implements HttpResponseInterceptor {
62
63    public ResponseConnControl() {
64        super();
65    }
66
67    public void process(final HttpResponse response, final HttpContext context)
68            throws HttpException, IOException {
69        if (response == null) {
70            throw new IllegalArgumentException("HTTP response may not be null");
71        }
72        if (context == null) {
73            throw new IllegalArgumentException("HTTP context may not be null");
74        }
75        // Always drop connection after certain type of responses
76        int status = response.getStatusLine().getStatusCode();
77        if (status == HttpStatus.SC_BAD_REQUEST ||
78                status == HttpStatus.SC_REQUEST_TIMEOUT ||
79                status == HttpStatus.SC_LENGTH_REQUIRED ||
80                status == HttpStatus.SC_REQUEST_TOO_LONG ||
81                status == HttpStatus.SC_REQUEST_URI_TOO_LONG ||
82                status == HttpStatus.SC_SERVICE_UNAVAILABLE ||
83                status == HttpStatus.SC_NOT_IMPLEMENTED) {
84            response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
85            return;
86        }
87        // Always drop connection for HTTP/1.0 responses and below
88        // if the content body cannot be correctly delimited
89        HttpEntity entity = response.getEntity();
90        if (entity != null) {
91            ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
92            if (entity.getContentLength() < 0 &&
93                    (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) {
94                response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
95                return;
96            }
97        }
98        // Drop connection if requested by the client
99        HttpRequest request = (HttpRequest)
100            context.getAttribute(ExecutionContext.HTTP_REQUEST);
101        if (request != null) {
102            Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
103            if (header != null) {
104                response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue());
105            }
106        }
107    }
108
109}
110