Example usage for org.apache.http.impl.conn SingleClientConnManager SingleClientConnManager

List of usage examples for org.apache.http.impl.conn SingleClientConnManager SingleClientConnManager

Introduction

In this page you can find the example usage for org.apache.http.impl.conn SingleClientConnManager SingleClientConnManager.

Prototype

public SingleClientConnManager(final SchemeRegistry schreg) 

Source Link

Document

Creates a new simple connection manager.

Usage

From source file:org.xdi.oxd.license.client.ClientUtils.java

public static HttpClient createHttpClientTrustAll() {
    try {/*w  w  w . jav a2  s .  co  m*/
        SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }, new X509HostnameVerifier() {
            @Override
            public void verify(String host, SSLSocket ssl) throws IOException {
            }

            @Override
            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", 443, sf));
        ClientConnectionManager ccm = new SingleClientConnManager(registry);
        return new DefaultHttpClient(ccm);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.personium.core.utils.HttpClientFactory.java

/**
 * HTTPClient?./*www  .  j a  v  a  2s .co  m*/
 * @param type 
 * @return ???HttpClient
 */
public static HttpClient create(final String type) {
    if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
        return new DefaultHttpClient();
    }

    SSLSocketFactory sf = null;
    try {
        if (TYPE_INSECURE.equalsIgnoreCase(type)) {
            sf = createInsecureSSLSocketFactory();
        }
    } catch (Exception e) {
        return null;
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
    schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
    HttpParams params = new BasicHttpParams();
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    // ClientConnectionManager cm = new
    // ThreadSafeClientConnManager(schemeRegistry);
    HttpClient hc = new DefaultHttpClient(cm, params);

    HttpParams params2 = hc.getParams();
    int timeout = TIMEOUT;
    HttpConnectionParams.setConnectionTimeout(params2, timeout); // ?
    HttpConnectionParams.setSoTimeout(params2, timeout); // ??
    return hc;
}

From source file:com.fujitsu.dc.test.jersey.HttpClientFactory.java

/**
 * HTTPClient?./*from w  w w  . java2s.c  o  m*/
 * @param type 
 * @param connectionTimeout ()0????
 * @return ???HttpClient
 */
public static HttpClient create(final String type, final int connectionTimeout) {
    if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
        return new DefaultHttpClient();
    }

    SSLSocketFactory sf = null;
    try {
        if (TYPE_INSECURE.equalsIgnoreCase(type)) {
            sf = createInsecureSSLSocketFactory();
        }
    } catch (Exception e) {
        return null;
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
    schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
    HttpParams params = new BasicHttpParams();
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    // ClientConnectionManager cm = new
    // ThreadSafeClientConnManager(schemeRegistry);
    HttpClient hc = new DefaultHttpClient(cm, params);

    HttpParams params2 = hc.getParams();
    int timeout = TIMEOUT;
    if (connectionTimeout != 0) {
        timeout = connectionTimeout;
    }
    HttpConnectionParams.setConnectionTimeout(params2, timeout); // ?
    HttpConnectionParams.setSoTimeout(params2, timeout); // ??
    return hc;
}

From source file:anhttpclient.impl.ClientConnectionManagerFactoryImpl.java

/**
 * {@inheritDoc}/* w  ww . ja va 2 s. c om*/
 */
public ClientConnectionManager newInstance(HttpParams params, SchemeRegistry schemeRegistry) {
    if (params != null) {
        boolean threadSafe = params.getBooleanParameter(THREAD_SAFE_CONNECTION_MANAGER, false);
        return threadSafe ? new ThreadSafeClientConnManager(schemeRegistry)
                : new SingleClientConnManager(schemeRegistry);
    }

    return new SingleClientConnManager(schemeRegistry);
}

From source file:org.eclipse.dirigible.ide.common.io.ProxyUtils.java

public static HttpClient getHttpClient(boolean trustAll) {
    HttpClient httpClient = null;//from   w w w  . j  a  v  a2 s  .co  m

    // Local case only
    // try {
    // loadLocalBuildProxy();
    // } catch (IOException e) {
    // logger.error(e.getMessage(), e);
    // }

    if (trustAll) {
        try {
            SchemeSocketFactory plainSocketFactory = PlainSocketFactory.getSocketFactory();
            SchemeSocketFactory sslSocketFactory = new SSLSocketFactory(createTrustAllSSLContext());

            Scheme httpScheme = new Scheme("http", 80, plainSocketFactory);
            Scheme httpsScheme = new Scheme("https", 443, sslSocketFactory);

            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(httpScheme);
            schemeRegistry.register(httpsScheme);

            ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
            httpClient = new DefaultHttpClient(cm);
        } catch (Exception e) {
            httpClient = new DefaultHttpClient();
        }
    } else {
        httpClient = new DefaultHttpClient();
    }

    String httpProxyHost = EnvUtils.getEnv(HTTP_PROXY_HOST);
    String httpProxyPort = EnvUtils.getEnv(HTTP_PROXY_PORT);

    if ((httpProxyHost != null) && (httpProxyPort != null)) {
        HttpHost httpProxy = new HttpHost(httpProxyHost, Integer.parseInt(httpProxyPort));
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpProxy);
    }

    return httpClient;
}

From source file:com.cubeia.firebase.clients.java.connector.HttpConnector.java

@Override
public void connect() throws IOException, GeneralSecurityException {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    client = new DefaultHttpClient(cm);
    // StringWriter w = new StringWriter();
    // new Exception().printStackTrace(new PrintWriter(w));
    exchange = new HttpExchange(); // w.toString());
    exchange.start();//from ww w.  j ava 2s  .  co m
}

From source file:wsattacker.plugin.intelligentdos.requestSender.Http4RequestSenderImpl.java

public String sendRequestHttpClient(RequestObject requestObject) {
    String strUrl = requestObject.getEndpoint();
    String strXml = requestObject.getRequestContent();

    StringBuilder result = new StringBuilder();
    BufferedReader rd = null;//ww w. j  a  v  a2  s  .  c  o  m
    try {
        URL url = new URL(strUrl);
        String protocol = url.getProtocol();

        HttpClient client;
        if (protocol.equalsIgnoreCase("https")) {
            SSLSocketFactory sf = get();
            Scheme httpsScheme = new Scheme("https", url.getPort(), sf);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(httpsScheme);

            // apache HttpClient version >4.2 should use
            // BasicClientConnectionManager
            ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);

            client = new DefaultHttpClient(cm);
        } else {
            client = new DefaultHttpClient();
        }

        setParamsToClient(client);

        HttpPost post = new HttpPost(strUrl);
        setHeader(requestObject, post);

        ByteArrayEntity entity = new ByteArrayEntity(strXml.getBytes("UTF-8")) {
            @Override
            public void writeTo(OutputStream outstream) throws IOException {
                super.writeTo(outstream);
                sendLastByte = System.nanoTime();
            }
        };

        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        receiveFirstByte = System.nanoTime();

        rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), Charset.defaultCharset()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
    } catch (IOException e) {
        return e.getMessage();
    } catch (RuntimeException e) {
        return e.getMessage();
    } finally {
        if (rd != null) {
            try {
                rd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result.toString();
}

From source file:com.nesscomputing.tinyhttp.HttpFetcher.java

public HttpFetcher(final SSLConfig sslConfig) {
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    registry.register(HTTP_SCHEME);//w  w w .  jav  a  2 s . c  o  m

    if (sslConfig != null && sslConfig.isSSLEnabled()) {
        try {
            final TrustManager[] trustManagers = new TrustManager[] {
                    HttpsTrustManagerFactory.getTrustManager(sslConfig) };
            final SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagers, null);
            final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext,
                    SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);

            registry.register(new Scheme("https", 443, sslSocketFactory));
            LOG.debug("HTTPS enabled.");
        } catch (GeneralSecurityException ce) {
            throw Throwables.propagate(ce);
        } catch (IOException ioe) {
            throw Throwables.propagate(ioe);
        }
    } else {
        LOG.debug("HTTPS disabled.");
    }

    connectionManager = new SingleClientConnManager(registry);

    LOG.debug("HTTP fetcher ready.");
}

