Example usage for org.apache.http.client.protocol HttpClientContext create

List of usage examples for org.apache.http.client.protocol HttpClientContext create

Introduction

In this page you can find the example usage for org.apache.http.client.protocol HttpClientContext create.

Prototype

public static HttpClientContext create() 

Source Link

Usage

From source file:org.apache.hadoop.gateway.GatewaySslFuncTest.java

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testKnox674SslCipherSuiteConfig() throws Exception {
    LOG_ENTER();/*from www  .  j  a va 2  s .  co m*/

    String topoStr = TestUtils.merge(DAT, "test-admin-topology.xml", params);
    File topoFile = new File(config.getGatewayTopologyDir(), "test-topology.xml");
    FileUtils.writeStringToFile(topoFile, topoStr);

    topos.reloadTopologies();

    String username = "guest";
    String password = "guest-password";
    String serviceUrl = gatewayUrl + "/test-topology/api/v1/version";

    HttpHost targetHost = new HttpHost("localhost", gatewayPort, gatewayScheme);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(username, password));

    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);

    CloseableHttpClient client = HttpClients.custom()
            .setSSLSocketFactory(
                    new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" },
                            new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, new TrustAllHosts()))
            .build();
    HttpGet request = new HttpGet(serviceUrl);
    CloseableHttpResponse response = client.execute(request, context);
    assertThat(the(new StreamSource(response.getEntity().getContent())), hasXPath("/ServerVersion/version"));
    response.close();
    client.close();

    gateway.stop();
    config.setExcludedSSLCiphers(Arrays.asList(new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }));
    config.setIncludedSSLCiphers(Arrays.asList(new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" }));

    startGatewayServer();
    serviceUrl = gatewayUrl + "/test-topology/api/v1/version";

    try {
        client = HttpClients.custom()
                .setSSLSocketFactory(
                        new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" },
                                new String[] { "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, new TrustAllHosts()))
                .build();
        request = new HttpGet(serviceUrl);
        client.execute(request, context);
        fail("Expected SSLHandshakeException");
    } catch (SSLHandshakeException e) {
        // Expected.
        client.close();
    }

    client = HttpClients.custom()
            .setSSLSocketFactory(
                    new SSLConnectionSocketFactory(createInsecureSslContext(), new String[] { "TLSv1.2" },
                            new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" }, new TrustAllHosts()))
            .build();
    request = new HttpGet(serviceUrl);
    response = client.execute(request, context);
    assertThat(the(new StreamSource(response.getEntity().getContent())), hasXPath("/ServerVersion/version"));
    response.close();
    client.close();

    LOG_EXIT();
}

From source file:org.fao.geonet.utils.AbstractHttpRequest.java

protected ClientHttpResponse doExecute(final HttpRequestBase httpMethod) throws IOException {
    return requestFactory.execute(httpMethod, new Function<HttpClientBuilder, Void>() {
        @Nullable//w w w.ja v  a  2  s.co  m
        @Override
        public Void apply(@Nonnull HttpClientBuilder input) {
            final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            if (credentials != null) {
                final URI uri = httpMethod.getURI();
                HttpHost hh = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                credentialsProvider.setCredentials(new AuthScope(hh), credentials);

                // Preemptive authentication
                if (isPreemptiveBasicAuth()) {
                    // 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(hh, basicAuth);

                    // Add AuthCache to the execution context
                    httpClientContext = HttpClientContext.create();
                    httpClientContext.setCredentialsProvider(credentialsProvider);
                    httpClientContext.setAuthCache(authCache);
                } else {
                    input.setDefaultCredentialsProvider(credentialsProvider);
                }
            } else {
                input.setDefaultCredentialsProvider(credentialsProvider);
            }

            if (useProxy) {
                final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                input.setProxy(proxy);
                if (proxyCredentials != null) {
                    credentialsProvider.setCredentials(new AuthScope(proxy), proxyCredentials);
                }
            }
            input.setRedirectStrategy(new LaxRedirectStrategy());
            return null;
        }
    }, this);
}

From source file:org.apache.manifoldcf.authorities.authorities.jira.JiraSession.java

private void getRest(String rightside, JiraJSONResponse response) throws IOException, ResponseException {

    // 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(host, basicAuth);/*ww  w  . j  a  v  a 2  s  . co  m*/

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    final HttpRequestBase method = new HttpGet(host.toURI() + path + rightside);
    method.addHeader("Accept", "application/json");

    try {
        HttpResponse httpResponse = httpClient.execute(method, localContext);
        int resultCode = httpResponse.getStatusLine().getStatusCode();
        if (resultCode != 200)
            throw new ResponseException(
                    "Unexpected result code " + resultCode + ": " + convertToString(httpResponse));
        Object jo = convertToJSON(httpResponse);
        response.acceptJSONObject(jo);
    } finally {
        method.abort();
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.jira.JiraSession.java

private void getRest(String rightside, JiraJSONResponse response) throws IOException, ResponseException {

    // 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(host, basicAuth);/*from   ww  w.j av a2 s  . c  om*/

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    final HttpRequestBase method = new HttpGet(host.toURI() + path + rightside);
    method.addHeader("Accept", "application/json");

    try {
        HttpResponse httpResponse = httpClient.execute(method, localContext);
        int resultCode = httpResponse.getStatusLine().getStatusCode();
        if (resultCode != 200)
            throw new IOException(
                    "Unexpected result code " + resultCode + ": " + convertToString(httpResponse));
        Object jo = convertToJSON(httpResponse);
        response.acceptJSONObject(jo);
    } finally {
        method.abort();
    }
}

From source file:SubmitResults.java

public boolean sendFile(Main parent, String hostname, String instanceFilePath, String status, String user,
        String password, boolean encrypted, String newIdent) {

    boolean submit_status = false;
    File tempFile = null;/*  w  w  w.java  2  s .  c o m*/

    // XSLT if ident needs to be changed
    final String changeIdXSLT = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
            + "<xsl:param name=\"surveyId\"/>" + "<xsl:template match=\"@*|node()\">" + "<xsl:copy>"
            + "<xsl:apply-templates select=\"@*|node()\"/>" + "</xsl:copy>" + "</xsl:template>"
            + "<xsl:template match=\"@id\">" + "<xsl:attribute name=\"id\">"
            + "<xsl:value-of select=\"$surveyId\"/>" + "</xsl:attribute>" + "</xsl:template>"
            + "</xsl:stylesheet>";

    //FileBody fb = null;
    ContentType ct = null;
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    String urlString = null;
    HttpHost targetHost = null;
    if (encrypted) {
        urlString = "https://" + hostname + "/submission";
        targetHost = new HttpHost(hostname, 443, "https");
        parent.appendToStatus("   Using https");
        //credsProvider.setCredentials(
        //        new AuthScope(hostname, 443, "smap", "digest"),
        //        new UsernamePasswordCredentials(user, password));
        credsProvider.setCredentials(new AuthScope(hostname, 443, "smap", "basic"),
                new UsernamePasswordCredentials(user, password));
    } else {
        urlString = "http://" + hostname + "/submission";
        targetHost = new HttpHost(hostname, 80, "http");
        parent.appendToStatus("   Using http (not encrypted)");
        credsProvider.setCredentials(new AuthScope(hostname, 80, "smap", "digest"),
                new UsernamePasswordCredentials(user, password));
    }

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

    // get instance file
    File instanceFile = new File(instanceFilePath);

    if (!instanceFile.exists()) {
        parent.appendToStatus("   Error: Submission file " + instanceFilePath + " does not exist");
    } else {

        HttpPost req = new HttpPost(URI.create(urlString));
        //req.setHeader("form_status", status);                  // smap add form_status header

        tempFile = populateRequest(parent, status, instanceFilePath, req, changeIdXSLT, ct, entityBuilder,
                newIdent);

        // find all files in parent directory
        /*
        File[] allFiles = instanceFile.getParentFile().listFiles();
                
        // add media files ignoring invisible files and the submission file
        List<File> files = new ArrayList<File>();
        for (File f : allFiles) {
           String fileName = f.getName();
           if (!fileName.startsWith(".") && !fileName.equals(instanceFile.getName())) {   // ignore invisible files and instance xml file    
         files.add(f);
           }
        }
        */

        // add the submission file first...

        /*
        ct = ContentType.create("text/xml");
         //fb = new FileBody(instanceFile, ct);
         entity.addBinaryBody("xml_submission_file", instanceFile, ct, instanceFile.getPath());
         //entity.addPart("xml_submission_file", fb);
        */

        /*
        for (int j = 0; j < files.size(); j++) {
                  
                
            File f = files.get(j);
            String fileName = f.getName();
            int idx = fileName.lastIndexOf(".");
            String extension = "";
            if (idx != -1) {
           extension = fileName.substring(idx + 1);
            }
                
            // we will be processing every one of these, so
            // we only need to deal with the content type determination...
            if (extension.equals("xml")) {
          ct = ContentType.create("text/xml");
            } else if (extension.equals("jpg")) {
          ct = ContentType.create("image/jpeg");
            } else if (extension.equals("3gp")) {
          ct = ContentType.create("video/3gp");
            } else if (extension.equals("3ga")) {
          ct = ContentType.create("audio/3ga");
            } else if (extension.equals("mp4")) {
          ct = ContentType.create("video/mp4");
            } else if (extension.equals("m4a")) {
            ct = ContentType.create("audio/m4a");
            }else if (extension.equals("csv")) {
          ct = ContentType.create("text/csv");
            } else if (f.getName().endsWith(".amr")) {
          ct = ContentType.create("audio/amr");
            } else if (extension.equals("xls")) {
          ct = ContentType.create("application/vnd.ms-excel");
            }  else {
          ct = ContentType.create("application/octet-stream");
          parent.appendToStatus("   Info: unrecognised content type for extension " + extension);
                  
            }
                
            //fb = new FileBody(f, ct);
            //entity.addPart(f.getName(), fb);
            entity.addBinaryBody(f.getName(), f, ct, f.getName());
                 
           parent.appendToStatus("   Info: added file " + f.getName());
                
        }
        */

        //req.setEntity(entity.build());

        // prepare response and return uploaded
        HttpResponse response = null;
        try {

            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();

            // Generate DIGEST scheme object, initialize it and add it to the local auth cache
            DigestScheme digestAuth = new DigestScheme();
            // Suppose we already know the realm name
            digestAuth.overrideParamter("realm", "smap");
            // Suppose we already know the expected nonce value
            digestAuth.overrideParamter("nonce", "whatever");
            authCache.put(targetHost, digestAuth);

            // Generate Basic scheme object
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(targetHost, basicAuth);

            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            parent.appendToStatus("   Info: submitting to: " + req.getURI().toString());
            response = httpclient.execute(targetHost, req, localContext);
            int responseCode = response.getStatusLine().getStatusCode();

            try {
                // have to read the stream in order to reuse the connection
                InputStream is = response.getEntity().getContent();
                // read to end of stream...
                final long count = 1024L;
                while (is.skip(count) == count)
                    ;
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            // verify that the response was a 201 or 202.
            // If it wasn't, the submission has failed.
            parent.appendToStatus("   Info: Response code: " + responseCode + " : "
                    + response.getStatusLine().getReasonPhrase());
            if (responseCode != HttpStatus.SC_CREATED && responseCode != HttpStatus.SC_ACCEPTED) {
                parent.appendToStatus("   Error: upload failed: ");
            } else {
                submit_status = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            parent.appendToStatus("   Error: Generic Exception. " + e.toString());
        }
    }

    try {
        httpclient.close();
    } catch (Exception e) {

    } finally {

    }

    if (tempFile != null) {
        tempFile.delete();
    }

    return submit_status;
}

From source file:com.ngdata.hbaseindexer.indexer.FusionPipelineClient.java

protected FusionSession establishSession(String url, String user, String password, String realm)
        throws Exception {

    FusionSession fusionSession = new FusionSession();

    if (!isKerberos && realm != null) {
        int at = url.indexOf("/api");
        String proxyUrl = url.substring(0, at);
        String sessionApi = proxyUrl + "/api/session?realmName=" + realm;
        String jsonString = "{\"username\":\"" + user + "\", \"password\":\"" + password + "\"}"; // TODO: ugly!

        URL sessionApiUrl = new URL(sessionApi);
        String sessionHost = sessionApiUrl.getHost();

        try {/*from  ww w . j  a  v  a  2s. co  m*/
            clearCookieForHost(sessionHost);
        } catch (Exception exc) {
            log.warn("Failed to clear session cookie for " + sessionHost + " due to: " + exc);
        }

        HttpPost postRequest = new HttpPost(sessionApiUrl.toURI());
        postRequest.setEntity(
                new StringEntity(jsonString, ContentType.create("application/json", StandardCharsets.UTF_8)));

        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);

        HttpResponse response = httpClient.execute(postRequest, context);
        HttpEntity entity = response.getEntity();
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200 && statusCode != 201 && statusCode != 204) {
                String body = extractResponseBodyText(entity);
                throw new SolrException(SolrException.ErrorCode.getErrorCode(statusCode),
                        "POST credentials to Fusion Session API [" + sessionApi + "] failed due to: "
                                + response.getStatusLine() + ": " + body);
            } else if (statusCode == 401) {
                // retry in case this is an expired error
                String body = extractResponseBodyText(entity);
                if (body != null && body.indexOf("session-idle-timeout") != -1) {
                    EntityUtils.consume(entity); // have to consume the previous entity before re-trying the request

                    log.warn(
                            "Received session-idle-timeout error from Fusion Session API, re-trying to establish a new session to "
                                    + url);
                    try {
                        clearCookieForHost(sessionHost);
                    } catch (Exception exc) {
                        log.warn("Failed to clear session cookie for " + sessionHost + " due to: " + exc);
                    }

                    response = httpClient.execute(postRequest, context);
                    entity = response.getEntity();
                    statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode != 200 && statusCode != 201 && statusCode != 204) {
                        body = extractResponseBodyText(entity);
                        throw new SolrException(SolrException.ErrorCode.getErrorCode(statusCode),
                                "POST credentials to Fusion Session API [" + sessionApi + "] failed due to: "
                                        + response.getStatusLine() + ": " + body);
                    }
                }
            }
        } finally {
            if (entity != null)
                EntityUtils.consume(entity);
        }
        log.info("Established secure session with Fusion Session API on " + url + " for user " + user
                + " in realm " + realm);
    }

    fusionSession.sessionEstablishedAt = System.nanoTime();

    URL fusionUrl = new URL(url);
    String hostAndPort = fusionUrl.getHost() + ":" + fusionUrl.getPort();
    fusionSession.docsSentMeter = getMeterByHost("Docs Sent to Fusion", hostAndPort);

    return fusionSession;
}

From source file:sachin.spider.WebSpider.java

private void processURL(WebURL curUrl) {
    curUrl.setProccessed(true);/*  w  w  w  . j a va  2 s .  c o m*/
    String url = curUrl.getUrl();
    HttpGet httpget = new HttpGet(url);
    RequestConfig requestConfig = getRequestConfigWithRedirectDisabled();
    httpget.setConfig(requestConfig);
    try {
        HttpClientContext context = HttpClientContext.create();
        long startingTime = System.currentTimeMillis();
        try (CloseableHttpResponse response = httpclient.execute(httpget, context)) {
            long endingTime = System.currentTimeMillis();
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            curUrl.setStatusCode(statusCode);
            curUrl.setStatusMessage(EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.ENGLISH));
            curUrl.setResposneTime(((int) (endingTime - startingTime)) / 1000);
            curUrl.setHeaders(response.getAllHeaders());
            curUrl.setBaseHref(context.getTargetHost().toString());
            if (curUrl.getStatusCode() >= 300 && curUrl.getStatusCode() < 400) {
                handleRedirectedLink(curUrl);
            } else if (statusCode == 200) {
                try {
                    processPage(response, curUrl);
                } catch (Exception ex) {
                    Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            handleLink(curUrl, response, statusCode,
                    EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.ENGLISH));
            EntityUtils.consumeQuietly(response.getEntity());
            HttpClientUtils.closeQuietly(response);
        }
    } catch (Exception ex) {
        System.out.println(curUrl.getUrl());
        curUrl.setErrorMsg(ex.toString());
        Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        httpget.releaseConnection();
    }
}

From source file:com.cloud.utils.rest.RESTServiceConnectorTest.java

@Test
public void testExecuteDeleteObject() throws Exception {
    final HttpEntity entity = mock(HttpEntity.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getEntity()).thenReturn(entity);
    when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE);
    final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
    when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)))
            .thenReturn(response);//w  ww .j a v a  2 s . c  o m
    final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost");
    final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build();

    connector.executeDeleteObject("/somepath");

    verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("DELETE"),
            any(HttpClientContext.class));
    verify(response).close();
}

