Example usage for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory.

Prototype

public static SSLSocketFactory getSocketFactory() throws SSLInitializationException 

Source Link

Document

Obtains default SSL socket factory with an SSL context based on the standard JSSE trust material (cacerts file in the security properties directory).

Usage

From source file:org.ebayopensource.fidouafclient.curl.Curl.java

private static HttpClient createHttpsClient() {
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    HttpClient client = new DefaultHttpClient();
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
    return httpClient;
}

From source file:edu.uci.ics.crawler4j.crawler.fetcher.PageFetcher.java

public PageFetcher(ICrawlerSettings config) {
    politenessDelay = config.getPolitenessDelay();
    maxDownloadSize = config.getMaxDownloadSize();
    show404Pages = config.getShow404Pages();
    ignoreBinary = !config.getIncludeBinaryContent();
    cache = config.getCacheProvider();// w  w  w.j a va  2 s .  com

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter("http.useragent", config.getUserAgent());

    params.setIntParameter("http.socket.timeout", config.getSocketTimeout());

    params.setIntParameter("http.connection.timeout", config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.getAllowHttps()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    connectionManager.setMaxTotal(config.getMaxTotalConnections());

    logger.setLevel(Level.INFO);
    httpclient = new DefaultHttpClient(connectionManager, params);
}

From source file:com.dalaran.async.task.http.AbstractHTTPService.java

protected String callPostRemoteService(String url, String json) throws IOException {
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 2008));

    HttpPost httpPost = new HttpPost(url);

    StringEntity se = new StringEntity(json);
    se.setContentEncoding(HTTP.UTF_8);//from  w ww  .  j  a v  a  2s  . c  o  m
    //        httpPost.setHeader(HTTP.USER_AGENT, URLs.USER_AGENT);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
    SingleClientConnManager cm = new SingleClientConnManager(httpPost.getParams(), schReg);

    httpPost.setEntity(se);
    HttpClient client = new DefaultHttpClient();

    HttpResponse response = null;
    try {
        response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        return responseToBuffer(client, entity);
    } catch (Exception e) {
        return null;
    }
}

From source file:net.sf.dvstar.transmission.protocol.TransmissionWebClient.java

public TransmissionWebClient(boolean authenticate, Logger loggerProvider) {
    this.authenticate = authenticate;
    this.loggerProvider = loggerProvider;

    configStorage = new ConfigStorage();
    configStorage.loadConfig();//from   www  .ja v  a 2s. com
    localSettiingsFactory = new LocalSettiingsFactory();
    localSettiingsFactory.setConfigProperties(configStorage);

    // PROXY

    //Properties systemSettings = System.getProperties();
    //systemSettings.put("http.proxyHost", localSettiingsFactory.getProxyHost()/* "192.168.4.7"*/);
    //systemSettings.put("http.proxyPort", localSettiingsFactory.getProxyPort()/* "3128"*/);
    //System.setProperties(systemSettings);

    //HttpHost target = new HttpHost(localSettiingsFactory.getHost(), Integer.parseInt(localSettiingsFactory.getPort()), "http");
    targetHttpHost = new HttpHost(localSettiingsFactory.getHost(), localSettiingsFactory.getIPort(), //80,
            "http");

    //DefaultHttpClient httpclient = new DefaultHttpClient();

    // general setup
    SchemeRegistry supportedSchemes = new SchemeRegistry();

    // Register the "http" and "https" protocol schemes, they are
    // required by the default operator to look up socket factories.
    supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    // prepare parameters
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, true);

    ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes);

    int connectionTimeoutMillis = 5 * 1000;
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
    HttpConnectionParams.setSoTimeout(params, connectionTimeoutMillis);

    httpclient = new DefaultHttpClient(ccm, params);

    if (localSettiingsFactory.isUseProxy()) {
        proxyHttpHost = new HttpHost(localSettiingsFactory.getProxyHost(),
                Integer.parseInt(localSettiingsFactory.getProxyPort()), "http");
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);
    }

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(localSettiingsFactory.getHost(), Integer.parseInt(localSettiingsFactory.getPort())),
            new UsernamePasswordCredentials(localSettiingsFactory.getUser(), localSettiingsFactory.getPass()));

}

