List of usage examples for org.apache.http.protocol BasicHttpContext setAttribute
public void setAttribute(String str, Object obj)
From source file:de.javakaffee.web.msm.integration.TestUtils.java
private static HttpResponse executeRequestWithAuth(final DefaultHttpClient client, final HttpUriRequest method, final Credentials credentials) throws IOException, ClientProtocolException { client.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); final BasicHttpContext localcontext = new BasicHttpContext(); // Generate BASIC scheme object and stick it to the local // execution context final BasicScheme basicAuth = new BasicScheme(); localcontext.setAttribute("preemptive-auth", basicAuth); // System.out.println( "cookies: " + method.getParams().getCookiePolicy() ); //method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); return client.execute(method, localcontext); }
From source file:projekat.rest_client.HttpComponentsClientHttpRequestFactoryBasicAuth.java
private HttpContext createHttpContext() { // 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);/* www. j a va 2s . c o m*/ // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); return localcontext; }
From source file:com.nestorledon.employeedirectory.http.HttpComponentsClientHttpRequestFactoryBasicAuth.java
private HttpContext createHttpContext(HttpHost host) { // 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 www .j a va 2 s. co m*/ // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); return localcontext; }
From source file:org.springframework.cloud.dataflow.shell.command.support.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java
private HttpContext createHttpContext() { final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);/*from w ww .j a va 2s .c o m*/ final BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); return localcontext; }
From source file:org.springframework.cloud.dataflow.rest.util.PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory.java
@Override protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);/*from w w w .j a va 2s .c om*/ final BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); return localcontext; }
From source file:org.syncope.client.http.PreemptiveAuthHttpRequestFactory.java
@Override protected HttpContext createHttpContext(final HttpMethod httpMethod, final URI uri) { 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); return localcontext; }
From source file:com.github.restdriver.clientdriver.integration.BasicAuthTest.java
@Test public void basicAuthWorks() throws Exception { clientDriver.addExpectation(onRequestTo("/").withBasicAuth("Aladdin", "open sesame"), giveEmptyResponse().withStatus(418)).anyTimes(); DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials(new AuthScope("localhost", AuthScope.ANY_PORT), new UsernamePasswordCredentials("Aladdin", "open sesame")); HttpHost host = new HttpHost("localhost", 12345); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth);/*from w ww .ja va 2 s . c om*/ BasicHttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.AUTH_CACHE, authCache); List<String> authPrefs = new ArrayList<String>(); authPrefs.add(AuthPolicy.BASIC); client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPrefs); HttpGet get = new HttpGet(clientDriver.getBaseUrl() + "/"); HttpResponse response = client.execute(host, get, context); assertThat(response.getStatusLine().getStatusCode(), is(418)); }
From source file:com.mycompany.projecta.Test.java
public void Auth() throws Exception { // Credentials String username = "fernandoadp"; String password = "ADP.2017"; // Jenkins url String jenkinsUrl = "http://localhost:8080"; // Build name String jobName = "ServiceA"; // Build token String buildToken = "build"; // Create your httpclient DefaultHttpClient client = new DefaultHttpClient(); // Then provide the right credentials ******************* client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new org.apache.http.auth.UsernamePasswordCredentials(username, password)); //client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), (Credentials) new UsernamePasswordCredentials(username, password)); // Generate BASIC scheme object and stick it to the execution context BasicScheme basicAuth = new BasicScheme(); BasicHttpContext context = new BasicHttpContext(); context.setAttribute("preemptive-auth", basicAuth); // Add as the first (because of the zero) request interceptor // It will first intercept the request and preemptively initialize the authentication scheme if there is not client.addRequestInterceptor(new PreemptiveAuth(), 0); // You get request that will start the build String getUrl = jenkinsUrl + "/job/" + jobName + "/build?token=" + buildToken; HttpGet get = new HttpGet(getUrl); try {// w w w. j a v a 2 s. c om // Execute your request with the given context HttpResponse response = client.execute(get, context); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.etk.common.net.ETKHttpClient.java
public HttpEntity execute(String targetURL) throws ClientProtocolException, IOException { HttpHost targetHost = new HttpHost("127.0.0.1", 8080, "http"); httpClient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("demo", "gtn")); // 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); HttpGet httpget = new HttpGet(targetURL); Header header = new BasicHeader("Content-Type", "application/json"); httpget.setHeader(header);//from ww w. j a va2 s .c om HttpResponse response = httpClient.execute(targetHost, httpget, localcontext); DumpHttpResponse.dumpHeader(response); HttpEntity entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); } EntityUtils.consume(entity); return entity; }
From source file:de.tsystems.mms.apm.performancesignature.viewer.rest.model.CustomJenkinsHttpClient.java
public CustomJenkinsHttpClient(final URI uri, final String username, final String password, final boolean verifyCertificate, final CustomProxy customProxy) { super(uri, addAuthentication(createHttpClientBuilder(verifyCertificate, customProxy), uri, username, password));/*w w w. ja v a 2 s . co m*/ if (StringUtils.isNotBlank(username)) { BasicHttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute("preemptive-auth", new BasicScheme()); super.setLocalContext(httpContext); } }