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      public String getId() {
90          return this.id;
91      }
92  
93      @Override
94      public void shutdown() throws IOException {
95          this.shutdown = true;
96          super.shutdown();
97      }
98  
99      public Object getAttribute(final String id) {
100         return this.attributes.get(id);
101     }
102 
103     public Object removeAttribute(final String id) {
104         return this.attributes.remove(id);
105     }
106 
107     public void setAttribute(final String id, final Object obj) {
108         this.attributes.put(id, obj);
109     }
110 
111     @Override
112     public void bind(final Socket socket) throws IOException {
113         if (this.shutdown) {
114             socket.close(); // allow this to throw...
115             // ...but if it doesn't, explicitly throw one ourselves.
116             throw new InterruptedIOException("Connection already shutdown");
117         }
118         super.bind(socket);
119     }
120 
121     @Override
122     public Socket getSocket() {
123         return super.getSocket();
124     }
125 
126     public SSLSession getSSLSession() {
127         final Socket socket = super.getSocket();
128         if (socket instanceof SSLSocket) {
129             return ((SSLSocket) socket).getSession();
130         } else {
131             return null;
132         }
133     }
134 
135 }