Example usage for org.apache.http.auth AuthScope AuthScope

List of usage examples for org.apache.http.auth AuthScope AuthScope

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope AuthScope.

Prototype

public AuthScope(final String host, final int port) 

Source Link

Document

Creates a new credentials scope for the given host, port, any realm name, and any authentication scheme.

Usage

From source file:edu.hust.grid.crawl.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

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

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());
    params.setBooleanParameter("http.protocol.handle-redirects", false);
    params.setParameter("http.language.Accept-Language", "en-us");
    params.setParameter("http.protocol.content-charset", "UTF-8");
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*  www . ja  v a2  s .  c  om*/

    connectionManager = new ThreadSafeClientConnManager(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.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
    }

    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.ibm.streamsx.topology.internal.context.AnalyticsServiceStreamsContext.java

private CloseableHttpClient createHttpClient(JSONObject credentials) {

    UsernamePasswordCredentials upc = new UsernamePasswordCredentials(credentials.get("userid").toString(),
            credentials.get("password").toString());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(credentials.get("rest_host").toString(), AuthScope.ANY_PORT),
            upc);//w ww.  java 2  s .c  o m
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    return httpClient;
}

From source file:org.hawk.service.api.utils.APIUtils.java

@SuppressWarnings({ "deprecation", "restriction" })
public static <T extends TServiceClient> T connectTo(Class<T> clazz, String url, ThriftProtocol thriftProtocol,
        final Credentials credentials) throws TTransportException, URISyntaxException {
    try {//from   w  w w .  jav a 2s .  c o m
        final URI parsed = new URI(url);

        TTransport transport;
        if (parsed.getScheme().startsWith("http")) {
            final DefaultHttpClient httpClient = APIUtils.createGZipAwareHttpClient();
            if (credentials != null) {
                httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), credentials);
            }
            transport = new THttpClient(url, httpClient);
        } else {
            transport = new TZlibTransport(new TSocket(parsed.getHost(), parsed.getPort()));
            transport.open();
        }
        Constructor<T> constructor = clazz.getDeclaredConstructor(org.apache.thrift.protocol.TProtocol.class);
        return constructor.newInstance(thriftProtocol.getProtocolFactory().getProtocol(transport));
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new TTransportException(e);
    }
}

From source file:com.calmio.calm.integration.Helpers.HTTPHandler.java

private static CredentialsProvider getCredentialsProvider(String url, String user, String pwd)
        throws URISyntaxException, MalformedURLException {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    URL uri = new URL(url);
    String domain = uri.getHost();
    domain = domain.startsWith("www.") ? domain.substring(4) : domain;
    credsProvider.setCredentials(new AuthScope(domain, uri.getPort()),
            (Credentials) new UsernamePasswordCredentials(user, pwd));
    return credsProvider;
}

From source file:com.autonomousturk.crawler.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    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.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    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.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*from ww  w.  java 2  s. c o  m*/

    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.aliyun.oss.common.comm.DefaultServiceClient.java

public DefaultServiceClient(ClientConfiguration config) {
    super(config);
    this.connectionManager = createHttpClientConnectionManager();
    this.httpClient = createHttpClient(this.connectionManager);
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout());
    requestConfigBuilder.setSocketTimeout(config.getSocketTimeout());
    requestConfigBuilder.setConnectionRequestTimeout(config.getConnectionRequestTimeout());

    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null && proxyPort > 0) {
        this.proxyHttpHost = new HttpHost(proxyHost, proxyPort);
        requestConfigBuilder.setProxy(proxyHttpHost);

        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        String proxyDomain = config.getProxyDomain();
        String proxyWorkstation = config.getProxyWorkstation();
        if (proxyUsername != null && proxyPassword != null) {
            this.credentialsProvider = new BasicCredentialsProvider();
            this.credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
                    new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));

            this.authCache = new BasicAuthCache();
            authCache.put(this.proxyHttpHost, new BasicScheme());
        }//  w w  w  .jav a2s  . c  o  m
    }

    this.requestConfig = requestConfigBuilder.build();
}

From source file:org.hyperic.hq.plugin.jboss7.JBossAdminHttp.java

public JBossAdminHttp(Properties props) throws PluginException {
    try {/*from   w  ww. j a v  a  2 s .  c  o m*/
        int port = Integer.parseInt(props.getProperty(JBossStandaloneDetector.PORT));
        String addr = props.getProperty(JBossStandaloneDetector.ADDR);
        boolean https = "true".equals(props.getProperty(JBossStandaloneDetector.HTTPS));
        this.user = props.getProperty(JBossStandaloneDetector.USERNAME);
        this.pass = props.getProperty(JBossStandaloneDetector.PASSWORD);
        this.hostName = props.getProperty(JBossStandaloneDetector.HOST);
        this.serverName = props.getProperty(JBossStandaloneDetector.SERVER);
        log.debug("props=" + props);

        targetHost = new HttpHost(addr, port, https ? "https" : "http");
        log.debug("targetHost=" + targetHost);
        AgentKeystoreConfig config = new AgentKeystoreConfig();
        client = new HQHttpClient(config, new HttpConfig(5000, 5000, null, 0), config.isAcceptUnverifiedCert());
        if ((user != null) && (pass != null)) {
            client.getCredentialsProvider().setCredentials(
                    new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                    new UsernamePasswordCredentials(user, pass));
        }
    } catch (Throwable ex) {
        throw new PluginException(ex.getMessage(), ex);
    }

    //        AuthCache authCache = new BasicAuthCache();
    //        DigestScheme digestAuth = new DigestScheme();
    //        digestAuth.overrideParamter("realm", "'TestRealm'");
    //        authCache.put(targetHost, digestAuth);
    //        localcontext = new BasicHttpContext();
    //        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}

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

public PageFetcher(FetchConfig config) {
    super(config);
    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()));
    }/*from  w w w. j a va  2 s  . co m*/

    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();

}