From source file:org.sbs.goodcrawler.fetcher.Fetcher.java

public void init(File fetchConfFile) {
    FetchConfig fetchConfig = new FetchConfig();
    Document document;//from  w  w  w.  ja  v  a2s .  com
    try {
        document = Jsoup.parse(fetchConfFile, "utf-8");
        Fetcher.config = fetchConfig.loadConfig(document);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getAgent());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeoutMilliseconds());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isHttps()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }

    connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:com.github.rnewson.couchdb.lucene.HttpClientFactory.java

public static synchronized HttpClient getInstance() throws MalformedURLException {
    if (instance == null) {
        final HttpParams params = new BasicHttpParams();
        // protocol params.
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUseExpectContinue(params, false);
        // connection params.
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        ConnManagerParams.setMaxTotalConnections(params, 1000);
        ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1000));

        final SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5984));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        final ClientConnectionManager cm = new ShieldedClientConnManager(
                new ThreadSafeClientConnManager(params, schemeRegistry));

        instance = new DefaultHttpClient(cm, params);

        if (INI != null) {
            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
            final Iterator<?> it = INI.getKeys();
            while (it.hasNext()) {
                final String key = (String) it.next();
                if (!key.startsWith("lucene.") && key.endsWith(".url")) {
                    final URL url = new URL(INI.getString(key));
                    if (url.getUserInfo() != null) {
                        credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
                                new UsernamePasswordCredentials(url.getUserInfo()));
                    }// w  ww . j a  va 2s. com
                }
            }
            instance.setCredentialsProvider(credsProvider);
            instance.addRequestInterceptor(new PreemptiveAuthenticationRequestInterceptor(), 0);
        }
    }
    return instance;
}

From source file:com.flipkart.poseidon.handlers.http.impl.HttpConnectionPool.java

/** Constructor
 * @param host Host Name/*www . ja v  a  2 s .c  om*/
 * @param port Port Name
 * @param secure
 * @param connectionTimeout
 * @param operationTimeout
 * @param maxConnections
 * @param processQueueSize
 * @param timeToLiveInSecs
 */
protected HttpConnectionPool(final String name, String host, Integer port, Boolean secure,
        Integer connectionTimeout, Integer operationTimeout, Integer maxConnections, Integer processQueueSize,
        Integer timeToLiveInSecs) {
    this.name = name;
    this.host = host;
    this.port = port;
    this.secure = secure;
    this.headers = new HashMap<String, String>();
    this.processQueue = new Semaphore(processQueueSize + maxConnections);
    if (timeToLiveInSecs != null) {
        this.timeToLiveInSecs = timeToLiveInSecs;
    }
    this.requestGzipEnabled = false;
    this.responseGzipEnabled = false;

    // create scheme
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    if (this.secure) {
        schemeRegistry.register(new Scheme("https", port, SSLSocketFactory.getSocketFactory()));
    } else {
        schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
    }

    // create connection manager
    PoolingClientConnectionManager cm;
    if (this.timeToLiveInSecs > 0) {
        cm = new PoolingClientConnectionManager(schemeRegistry, this.timeToLiveInSecs, TimeUnit.SECONDS);
    } else {
        cm = new PoolingClientConnectionManager(schemeRegistry);
    }

    // Max pool size
    cm.setMaxTotal(maxConnections);

    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(maxConnections);

    // Increase max connections for host:port
    HttpHost httpHost = new HttpHost(host, port);
    cm.setMaxPerRoute(new HttpRoute(httpHost), maxConnections);

    // set timeouts
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParams, operationTimeout);

    // create client pool
    this.client = new DefaultHttpClient(cm, httpParams);

    // policies (cookie)
    this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

    // adding gzip support for http client
    addGzipHeaderInRequestResponse();

}

From source file:eu.sisob.uma.api.crawler4j.crawler.PageFetcher.java

