Search in sources :

Example 6 with HttpRoutePlanner

use of org.apache.http.conn.routing.HttpRoutePlanner in project fess-crawler by codelibs.

the class HcHttpClient method init.

@Override
public synchronized void init() {
    if (httpClient != null) {
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }
    super.init();
    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE, Boolean.class);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }
    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout, Integer.class);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);
    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout, Integer.class);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }
    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    @SuppressWarnings("unchecked") final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY, authSchemeProviderMap, Map.class);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }
    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent, String.class);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }
    final HttpRoutePlanner planner = buildRoutePlanner();
    if (planner != null) {
        httpClientBuilder.setRoutePlanner(planner);
    }
    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY, new Authentication[0], Authentication[].class);
    final List<Pair<FormScheme, Credentials>> formSchemeList = new ArrayList<>();
    for (final Authentication authentication : siteCredentialList) {
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScheme instanceof FormScheme) {
            formSchemeList.add(new Pair<>((FormScheme) authScheme, authentication.getCredentials()));
        } else {
            final AuthScope authScope = authentication.getAuthScope();
            credentialsProvider.setCredentials(authScope, authentication.getCredentials());
            if (authScope.getHost() != null && authScheme != null) {
                final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
                authCache.put(targetHost, authScheme);
            }
        }
    }
    httpClientContext.setAuthCache(authCache);
    httpClientContext.setCredentialsProvider(credentialsProvider);
    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0], RequestHeader[].class);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }
    // do not redirect
    requestConfigBuilder.setRedirectsEnabled(getInitParameter(REDIRECTS_ENABLED, redirectsEnabled, Boolean.class));
    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }
    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0], Cookie[].class);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }
    // cookie registry
    final Lookup<CookieSpecProvider> cookieSpecRegistry = buildCookieSpecRegistry();
    if (cookieSpecRegistry != null) {
        httpClientBuilder.setDefaultCookieSpecRegistry(cookieSpecRegistry);
    }
    // SSL
    final LayeredConnectionSocketFactory sslSocketFactory = buildSSLSocketFactory();
    if (sslSocketFactory != null) {
        httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
    }
    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout), connectionCheckInterval, true);
    final CloseableHttpClient closeableHttpClient = httpClientBuilder.setDnsResolver(dnsResolver).setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build()).build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }
    formSchemeList.forEach(p -> {
        final FormScheme scheme = p.getFirst();
        final Credentials credentials = p.getSecond();
        scheme.authenticate(credentials, (request, consumer) -> {
            // request header
            for (final Header header : requestHeaderList) {
                request.addHeader(header);
            }
            HttpEntity httpEntity = null;
            try {
                final HttpResponse response = closeableHttpClient.execute(request, new BasicHttpContext(httpClientContext));
                httpEntity = response.getEntity();
                consumer.accept(response, httpEntity);
            } catch (final Exception e) {
                request.abort();
                logger.warn("Failed to authenticate on " + scheme, e);
            } finally {
                EntityUtils.consumeQuietly(httpEntity);
            }
        });
    });
    httpClient = closeableHttpClient;
}
Also used : HttpEntity(org.apache.http.HttpEntity) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) ArrayList(java.util.ArrayList) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) AuthScheme(org.apache.http.auth.AuthScheme) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpHost(org.apache.http.HttpHost) BeanDesc(org.codelibs.core.beans.BeanDesc) PropertyDesc(org.codelibs.core.beans.PropertyDesc) Pair(org.codelibs.core.misc.Pair) Cookie(org.apache.http.cookie.Cookie) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) ParseException(java.text.ParseException) NoRouteToHostException(java.net.NoRouteToHostException) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) AuthScope(org.apache.http.auth.AuthScope) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider) RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Map(java.util.Map) HashMap(java.util.HashMap) FormScheme(org.codelibs.fess.crawler.client.http.form.FormScheme) BasicHeader(org.apache.http.message.BasicHeader) Credentials(org.apache.http.auth.Credentials)