From source file:me.Aron.Heinecke.fbot.lib.Socket.java

/***
 * Downloads a specified file via http/*from  w  ww.  j av a2 s. c  om*/
 * @param url request url
 * @param savFile path to safe the file at
 * @throws ClientProtocolException, IOException, SSLPeerUnverifiedException, FileNotFoundException 
 */
public synchronized String downloadFile(String url, String savFile)
        throws ClientProtocolException, IOException, SSLPeerUnverifiedException, ClientProtocolException,
        IOException, SSLPeerUnverifiedException, FileNotFoundException {
    //      https://fronter.com/giessen/links/link.phtml?idesc=1&iid=12841;

    //Create context and set custom cookies store
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(fbot.getDB().getCookieStore());

    HttpGet request = new HttpGet(url);

    // add request header
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.addHeader("Accept-Encoding", "binary");
    request.addHeader("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
    request.addHeader("Connection", "keep-alive");
    request.addHeader("DNT", "1");
    request.addHeader("Host", "fronter.com");
    request.addHeader("User-Agent", UA);
    //execute request with local context (session-cookie)
    HttpResponse response = client.execute(request, context);

    if (fbot.isDebug()) {
        fbot.getLogger().debug("socket", "Sending POST request to URL: " + url);
        fbot.getLogger().debug("socket", "Response code: " + response.getStatusLine().getStatusCode());
        fbot.getLogger().log("debug", "socket", context.getCookieStore().getCookies());
    }

    //save response to file
    HttpEntity entity = response.getEntity();
    if (entity != null && response.getStatusLine().getStatusCode() == 200) {
        InputStream inputStream = entity.getContent();

        OutputStream os = new FileOutputStream(savFile);

        byte[] buffer = new byte[1024];
        int bytesRead;
        //read from is to buffer
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        //flush OutputStream to write any buffered data to file
        os.flush();
        os.close();
        inputStream.close();

        return savFile;
    } else {
        fbot.getLogger().severe("socket",
                "File download, server response: " + response.getStatusLine().getStatusCode());
        request.abort();
        request.releaseConnection();
        throw new FileNotFoundException("Not found!");
    }
}

From source file:org.callimachusproject.client.HttpClientRedirectTest.java

@Test
public void testAbsoluteRequestURIWithFragment() throws Exception {
    register("*", new SimpleService());
    final HttpHost target = getServerHttp();

    final URI uri = new URIBuilder().setHost(target.getHostName()).setPort(target.getPort())
            .setScheme(target.getSchemeName()).setPath("/stuff").setFragment("blahblah").build();

    final HttpGet httpget = new HttpGet(uri);
    final HttpClientContext context = HttpClientContext.create();

    final HttpResponse response = this.httpclient.execute(httpget, context);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());

    final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    Assert.assertEquals("/stuff", request.getRequestLine().getUri());

    final URI location = getHttpLocation(httpget, context);
    Assert.assertEquals(uri, location);/* w  ww. j a v  a2 s.  c o m*/
}