org.apache.commons.httpclient.params.HttpConnectionParams Java Examples

The following examples show how to use org.apache.commons.httpclient.params.HttpConnectionParams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EasySSLProtocolSocketFactory.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
    * Attempts to get a new socket connection to the given host within the given time limit.
    * <p/>
    * To circumvent the limitations of older JREs that do not support connect timeout a
    * controller thread is executed. The controller thread attempts to create a new socket
    * within the given limit of time. If socket constructor does not return until the
    * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
    * </p>
    *
    * @param host   the host name/IP
    * @param port   the port on the host
    * @param params {@link HttpConnectionParams Http connection parameters}
    * @return Socket a new socket
    * @throws IOException          if an I/O error occurs while creating the socket
    * @throws UnknownHostException if the IP address of the host cannot be
    *                              determined
    */
   @Override
public Socket createSocket(
           final String host,
           final int port,
           final InetAddress localAddress,
           final int localPort,
           final HttpConnectionParams params
   ) throws IOException {
       if (params == null) {
           throw new IllegalArgumentException("Parameters may not be null");
       }
       int timeout = params.getConnectionTimeout();
       if (timeout == 0) {
           return createSocket(host, port, localAddress, localPort);
       }
       else {
           // To be eventually deprecated when migrated to Java 1.4 or above
           return ControllerThreadSocketFactory.createSocket(
                   this, host, port, localAddress, localPort, timeout);
       }
   }
 
Example #2
Source File: EasySSLProtocolSocketFactory.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 *
 * <p>To circumvent the limitations of older JREs that do not support connect timeout a
 * controller thread is executed. The controller thread attempts to create a new socket within
 * the given limit of time. If socket constructor does not return until the timeout expires, the
 * controller terminates and throws an {@link ConnectTimeoutException}
 *
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * @return Socket a new socket
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be determined
 */
public Socket createSocket(
        final String host,
        final int port,
        final InetAddress localAddress,
        final int localPort,
        final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #3
Source File: EasySSLProtocolSocketFactoryUnitTest.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateSocketWithGivenLocalAddressAndPort() throws Exception {
    // Given
    InetAddress localAddress = InetAddress.getLoopbackAddress();
    int localPort = 28080;
    // When
    Socket sslSocket =
            socketFactory.createSocket(
                    "localhost",
                    serverPort,
                    localAddress,
                    localPort,
                    new HttpConnectionParams());
    // Then
    assertThat(sslSocket.getLocalAddress(), is(equalTo(localAddress)));
    assertThat(sslSocket.getLocalPort(), is(equalTo(localPort)));
}
 
Example #4
Source File: EasySSLProtocolSocketFactoryUnitTest.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled(value = "Requires a way to slow down connect process artificially")
public void shouldFailCreatingSocketWithInstantTimeout() throws Exception {
    // Given
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(1);
    // When / Then
    assertThrows(
            SocketTimeoutException.class,
            () ->
                    socketFactory.createSocket(
                            "localhost",
                            serverPort,
                            InetAddress.getLoopbackAddress(),
                            38080,
                            params));
}
 
Example #5
Source File: EasySSLProtocolSocketFactory.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * To circumvent the limitations of older JREs that do not support connect timeout a 
 * controller thread is executed. The controller thread attempts to create a new socket 
 * within the given limit of time. If socket constructor does not return until the 
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params
) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #6
Source File: EasySSLProtocolSocketFactory.java    From http4e with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * To circumvent the limitations of older JREs that do not support connect timeout a 
 * controller thread is executed. The controller thread attempts to create a new socket 
 * within the given limit of time. If socket constructor does not return until the 
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params
) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #7
Source File: SocketFactoryWrapper.java    From http4e with Apache License 2.0 6 votes vote down vote up
public Socket createSocket(
        String host, 
        int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException,
        ConnectTimeoutException {
    // Based on code from EasySSLProtocolSocketFactory.java
    Socket rval;
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        rval = socketFactory.createSocket(host, port, localAddress, localPort);
    } else {
        rval = socketFactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        rval.bind(localaddr);
        rval.connect(remoteaddr, timeout);
    }
    return rval;
}
 
