Search in sources :

Example 51 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method performConnect.

private IStatus performConnect(IProgressMonitor monitor) {
    // there might be more ticks in the future perhaps for
    // connect socket, certificate validation, send request, authenticate,
    int ticks = 1;
    monitor.beginTask(getRemoteFileURL().toString() + Messages.HttpClientRetrieveFileTransfer_CONNECTING_TASK_NAME, ticks);
    try {
        if (monitor.isCanceled())
            throw newUserCancelledException();
        httpContext = new BasicHttpContext();
        httpResponse = httpClient.execute(getMethod, httpContext);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + responseCode);
    } catch (final Exception e) {
        // $NON-NLS-1$
        Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "performConnect", e);
        if (!isDone()) {
            setDoneException(e);
        }
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : BasicHttpContext(org.apache.http.protocol.BasicHttpContext) ECFRuntimeException(org.eclipse.ecf.core.util.ECFRuntimeException) BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 52 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project jbpm by kiegroup.

the class RESTWorkItemHandler method doRequestWithAuthorization.

protected HttpResponse doRequestWithAuthorization(HttpClient httpclient, HttpRequestBase httpMethod, Map<String, Object> params, AuthenticationType type) {
    if (type == null || type == AuthenticationType.NONE) {
        try {
            return httpclient.execute(httpMethod);
        } catch (Exception e) {
            throw new RuntimeException("Could not execute request [" + httpMethod.getMethod() + "] " + httpMethod.getURI(), e);
        }
    }
    String u = (String) params.get(PARAM_USERNAME);
    String p = (String) params.get(PARAM_PASSWORD);
    if (u == null || p == null) {
        u = this.username;
        p = this.password;
    }
    if (u == null) {
        throw new IllegalArgumentException("Could not find username");
    }
    if (p == null) {
        throw new IllegalArgumentException("Could not find password");
    }
    if (type == AuthenticationType.BASIC) {
        HttpHost targetHost = new HttpHost(httpMethod.getURI().getHost(), httpMethod.getURI().getPort(), httpMethod.getURI().getScheme());
        ((DefaultHttpClient) httpclient).getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(u, p));
        // 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(targetHost, basicAuth);
        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        try {
            return httpclient.execute(targetHost, httpMethod, localcontext);
        } catch (Exception e) {
            throw new RuntimeException("Could not execute request [" + httpMethod.getMethod() + "] " + httpMethod.getURI(), e);
        }
    } else if (type == AuthenticationType.FORM_BASED) {
        String authUrlStr = (String) params.get(PARAM_AUTHURL);
        if (authUrlStr == null) {
            authUrlStr = authUrl;
        }
        if (authUrlStr == null) {
            throw new IllegalArgumentException("Could not find authentication url");
        }
        try {
            httpclient.execute(httpMethod);
        } catch (IOException e) {
            throw new RuntimeException("Could not execute request for form-based authentication", e);
        } finally {
            httpMethod.releaseConnection();
        }
        HttpPost authMethod = new HttpPost(authUrlStr);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", u));
        nvps.add(new BasicNameValuePair("j_password", p));
        authMethod.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
        try {
            httpclient.execute(authMethod);
        } catch (IOException e) {
            throw new RuntimeException("Could not initialize form-based authentication", e);
        } finally {
            authMethod.releaseConnection();
        }
        try {
            return httpclient.execute(httpMethod);
        } catch (Exception e) {
            throw new RuntimeException("Could not execute request [" + httpMethod.getMethod() + "] " + httpMethod.getURI(), e);
        }
    } else {
        throw new RuntimeException("Unknown AuthenticationType " + type);
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) AuthScope(org.apache.http.auth.AuthScope) List(java.util.List) ArrayList(java.util.ArrayList)

Example 53 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext 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 54 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project pinpoint by naver.

the class DefaultHttpRequestRetryHandlerModifierIT method test.

@Test
public void test() throws Exception {
    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
    IOException iOException = new IOException();
    HttpContext context = new BasicHttpContext();
    assertTrue(retryHandler.retryRequest(iOException, 1, context));
    assertTrue(retryHandler.retryRequest(iOException, 2, context));
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 1"), annotation("RETURN_DATA", true)));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 2"), annotation("RETURN_DATA", true)));
    verifier.verifyTraceCount(0);
}
Also used : BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 55 with BasicHttpContext

use of org.apache.http.protocol.BasicHttpContext in project pinpoint by naver.

the class HttpClient4PluginTest method addDefaultHttpRequestRetryHandlerClass.

@Test
public void addDefaultHttpRequestRetryHandlerClass() {
    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
    IOException iOException = new IOException();
    HttpContext context = new BasicHttpContext();
    assertTrue(retryHandler.retryRequest(iOException, 1, context));
    assertTrue(retryHandler.retryRequest(iOException, 2, context));
}
Also used : BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

BasicHttpContext (org.apache.http.protocol.BasicHttpContext)60 HttpContext (org.apache.http.protocol.HttpContext)37 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)21 HttpGet (org.apache.http.client.methods.HttpGet)19 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)14 HttpHost (org.apache.http.HttpHost)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)11 HttpEntity (org.apache.http.HttpEntity)10 HttpPost (org.apache.http.client.methods.HttpPost)10 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)10 Header (org.apache.http.Header)8 BasicScheme (org.apache.http.impl.auth.BasicScheme)8 InputStream (java.io.InputStream)7 AuthScope (org.apache.http.auth.AuthScope)7 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 URI (java.net.URI)6 ArrayList (java.util.ArrayList)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6