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.config.MessageConstraints;
44  import org.apache.http.conn.ManagedHttpClientConnection;
45  import org.apache.http.entity.ContentLengthStrategy;
46  import org.apache.http.impl.DefaultBHttpClientConnection;
47  import org.apache.http.io.HttpMessageParserFactory;
48  import org.apache.http.io.HttpMessageWriterFactory;
49  import org.apache.http.protocol.HttpContext;
50  
51  /**
52   * Default {@link ManagedHttpClientConnection} implementation.
53   * @since 4.3
54   */
55  public class DefaultManagedHttpClientConnection extends DefaultBHttpClientConnection
56                                   implements ManagedHttpClientConnection, HttpContext {
57  
58      private final String id;
59      private final Map<String, Object> attributes;
60  
61      private volatile boolean shutdown;
62  
63      public DefaultManagedHttpClientConnection(
64              final String id,
65              final int buffersize,
66              final int fragmentSizeHint,
67              final CharsetDecoder chardecoder,
68              final CharsetEncoder charencoder,
69              final MessageConstraints constraints,
70              final ContentLengthStrategy incomingContentStrategy,
71              final ContentLengthStrategy outgoingContentStrategy,
72              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
73              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
74          super(buffersize, fragmentSizeHint, chardecoder, charencoder,
75                  constraints, incomingContentStrategy, outgoingContentStrategy,
76                  requestWriterFactory, responseParserFactory);
77          this.id = id;
78          this.attributes = new ConcurrentHashMap<String, Object>();
79      }
80  
81      public DefaultManagedHttpClientConnection(
82              final String id,
83              final int buffersize) {
84          this(id, buffersize, buffersize, null, null, null, null, null, null, null);
85      }
86  
87      @Override
88      public String getId() {
89          return this.id;
90      }
91  
92      @Override
93      public void shutdown() throws IOException {
94          this.shutdown = true;
95          super.shutdown();
96      }
97  
98      @Override
99      public Object getAttribute(final String id) {
100         return this.attributes.get(id);
101     }
102 
103     @Override
104     public Object removeAttribute(final String id) {
105         return this.attributes.remove(id);
106     }
107 
108     @Override
109     public void setAttribute(final String id, final Object obj) {
110         this.attributes.put(id, obj);
111     }
112 
113     @Override
114     public void bind(final Socket socket) throws IOException {
115         if (this.shutdown) {
116             socket.close(); // allow this to throw...
117             // ...but if it doesn't, explicitly throw one ourselves.
118             throw new InterruptedIOException("Connection already shutdown");
119         }
120         super.bind(socket);
121     }
122 
123     @Override
124     public Socket getSocket() {
125         return super.getSocket();
126     }
127 
128     @Override
129     public SSLSession getSSLSession() {
130         final Socket socket = super.getSocket();
131         if (socket instanceof SSLSocket) {
132             return ((SSLSocket) socket).getSession();
133         } else {
134             return null;
135         }
136     }
137 
138 }