Example #8
Source File: AuthSSLProtocolSocketFactory.java    From http4e with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * To circumvent the limitations of older JREs that do not support connect timeout a 
 * controller thread is executed. The controller thread attempts to create a new socket 
 * within the given limit of time. If socket constructor does not return until the 
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params
) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #9
Source File: HttpClientFactory.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
 * 
 * @param user
 *            can be NULL
 * @param password
 *            can be NULL
 * @return HttpClient
 */
public static HttpClient getHttpClientInstance(String user, String password) {
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionParams params = connectionManager.getParams();
    // wait max 10 seconds to establish connection
    params.setConnectionTimeout(10000);
    // a read() call on the InputStream associated with this Socket
    // will block for only this amount
    params.setSoTimeout(10000);
    HttpClient c = new HttpClient(connectionManager);

    // use basic authentication if available
    if (user != null && user.length() > 0) {
        AuthScope authScope = new AuthScope(null, -1, null);
        Credentials credentials = new UsernamePasswordCredentials(user, password);
        c.getState().setCredentials(authScope, credentials);
    }
    return c;
}
 
Example #10
Source File: HttpClientFactory.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * A HttpClient with basic authentication and no host or port setting. Can only be used to retrieve absolute URLs
 * 
 * @param user
 *            can be NULL
 * @param password
 *            can be NULL
 * @return HttpClient
 */
public static HttpClient getHttpClientInstance(String user, String password) {
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionParams params = connectionManager.getParams();
    // wait max 10 seconds to establish connection
    params.setConnectionTimeout(10000);
    // a read() call on the InputStream associated with this Socket
    // will block for only this amount
    params.setSoTimeout(10000);
    HttpClient c = new HttpClient(connectionManager);

    // use basic authentication if available
    if (user != null && user.length() > 0) {
        AuthScope authScope = new AuthScope(null, -1, null);
        Credentials credentials = new UsernamePasswordCredentials(user, password);
        c.getState().setCredentials(authScope, credentials);
    }
    return c;
}
 
Example #11
Source File: EasySSLProtocolSocketFactory.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * To circumvent the limitations of older JREs that do not support connect timeout a
 * controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *
 * @param host         the host name/IP
 * @param port         the port on the host
 * @param localAddress the local host name/IP to bind the socket to
 * @param localPort    the port on the local machine
 * @param params       {@link HttpConnectionParams Http connection parameters}
 * @return Socket a new socket
 * @throws IOException          if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 *                              determined
 */
public Socket createSocket(final String host,
                           final int port,
                           final InetAddress localAddress,
                           final int localPort,
                           final HttpConnectionParams params) throws IOException {
    if (params == null)
    {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    if (timeout == 0)
    {
        return createSocket(host, port, localAddress, localPort);
    }
    else
    {
        // To be eventually deprecated when migrated to Java 1.4 or above
        return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
    }
}
 
Example #12
Source File: CustomSSLProtocolSocketFactory.java    From developer-studio with Apache License 2.0 6 votes vote down vote up
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
                           final HttpConnectionParams params) throws IOException {
	if (params == null) {
		throw new IllegalArgumentException("Parameters may not be null");
	}
	int timeout = params.getConnectionTimeout();
	SocketFactory socketfactory = ctx.getSocketFactory();
	if (timeout == 0) {
		return socketfactory.createSocket(host, port, localAddress, localPort);
	} else {
		Socket socket = socketfactory.createSocket();
		SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
		SocketAddress remoteaddr = new InetSocketAddress(host, port);
		socket.bind(localaddr);
		socket.connect(remoteaddr, timeout);
		return socket;
	}
}
 
