View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.conn;
29  
30  import java.io.IOException;
31  import java.io.InterruptedIOException;
32  import java.net.Socket;
33  import java.nio.charset.CharsetDecoder;
34  import java.nio.charset.CharsetEncoder;
35  import java.util.Map;
36  import java.util.concurrent.ConcurrentHashMap;
37  
38  import javax.net.ssl.SSLSession;
39  import javax.net.ssl.SSLSocket;
40  
41  import org.apache.http.HttpRequest;
42  import org.apache.http.HttpResponse;
43  import org.apache.http.annotation.NotThreadSafe;
44  import org.apache.http.config.MessageConstraints;
45  import org.apache.http.conn.ManagedHttpClientConnection;
46  import org.apache.http.entity.ContentLengthStrategy;
47  import org.apache.http.impl.DefaultBHttpClientConnection;
48  import org.apache.http.io.HttpMessageParserFactory;
49  import org.apache.http.io.HttpMessageWriterFactory;
50  import org.apache.http.protocol.HttpContext;
51  
52  /**
53   * Default {@link ManagedHttpClientConnection} implementation.
54   * @since 4.3
55   */
56  @NotThreadSafe
57  public class DefaultManagedHttpClientConnection extends DefaultBHttpClientConnection
58                                   implements ManagedHttpClientConnection, HttpContext {
59  
60      private final String id;
61      private final Map<String, Object> attributes;
62  
63      private volatile boolean shutdown;
64  
65      public DefaultManagedHttpClientConnection(
66              final String id,
67              final int buffersize,
68              final int fragmentSizeHint,
69              final CharsetDecoder chardecoder,
70              final CharsetEncoder charencoder,
71              final MessageConstraints constraints,
72              final ContentLengthStrategy incomingContentStrategy,
73              final ContentLengthStrategy outgoingContentStrategy,
74              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
75              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
76          super(buffersize, fragmentSizeHint, chardecoder, charencoder,
77                  constraints, incomingContentStrategy, outgoingContentStrategy,
78                  requestWriterFactory, responseParserFactory);
79          this.id = id;
80          this.attributes = new ConcurrentHashMap<String, Object>();
81      }
82  
83      public DefaultManagedHttpClientConnection(
84              final String id,
85              final int buffersize) {
86          this(id, buffersize, buffersize, null, null, null, null, null, null, null);
87      }
88  
89      @Override
90      public String getId() {
91          return this.id;
92      }
93  
94      @Override
95      public void shutdown() throws IOException {
96          this.shutdown = true;
97          super.shutdown();
98      }
99  
100     @Override
101     public Object getAttribute(final String id) {
102         return this.attributes.get(id);
103     }
104 
105     @Override
106     public Object removeAttribute(final String id) {
107         return this.attributes.remove(id);
108     }
109 
110     @Override
111     public void setAttribute(final String id, final Object obj) {
112         this.attributes.put(id, obj);
113     }
114 
115     @Override
116     public void bind(final Socket socket) throws IOException {
117         if (this.shutdown) {
118             socket.close(); // allow this to throw...
119             // ...but if it doesn't, explicitly throw one ourselves.
120             throw new InterruptedIOException("Connection already shutdown");
121         }
122         super.bind(socket);
123     }
124 
125     @Override
126     public Socket getSocket() {
127         return super.getSocket();
128     }
129 
130     @Override
131     public SSLSession getSSLSession() {
132         final Socket socket = super.getSocket();
133         if (socket instanceof SSLSocket) {
134             return ((SSLSocket) socket).getSession();
135         } else {
136             return null;
137         }
138     }
139 
140 }