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