Example #13
Source File: EasySSLProtocolSocketFactory.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * To circumvent the limitations of older JREs that do not support connect timeout a 
 * controller thread is executed. The controller thread attempts to create a new socket 
 * within the given limit of time. If socket constructor does not return until the 
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params
) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #14
Source File: HttpClientUtil.java    From javabase with Apache License 2.0 6 votes vote down vote up
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
	HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
	if (params == null) {
		throw new IllegalArgumentException("Parameters may not be null");
	}
	int timeout = params.getConnectionTimeout();
	SocketFactory socketfactory = getSSLContext().getSocketFactory();
	if (timeout == 0) {
		return socketfactory.createSocket(host, port, localAddress, localPort);
	} else {
		Socket socket = socketfactory.createSocket();
		SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
		SocketAddress remoteaddr = new InetSocketAddress(host, port);
		socket.bind(localaddr);
		socket.connect(remoteaddr, timeout);
		return socket;
	}
}
 
Example #15
Source File: HttpClientFactory.java    From alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
 
Example #16
Source File: SocksSocketFactory.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {

    InetSocketAddress socksAddr = new InetSocketAddress(socksHost, socksPort);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
    int timeout = params.getConnectionTimeout();

    Socket socket = new Socket(proxy);
    socket.setSoTimeout(timeout);

    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    return socket;
}
 
Example #17
Source File: SSLSocketFactory.java    From elasticsearch-hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    }
    else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
 
Example #18
Source File: EasySSLProtocolSocketFactory.java    From freeacs with MIT License 6 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 *
 * <p>To circumvent the limitations of older JREs that do not support connect timeout a controller
 * thread is executed. The controller thread attempts to create a new socket within the given
 * limit of time. If socket constructor does not return until the timeout expires, the controller
 * terminates and throws an {@link ConnectTimeoutException}
 *
 * @param host the host name/IP
 * @param port the port on the host
 * @param localAddress the local address
 * @param localPort the local port
 * @param params {@link HttpConnectionParams Http connection parameters}
 * @return Socket a new socket
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be determined
 * @throws ConnectTimeoutException the connect timeout exception
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params)
    throws IOException, UnknownHostException, ConnectTimeoutException {
  if (params == null) {
    throw new IllegalArgumentException("Parameters may not be null");
  }
  int timeout = params.getConnectionTimeout();
  SocketFactory socketfactory = getSSLContext().getSocketFactory();
  if (timeout == 0) {
    return socketfactory.createSocket(host, port, localAddress, localPort);
  } else {
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    return socket;
  }
}
 
Example #19
Source File: TrustingProtocolSocketFactory.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public Socket createSocket(String host, int port, InetAddress localAddress,
        int localPort, HttpConnectionParams params) throws IOException,
        UnknownHostException, ConnectTimeoutException {
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        return createSocket(host, port, localAddress, localPort);
    }
    else {
        Socket s = ssf.createSocket();
        s.bind(new InetSocketAddress(localAddress, localPort));
        s.connect(new InetSocketAddress(host, port), timeout);
        return s;
    }
}
 
Example #20
Source File: EasySSLProtocolSocketFactoryUnitTest.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSucceedCreatingSocketWithReasonableTimeout() throws Exception {
    // Given
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(1000);
    // When
    Socket sslSocket =
            socketFactory.createSocket(
                    "localhost", serverPort, InetAddress.getLoopbackAddress(), 48080, params);
    // Then
    assertThat(sslSocket, is(notNullValue()));
}
 
Example #21
Source File: EasySSLProtocolSocketFactoryUnitTest.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailCreatingSocketForUnknownHost() throws Exception {
    // Given
    String unknownHost = "localhorst";
    InetAddress localAddress = InetAddress.getLoopbackAddress();
    int localPort = 28080;
    HttpConnectionParams params = new HttpConnectionParams();
    params.setConnectionTimeout(60000);
    // When / Then
    assertThrows(
            IOException.class,
            () ->
                    socketFactory.createSocket(
                            unknownHost, serverPort, localAddress, localPort, params));
}
 
Example #22
Source File: EasySSLProtocolSocketFactoryUnitTest.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailCreatingSocketForMissingParameters() throws Exception {
    // Given
    HttpConnectionParams nullParams = null;
    // When / Then
    assertThrows(
            IllegalArgumentException.class,
            () ->
                    socketFactory.createSocket(
                            "localhost",
                            serverPort,
                            InetAddress.getLoopbackAddress(),
                            12345,
                            nullParams));
}
 