Example 7 with HttpRoutePlanner

use of org.apache.http.conn.routing.HttpRoutePlanner in project dropwizard by dropwizard.

the class HttpClientBuilderTest method usesACustomRoutePlanner.

@Test
void usesACustomRoutePlanner() throws Exception {
    final HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.52.1", 8080)));
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
    final CloseableHttpClient httpClient = builder.using(configuration).using(routePlanner).createClient(apacheBuilder, connectionManager, "test").getClient();
    assertThat(httpClient).isNotNull();
    assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isSameAs(routePlanner);
    assertThat(spyHttpClientField("routePlanner", httpClient)).isSameAs(routePlanner);
}
Also used : ProxySelector(java.net.ProxySelector) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.jupiter.api.Test)

Example 8 with HttpRoutePlanner

use of org.apache.http.conn.routing.HttpRoutePlanner in project pentaho-kettle by pentaho.

the class JobEntryHTTP method getResultFromHttpSchema.

private InputStream getResultFromHttpSchema(String realUploadFile, URI uri) throws IOException, KettleException {
    HttpClient client = null;
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (!Utils.isEmpty(username)) {
        String realPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
        String realUser = environmentSubstitute(username);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(realUser, realPassword != null ? realPassword : "");
        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, credentials);
        clientBuilder.setDefaultCredentialsProvider(provider);
    }
    String proxyHostnameValue = environmentSubstitute(proxyHostname);
    String proxyPortValue = environmentSubstitute(proxyPort);
    String nonProxyHostsValue = environmentSubstitute(nonProxyHosts);
    if (!Utils.isEmpty(proxyHostnameValue)) {
        HttpHost proxy = new HttpHost(proxyHostnameValue, Integer.parseInt(proxyPortValue));
        clientBuilder.setProxy(proxy);
        if (!Utils.isEmpty(nonProxyHostsValue)) {
            HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) {

                @Override
                public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
                    String hostName = host.getHostName();
                    if (hostName.matches(nonProxyHostsValue)) {
                        // Return direct route
                        return new HttpRoute(host);
                    }
                    return super.determineRoute(host, request, context);
                }
            };
            clientBuilder.setRoutePlanner(routePlanner);
        }
    }
    client = clientBuilder.build();
    HttpRequestBase httpRequestBase;
    // See if we need to send a file over?
    if (!Utils.isEmpty(realUploadFile)) {
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobHTTP.Log.SendingFile", realUploadFile));
        }
        httpRequestBase = new HttpPost(uri);
        // Get content of file
        String content = new String(Files.readAllBytes(Paths.get(realUploadFile)));
        // upload data to web server
        StringEntity requestEntity = new StringEntity(content);
        requestEntity.setContentType("application/x-www-form-urlencoded");
        ((HttpPost) httpRequestBase).setEntity(requestEntity);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobHTTP.Log.FinishedSendingFile"));
        }
    } else {
        httpRequestBase = new HttpGet(uri);
    }
    // if we have HTTP headers, add them
    if (!Utils.isEmpty(headerName)) {
        if (log.isDebug()) {
            log.logDebug(BaseMessages.getString(PKG, "JobHTTP.Log.HeadersProvided"));
        }
        for (int j = 0; j < headerName.length; j++) {
            if (!Utils.isEmpty(headerValue[j])) {
                httpRequestBase.addHeader(environmentSubstitute(headerName[j]), environmentSubstitute(headerValue[j]));
                if (log.isDebug()) {
                    log.logDebug(BaseMessages.getString(PKG, "JobHTTP.Log.HeaderSet", environmentSubstitute(headerName[j]), environmentSubstitute(headerValue[j])));
                }
            }
        }
    }
    // Get a stream for the specified URL
    HttpResponse response = null;
    if (!Utils.isEmpty(proxyHostname)) {
        HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
        response = client.execute(target, httpRequestBase, localContext);
    } else {
        response = client.execute(httpRequestBase);
    }
    responseStatusCode = response.getStatusLine().getStatusCode();
    if (HttpStatus.SC_OK != responseStatusCode) {
        throw new KettleException("StatusCode: " + responseStatusCode);
    }
    if (log.isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobHTTP.Log.StartReadingReply"));
    }
    logBasic(BaseMessages.getString(PKG, "JobHTTP.Log.ReplayInfo", response.getEntity().getContentType(), response.getLastHeader(HttpHeaders.DATE).getValue()));
    // Read the result from the server...
    return response.getEntity().getContent();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpPost(org.apache.http.client.methods.HttpPost) BasicScheme(org.apache.http.impl.auth.BasicScheme) KettleException(org.pentaho.di.core.exception.KettleException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpRoute(org.apache.http.conn.routing.HttpRoute) StringEntity(org.apache.http.entity.StringEntity) HttpHost(org.apache.http.HttpHost) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpClient(org.apache.http.client.HttpClient)

Example 9 with HttpRoutePlanner

use of org.apache.http.conn.routing.HttpRoutePlanner in project vscrawler by virjar.

the class CrawlerSession method decorateRoutePlanner.

private void decorateRoutePlanner(CrawlerHttpClient crawlerHttpClient) {
    HttpRoutePlanner routePlanner = crawlerHttpClient.getRoutePlanner();
    if (!(routePlanner instanceof ProxyBindRoutPlanner)) {
        log.warn("自定义了代理发生器,vscrawler的代理功能将不会生效");
        return;
    }
    VSCrawlerRoutePlanner vsCrawlerRoutePlanner = new VSCrawlerRoutePlanner((ProxyBindRoutPlanner) routePlanner, ipPool, proxyPlanner, this);
    crawlerHttpClient.setRoutePlanner(vsCrawlerRoutePlanner);
}
Also used : VSCrawlerRoutePlanner(com.virjar.vscrawler.core.net.proxy.VSCrawlerRoutePlanner) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) ProxyBindRoutPlanner(com.virjar.dungproxy.client.httpclient.conn.ProxyBindRoutPlanner)

