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   * @since 4.3
50   */
51  @Contract(threading = ThreadingBehavior.STATELESS)
52  public class PlainConnectionSocketFactory implements ConnectionSocketFactory {
53  
54      public static final PlainConnectionSocketFactory INSTANCE = new PlainConnectionSocketFactory();
55  
56      public static PlainConnectionSocketFactory getSocketFactory() {
57          return INSTANCE;
58      }
59  
60      public PlainConnectionSocketFactory() {
61          super();
62      }
63  
64      @Override
65      public Socket createSocket(final Proxy proxy, final HttpContext context) throws IOException {
66          return proxy != null ? new Socket(proxy) : createSocket(context);
67      }
68  
69      @Override
70      public Socket createSocket(final HttpContext context) throws IOException {
71          return new Socket();
72      }
73  
74      @Override
75      public Socket connectSocket(
76              final TimeValue connectTimeout,
77              final Socket socket,
78              final HttpHost host,
79              final InetSocketAddress remoteAddress,
80              final InetSocketAddress localAddress,
81              final HttpContext context) throws IOException {
82          final Socket sock = socket != null ? socket : createSocket(context);
83          if (localAddress != null) {
84              sock.bind(localAddress);
85          }
86          try {
87              // Run this under a doPrivileged to support lib users that run under a SecurityManager this allows granting connect permissions
88              // only to this library
89              try {
90                  AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
91                      sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisecondsIntBound() : 0);
92                      return null;
93                  });
94              } catch (final PrivilegedActionException e) {
95                  Asserts.check(e.getCause() instanceof  IOException,
96                          "method contract violation only checked exceptions are wrapped: " + e.getCause());
97                  // only checked exceptions are wrapped - error and RTExceptions are rethrown by doPrivileged
98                  throw (IOException) e.getCause();
99              }
100         } catch (final IOException ex) {
101             Closer.closeQuietly(sock);
102             throw ex;
103         }
104         return sock;
105     }
106 
107 }