From source file:com.intel.cosbench.client.http.HttpClientUtil.java

private static ClientConnectionManager createClientConnManager() {
    SchemeRegistry sr = new SchemeRegistry();

    sr.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    sr.register(new Scheme("https", 443, createSSLSocketFactory()));

    return new SingleClientConnManager(sr);
}

From source file:com.gallery.GalleryRemote.GalleryComm.java

protected GalleryComm(Gallery g, StatusUpdate su) {
    if (g == null) {
        throw new IllegalArgumentException("Must supply a non-null gallery.");
    }//  ww w  .java  2  s. co  m

    this.g = g;
    this.su = su;

    SingleClientConnManager cm = null;

    // Set all-trusting SSL manager, if necessary
    if (g.getUrl().getProtocol().equals("https")) {
        try {
            SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getInstance("TLS"),
                    SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("https", sf, 443));
            cm = new SingleClientConnManager(schemeRegistry);
        } catch (NoSuchAlgorithmException e) {
            Log.logException(Log.LEVEL_CRITICAL, MODULE, e);
        }
    }

    httpclient = new DefaultHttpClient(cm);

    // Use default proxy (as defined by the JVM)
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpclient.setRoutePlanner(routePlanner);

    // use GR User-Agent
    httpclient.removeRequestInterceptorByClass(RequestUserAgent.class);
    final String ua = g.getUserAgent();
    httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            request.setHeader(HTTP.USER_AGENT, ua);
        }
    });
}