Search in sources :

Example 1 with HttpCallerException

use of com.alibaba.csb.sdk.HttpCallerException in project csb-sdk by aliyun.

the class HttpClientFactory method createCloseableHttpClient.

public static CloseableHttpClient createCloseableHttpClient(PoolingHttpClientConnectionManager connManager) throws HttpCallerException {
    ConnectionKeepAliveStrategy myStrategy = createKeepAliveStrategy();
    CloseableHttpClient client;
    try {
        client = HttpClients.custom().setConnectionManager(connManager).setKeepAliveStrategy(myStrategy).build();
    } catch (Exception e) {
        throw new HttpCallerException("Failed to create httpclient: " + e.getMessage(), e);
    }
    return client;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpCallerException(com.alibaba.csb.sdk.HttpCallerException) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpCallerException(com.alibaba.csb.sdk.HttpCallerException)

Example 2 with HttpCallerException

use of com.alibaba.csb.sdk.HttpCallerException in project csb-sdk by aliyun.

the class HttpClientFactory method createConnManager.

/**
 * Create a connection pool which supports http and https socket
 * @return
 * @throws HttpCallerException
 */
public static PoolingHttpClientConnectionManager createConnManager() throws HttpCallerException {
    try {
        // ignore SSL certificate info with the below two setting:
        // 1. trust https server certificate always.
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

            @Override
            public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
                return true;
            }
        }).build();
        // 2. hostname verifier pass
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
        return new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    } catch (Exception e) {
        throw new HttpCallerException("Failed to create httpclient: " + e.getMessage(), e);
    }
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpCallerException(com.alibaba.csb.sdk.HttpCallerException) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpCallerException(com.alibaba.csb.sdk.HttpCallerException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder)

Example 3 with HttpCallerException

use of com.alibaba.csb.sdk.HttpCallerException in project csb-sdk by aliyun.

the class HttpClientHelper method getUrlPathInfo.

public static String getUrlPathInfo(String url) throws HttpCallerException {
    URL urlStr = null;
    try {
        urlStr = new URL(url);
    } catch (Exception e) {
        throw new HttpCallerException("url is unformat, url is " + url);
    }
    String path = urlStr.getPath();
    return path;
}
Also used : HttpCallerException(com.alibaba.csb.sdk.HttpCallerException) URL(java.net.URL) InvalidParameterException(java.security.InvalidParameterException) HttpCallerException(com.alibaba.csb.sdk.HttpCallerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HttpCallerException (com.alibaba.csb.sdk.HttpCallerException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 InvalidParameterException (java.security.InvalidParameterException)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLContext (javax.net.ssl.SSLContext)1 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)1 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)1 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)1 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 TrustStrategy (org.apache.http.conn.ssl.TrustStrategy)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)1