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.hc.client5.http.socket;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.net.Proxy;
33  import java.net.Socket;
34  import java.security.AccessController;
35  import java.security.PrivilegedActionException;
36  import java.security.PrivilegedExceptionAction;
37  
38  import org.apache.hc.core5.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.protocol.HttpContext;
42  import org.apache.hc.core5.io.Closer;
43  import org.apache.hc.core5.util.Asserts;
44  import org.apache.hc.core5.util.TimeValue;
45  
46  /**
47   * The default class for creating plain (unencrypted) sockets.
48   *
49   * @deprecated Use {@link org.apache.hc.client5.http.io.DetachedSocketFactory}.
50   * Please note this interface is considered internal.
51   *
52   * @see org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator
53   * @see org.apache.hc.client5.http.io.DetachedSocketFactory
54   */
55  @Deprecated
56  @Contract(threading = ThreadingBehavior.STATELESS)
57  public class PlainConnectionSocketFactory implements ConnectionSocketFactory {
58  
59      /**
60       * Default instance of {@link PlainConnectionSocketFactory}.
61       */
62      public static final PlainConnectionSocketFactory INSTANCE = new PlainConnectionSocketFactory();
63  
64      public static PlainConnectionSocketFactory getSocketFactory() {
65          return INSTANCE;
66      }
67  
68      public PlainConnectionSocketFactory() {
69          super();
70      }
71  
72      @Override
73      public Socket createSocket(final Proxy proxy, final HttpContext context) throws IOException {
74          return proxy != null ? new Socket(proxy) : createSocket(context);
75      }
76  
77      @Override
78      public Socket createSocket(final HttpContext context) throws IOException {
79          return new Socket();
80      }
81  
82      @Override
83      public Socket connectSocket(
84              final TimeValue connectTimeout,
85              final Socket socket,
86              final HttpHost host,
87              final InetSocketAddress remoteAddress,
88              final InetSocketAddress localAddress,
89              final HttpContext context) throws IOException {
90          final Socket sock = socket != null ? socket : createSocket(context);
91          if (localAddress != null) {
92              sock.bind(localAddress);
93          }
94          try {
95              // Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
96              // only to this library
97              try {
98                  AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
99                      sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisecondsIntBound() : 0);
100                     return null;
101                 });
102             } catch (final PrivilegedActionException e) {
103                 Asserts.check(e.getCause() instanceof  IOException,
104                         "method contract violation only checked exceptions are wrapped: " + e.getCause());
105                 // only checked exceptions are wrapped - error and RTExceptions are rethrown by doPrivileged
106                 throw (IOException) e.getCause();
107             }
108         } catch (final IOException ex) {
109             Closer.closeQuietly(sock);
110             throw ex;
111         }
112         return sock;
113     }
114 
115 }