Example 10 with HttpRoutePlanner

use of org.apache.http.conn.routing.HttpRoutePlanner in project java-sdk by optimizely.

the class OptimizelyFactoryTest method newDefaultInstanceWithDatafileAccessTokenAndCustomHttpClient.

@Test
public void newDefaultInstanceWithDatafileAccessTokenAndCustomHttpClient() throws Exception {
    // Add custom Proxy and Port here
    int port = 443;
    String proxyHostName = "someProxy.com";
    HttpHost proxyHost = new HttpHost(proxyHostName, port);
    HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
    HttpClientBuilder clientBuilder = HttpClients.custom();
    clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
    CloseableHttpClient httpClient = clientBuilder.build();
    String datafileString = Resources.toString(Resources.getResource("valid-project-config-v4.json"), Charsets.UTF_8);
    optimizely = OptimizelyFactory.newDefaultInstance("sdk-key", datafileString, "auth-token", httpClient);
    assertTrue(optimizely.isValid());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpHost(org.apache.http.HttpHost) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Test(org.junit.Test)

Aggregations

HttpRoutePlanner (org.apache.http.conn.routing.HttpRoutePlanner)10 HttpHost (org.apache.http.HttpHost)6 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 DefaultProxyRoutePlanner (org.apache.http.impl.conn.DefaultProxyRoutePlanner)4 IOException (java.io.IOException)3 AuthScope (org.apache.http.auth.AuthScope)3 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)3 CredentialsProvider (org.apache.http.client.CredentialsProvider)3 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)3 InetSocketAddress (java.net.InetSocketAddress)2 Proxy (java.net.Proxy)2 ProxySelector (java.net.ProxySelector)2 SocketAddress (java.net.SocketAddress)2 URI (java.net.URI)2 List (java.util.List)2 Header (org.apache.http.Header)2 HttpRequest (org.apache.http.HttpRequest)2 HttpResponse (org.apache.http.HttpResponse)2 Credentials (org.apache.http.auth.Credentials)2