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:cn.edu.pku.lib.dataverse.doi.DataCiteRESTfullClient.java

public DataCiteRESTfullClient(String url, String username, String password) {
    this.url = url;
    try {//  www. j  a  va 2 s .c  om
        context = HttpClientContext.create();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(null, -1),
                new UsernamePasswordCredentials(username, password));
        context.setCredentialsProvider(credsProvider);

        httpClient = HttpClients.createDefault();
    } catch (Exception ioe) {
        close();
        logger.log(Level.SEVERE, "Fail to init Client", ioe);
        throw new RuntimeException("Fail to init Client", ioe);
    }
}

From source file:httputils.RavelloHttpClient.java

public RavelloHttpClient(String username, String password) {
    this.client = HttpClients.createDefault();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username + ":" + password));
    this.context = HttpClientContext.create();
    this.context.setCredentialsProvider(credentialsProvider);
}

From source file:org.openscore.content.httpclient.build.ContextBuilder.java

public HttpClientContext build() {
    if (StringUtils.isEmpty(preemptiveAuth)) {
        preemptiveAuth = "true";
    }/*from   w  w w.j  a v  a 2s.co m*/
    HttpClientContext context = HttpClientContext.create();
    if (authTypes.size() == 1 && Boolean.parseBoolean(preemptiveAuth)) {
        AuthCache authCache = new BasicAuthCache();
        authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()),
                authSchemeLookup.lookup(authTypes.iterator().next()).create(context));
        context.setCredentialsProvider(credentialsProvider);
        context.setAuthCache(authCache);
    }
    return context;
}

From source file:io.cloudslang.content.httpclient.build.ContextBuilder.java

public HttpClientContext build() {
    if (StringUtils.isEmpty(preemptiveAuth)) {
        preemptiveAuth = "true";
    }/*  w ww.j  a  v a  2  s  .  c o  m*/
    HttpClientContext context = HttpClientContext.create();
    if (authTypes.size() == 1 && Boolean.parseBoolean(preemptiveAuth)
            && !authTypes.contains(AuthTypes.ANONYMOUS)) {
        AuthCache authCache = new BasicAuthCache();
        authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()),
                authSchemeLookup.lookup(authTypes.iterator().next()).create(context));
        context.setCredentialsProvider(credentialsProvider);
        context.setAuthCache(authCache);
    }
    return context;
}

From source file:at.bitfire.davdroid.webdav.DavRedirectStrategyTest.java

public void testMissingLocation() throws Exception {
    HttpContext context = HttpClientContext.create();
    HttpUriRequest request = new HttpOptions(TestConstants.roboHydra.resolve("redirect/without-location"));
    HttpResponse response = httpClient.execute(request, context);
    assertFalse(strategy.isRedirected(request, response, context));
}

From source file:org.jasig.portlet.proxy.service.web.HttpContentRequestImplTest.java

@Test
/**//from www  . java2  s . com
 * ensures duplicate() returns a unique object by duplicating an object,
 * changing the original, and then ensuring that the duplicate and the original
 * are not the same
 */
public void testDuplicate() {
    final String originalMethod = "post";
    final String newMethod = "get";

    final String originalProxiedLocation = "http://www.yahoo.com";
    final String newProxiedLocation = "http://www.espn.com";

    final String originalHeaderKey = "headerKey";
    final String originalHeaderValue = "headerValue";
    final String newHeaderValue = "newHeaderValue";

    final String originalParameterKey = "parameterA";
    final String originalParameterValue1 = "A";
    final String originalParameterValue2 = "B";
    final String newParameterValue1 = "99";

    final HttpClientContext originalHttpClientContext = HttpClientContext.create();
    final HttpClientContext newHttpClientContext = HttpClientContext.create();

    Map<String, IFormField> parameters = new LinkedHashMap<String, IFormField>();
    IFormField parameter = new FormFieldImpl();
    parameter.setName(originalParameterKey);
    String[] values = { originalParameterValue1, originalParameterValue2 };
    parameter.setValues(values);
    parameters.put(originalParameterKey, parameter);

    Map<String, String> headers = new LinkedHashMap<String, String>();
    headers.put(originalHeaderKey, originalHeaderValue);

    HttpContentRequestImpl original = new HttpContentRequestImpl();
    original.setForm(true);
    original.setProxiedLocation(originalProxiedLocation);
    original.setMethod(originalMethod);
    original.setHeaders(headers);
    original.setParameters(parameters);
    original.setHttpContext(originalHttpClientContext);

    HttpContentRequestImpl copy = original.duplicate();

    assertEquals(copy.isForm(), true);
    assertEquals(copy.getMethod(), originalMethod);
    assertEquals(copy.getProxiedLocation(), originalProxiedLocation);
    assertEquals(copy.getHeaders().get(originalHeaderKey), originalHeaderValue);
    assertEquals(copy.getHttpContext(), originalHttpClientContext);
    IFormField copyParameter = copy.getParameters().get(originalParameterKey);
    assertEquals(copyParameter.getValues()[0], originalParameterValue1);
    assertEquals(copyParameter.getValues()[1], originalParameterValue2);

    original.setForm(false);
    original.setProxiedLocation(newProxiedLocation);
    original.setMethod(newMethod);
    original.setHttpContext(newHttpClientContext);
    IFormField originalParameter = original.getParameters().get(originalParameterKey);
    originalParameter.getValues()[0] = newParameterValue1;
    original.getHeaders().put(originalHeaderKey, newHeaderValue);

    assertNotSame(original.isForm(), copy.isForm());
    assertNotSame(original.getMethod(), copy.getMethod());
    assertNotSame(original.getProxiedLocation(), copy.getProxiedLocation());
    assertNotSame(original.getHttpContext(), copy.getHttpContext());
    assertNotSame(original.getParameters().get(originalParameterKey),
            copy.getParameters().get(originalParameterKey));
    assertNotSame(original.getHeaders().get(originalHeaderKey), copy.getHeaders().get(originalHeaderKey));
}