Example #23
Source File: HttpMethodDirector.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Applies connection parameters specified for a given method
 * 
 * @param method HTTP method
 * 
 * @throws IOException if an I/O occurs setting connection parameters 
 */
private void applyConnectionParams(final HttpMethod method) throws IOException {
    int timeout = 0;
    // see if a timeout is given for this method
    Object param = method.getParams().getParameter(HttpMethodParams.SO_TIMEOUT);
    if (param == null) {
        // if not, use the default value
        param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT);
    }
    if (param != null) {
        timeout = ((Integer)param).intValue();
    }
    this.conn.setSocketTimeout(timeout);                    
}
 
Example #24
Source File: SAMLProcessorConfigurer.java    From spring-boot-security-saml with MIT License 5 votes vote down vote up
@VisibleForTesting
protected HTTPArtifactBinding createDefaultArtifactBinding(ServiceProviderBuilder builder) {
    HttpClientParams params = new HttpClientParams();
    params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 60000);
    HttpClient httpClient = new HttpClient(params, new MultiThreadedHttpConnectionManager());
    ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(httpClient);
    builder.setSharedObject(ArtifactResolutionProfile.class, artifactResolutionProfile);
    HTTPSOAP11Binding soapBinding = new HTTPSOAP11Binding(parserPool);
    artifactResolutionProfile.setProcessor(new SAMLProcessorImpl(soapBinding));
    return new HTTPArtifactBinding(parserPool, getVelocityEngine(), artifactResolutionProfile);
}
 
Example #25
Source File: MultiThreadedHttpConnectionManager.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setParams(final HttpConnectionParams params) {
    if (hasConnection()) {
        wrappedConnection.setParams(params);
    } else {
        throw new IllegalStateException("Connection has been released");
    }
}
 
Example #26
Source File: MultiThreadedHttpConnectionManager.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public HttpConnectionParams getParams() {
    if (hasConnection()) {
        return wrappedConnection.getParams();
    } else {
        throw new IllegalStateException("Connection has been released");
    }
}
 
Example #27
Source File: SocketFactoryDinamico.java    From Java_Certificado with MIT License 5 votes vote down vote up
@Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException {
    final Socket socket = this.ssl.getSocketFactory().createSocket();
    socket.bind(new InetSocketAddress(localAddress, localPort));
    socket.connect(new InetSocketAddress(host, port), 60000);
    return socket;
}
 
Example #28
Source File: HttpMethodDirector.java    From http4e with Apache License 2.0 5 votes vote down vote up
/**
 * Applies connection parameters specified for a given method
 * 
 * @param method HTTP method
 * 
 * @throws IOException if an I/O occurs setting connection parameters 
 */
private void applyConnectionParams(final HttpMethod method) throws IOException {
    int timeout = 0;
    // see if a timeout is given for this method
    Object param = method.getParams().getParameter(HttpMethodParams.SO_TIMEOUT);
    if (param == null) {
        // if not, use the default value
        param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT);
    }
    if (param != null) {
        timeout = ((Integer)param).intValue();
    }
    this.conn.setSocketTimeout(timeout);                    
}
 
Example #29
Source File: StrictSSLProtocolSocketFactory.java    From http4e with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * This method employs several techniques to circumvent the limitations of older JREs that 
 * do not support connect timeout. When running in JRE 1.4 or above reflection is used to 
 * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older 
 * JREs a controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the timeout 
 * expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(
    final String host,
    final int port,
    final InetAddress localAddress,
    final int localPort,
    final HttpConnectionParams params
) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    Socket socket = null;
    
    SocketFactory socketfactory = SSLSocketFactory.getDefault();
    if (timeout == 0) {
        socket = socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }
    verifyHostname((SSLSocket)socket);
    return socket;
}
 
Example #30
Source File: DavGatewaySSLProtocolSocketFactory.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort, HttpConnectionParams params) throws IOException {
    try {
        return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | InvalidAlgorithmParameterException e) {
        throw new IOException(e + " " + e.getMessage());
    }
}