public synchronized static void startConnectionMonitorThread() {
    if (connectionMonitorThread == null) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
        paramsBean.setVersion(HttpVersion.HTTP_1_1);
        paramsBean.setContentCharset("UTF-8");
        paramsBean.setUseExpectContinue(false);

        params.setParameter("http.useragent", Configurations.getStringProperty("fetcher.user_agent",
                "crawler4j (http://code.google.com/p/crawler4j/)"));

        params.setIntParameter("http.socket.timeout",
                Configurations.getIntProperty("fetcher.socket_timeout", 20000));

        params.setIntParameter("http.connection.timeout",
                Configurations.getIntProperty("fetcher.connection_timeout", 30000));

        params.setBooleanParameter("http.protocol.handle-redirects", false);

        ConnPerRouteBean connPerRouteBean = new ConnPerRouteBean();
        connPerRouteBean//w  ww.ja  va 2 s.  c om
                .setDefaultMaxPerRoute(Configurations.getIntProperty("fetcher.max_connections_per_host", 100));
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRouteBean);
        ConnManagerParams.setMaxTotalConnections(params,
                Configurations.getIntProperty("fetcher.max_total_connections", 100));

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        if (Configurations.getBooleanProperty("fetcher.crawl_https", false)) {
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        }

        connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

        ProjectLogger.LOGGER.setLevel(Level.INFO);
        httpclient = new DefaultHttpClient(connectionManager, params);
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:de.damdi.fitness.activity.settings.sync.RestClient.java

/**
 * Creates a rest client.//w ww .j  ava 2  s.  c  o m
 * 
 * @param hostname
 *            The host name of the server
 * @param port
 *            The TCP port of the server that should be addressed
 * @param scheme
 *            The used protocol scheme
 * @param versionCode
 *            The version of the app (used for user agent)
 * 
 */
public RestClient(final String hostname, final int port, final String scheme, final int versionCode) {
    final StringBuilder uri = new StringBuilder(scheme);
    uri.append("://");
    uri.append(hostname);
    if (port > 0) {
        uri.append(":");
        uri.append(port);
    }
    mBaseUri = uri.toString();
    mHostName = hostname;

    //TODO Fix SSL problem before exchanging user data
    // workaround for SSL problems, may lower the security level (man-in-the-middle-attack possible)
    // Android does not use the correct SSL certificate for wger.de and throws the exception 
    // javax.net.ssl.SSLException: hostname in certificate didn't match: <wger.de> != <vela.uberspace.de> OR <vela.uberspace.de> OR <uberspace.de> OR <*.vela.uberspace.de>
    // issue is not too serious as no user data is exchanged at the moment
    Log.w(TAG,
            "OpenTraining will accept all SSL-certificates. This issue has to be fixed before exchanging real user data.");
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    ClientConnectionManager mgr = new ThreadSafeClientConnManager(client.getParams(), registry);
    mClient = new DefaultHttpClient(mgr, client.getParams());

    mClient.setRedirectHandler(sRedirectHandler);
    mClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);

    // Set verifier     
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // set user agent
    USER_AGENT = "opentraining/" + versionCode;
}

From source file:me.pjq.benchmark.AndroidHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 *
 * @param userAgent to report in your HTTP requests.
 * @param sessionCache persistent session cache
 * @return AndroidHttpClient for you to use for all your requests.
 *///w  ww .  ja v  a  2s. c o m

public static AndroidHttpClient newInstance(String userAgent)/*,
                                                             SSLClientSessionCache sessionCache) 
                                                             */
{
    HttpParams params = new BasicHttpParams();

    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    // Default connection and socket timeout of 20 seconds.  Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);

    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    //        schemeRegistry.register(new Scheme("https",
    //                socketFactoryWithCache(sessionCache), 443));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    //        DhcpInfo info = new DhcpInfo();
    //        UVANLogger.getInstance().d("Server", info.toString());

    //to do waiting for a while
    //        HttpHost host = UVANApplication.getHttpProxy();
    //        if (host==null){
    //           host = new HttpHost("0.0.0.0");
    //        }
    //        HttpRoute route = new HttpRoute(host);
    //        
    //        Object object = new Object();
    //        try {
    //           ManagedClientConnection managedClientConnection = manager.requestConnection(route, object).getConnection(20 * 1000, TimeUnit.MILLISECONDS);
    //           managedClientConnection.setIdleDuration(5 * 60 * 1000, TimeUnit.MILLISECONDS);
    //        } catch (ConnectionPoolTimeoutException e) {
    //         e.printStackTrace();
    //      } catch (InterruptedException e) {
    //         e.printStackTrace();
    //      }

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}