From source file:it.polito.tellmefirst.classify.threads.GetWikiHtmlThread.java

/**
 * @param httpClient/*from ww  w. ja  va2  s .  c  o  m*/
 * @param httpget
 * @param result
 * @param i
 */
public GetWikiHtmlThread(CloseableHttpClient httpClient, HttpGet httpget, ArrayList<String[]> result, int i) {
    this.httpClient = httpClient;
    this.context = HttpClientContext.create();
    this.httpget = httpget;
    this.index = i;
    this.result = result;
}

From source file:io.kahu.hawaii.util.call.http.AbortableHttpRequest.java

public AbortableHttpRequest(RequestPrototype<HttpResponse, T> prototype, HttpRequestBase httpRequest) {
    super(prototype);
    this.httpRequest = httpRequest;
    this.httpClientContext = HttpClientContext.create();
}

From source file:org.openmrs.module.webservices.rest.ITBase.java

@BeforeClass
public static void waitForServerToStart() {
    synchronized (serverStartupLock) {
        if (!serverStarted) {
            final long time = System.currentTimeMillis();
            final int timeout = 300000;
            final int retryAfter = 10000;

            final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(retryAfter)
                    .setConnectTimeout(retryAfter).build();

            final String startupUri = TEST_URL.getScheme() + "://" + TEST_URL.getHost() + ":"
                    + TEST_URL.getPort() + TEST_URL.getPath();
            System.out.println(// w w  w. j a  v a  2s. c  om
                    "Waiting for server at " + startupUri + " for " + timeout / 1000 + " more seconds...");

            while (System.currentTimeMillis() - time < timeout) {
                try {
                    final HttpClient client = HttpClientBuilder.create().disableAutomaticRetries().build();
                    final HttpGet sessionGet = new HttpGet(startupUri);
                    sessionGet.setConfig(requestConfig);
                    final HttpClientContext context = HttpClientContext.create();
                    final HttpResponse response = client.execute(sessionGet, context);

                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 400) {
                        throw new RuntimeException(status + " " + response.getStatusLine().getReasonPhrase());
                    }

                    URI finalUri = sessionGet.getURI();
                    List<URI> redirectLocations = context.getRedirectLocations();
                    if (redirectLocations != null) {
                        finalUri = redirectLocations.get(redirectLocations.size() - 1);
                    }

                    String finalUriString = finalUri.toString();
                    if (!finalUriString.contains("initialsetup")) {
                        serverStarted = true;
                        return;
                    }
                } catch (IOException e) {
                    System.out.println(e.toString());
                }

                try {
                    System.out.println("Waiting for " + (timeout - (System.currentTimeMillis() - time)) / 1000
                            + " more seconds...");
                    Thread.sleep(retryAfter);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            throw new RuntimeException("Server startup took longer than 5 minutes!");
        }
    }
}

From source file:securitytools.nessus.NessusClient.java

public NessusClient(HttpHost targetHost, ClientConfiguration clientConfiguration) {
    this.target = targetHost;

    context = HttpClientContext.create();

    try {/*w  ww  .j a v a2  s  . c  om*/
        client = HttpClientFactory.build(clientConfiguration);
    } catch (NoSuchAlgorithmException nsae) {
        throw new NessusClientException(nsae.getMessage(), nsae);
    }
}