List of usage examples for org.apache.http.auth AuthScope ANY_HOST
String ANY_HOST
To view the source code for org.apache.http.auth AuthScope ANY_HOST.
Click Source Link
From source file:es.tsb.ltba.nomhad.example.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient = wrapClient(httpclient); httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("A100", "123456")); try {// w ww .j a va 2s . com HttpGet httpget = new HttpGet(NOMHAD_URL_HEADER + "A100" + OBSERVATIONS_REQUEST); HttpPost httppost = new HttpPost(NOMHAD_URL_HEADER + "A100" + OBSERVATIONS_REQUEST); httppost.setEntity(new StringEntity(BODY_TEST)); System.out.println("executing request " + httpget.getURI()); // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.emc.cto.ridagent.rid.test.TestScript.java
public static void main(String args[]) throws SAXException, ParserConfigurationException, URISyntaxException, ClientProtocolException, IOException { String xmlData = " <iodef-rid:RID lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:iodef-rid=\"urn:ietf:params:xml:ns:iodef-rid-2.0\" xmlns:iodef=\"urn:ietf:params:xml:ns:iodef-1.42\" xsi:schemaLocation=\"urn:ietf:params:xml:ns:iodef-rid-2.0 iodef-rid-2.0.xsd\">"; xmlData = xmlData + "<iodef-rid:RIDPolicy MsgType=\"Report\" MsgDestination=\"RIDSystem\">"; xmlData = xmlData + "<iodef-rid:PolicyRegion region=\"IntraConsortium\"/>"; xmlData = xmlData + " <iodef:Node>"; xmlData = xmlData + " <iodef:NodeName>192.168.1.1</iodef:NodeName>"; xmlData = xmlData + " </iodef:Node>"; xmlData = xmlData + "<iodef-rid:TrafficType type=\"Network\"/>"; xmlData = xmlData + "</iodef-rid:RIDPolicy>"; xmlData = xmlData + "</iodef-rid:RID>"; String id = TestScript.httpSend(xmlData, "https://ridtest.emc.com:4590/"); HttpGet httpget = new HttpGet("http://localhost:1280/federation/RID/" + id + "/report.xml"); DefaultHttpClient httpclient = new DefaultHttpClient(); Credentials defaultcreds = new UsernamePasswordCredentials("Administrator", "secret"); httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), defaultcreds);//from w ww. j a v a 2s . com httpget.setHeader("User-Agent", "EMC RID System"); HttpResponse response = httpclient.execute(httpget); if (response.getEntity() != null) { int code = response.getStatusLine().getStatusCode(); if (code == 404) { System.out.println("Error has occured! Document not found in the xDB"); } else if (code == 200) { System.out.println("Document Successfully saved in the database"); } else { System.out.println("Error could not be determined"); } } }
From source file:se.anyro.tagtider.utils.Http.java
public static DefaultHttpClient getClient() { if (client == null) { client = new DefaultHttpClient(); Credentials creds = new UsernamePasswordCredentials("tagtider", "codemocracy"); AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); client.getCredentialsProvider().setCredentials(scope, creds); }//from w w w .j ava2s .c o m return client; }
From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitManagementUtils.java
public static RestTemplate buildRestTemplate(String adminUri, String user, String password) { BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(user, password)); HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); // Set up pre-emptive basic Auth because the rabbit plugin doesn't currently support challenge/response for PUT // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local; from the apache docs... // auth cache BasicScheme basicAuth = new BasicScheme(); URI uri;//from ww w. j ava2 s. c om try { uri = new URI(adminUri); } catch (URISyntaxException e) { throw new RabbitAdminException("Invalid URI", e); } authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), basicAuth); // Add AuthCache to the execution context final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient) { @Override protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { return localContext; } }); restTemplate.setMessageConverters( Collections.<HttpMessageConverter<?>>singletonList(new MappingJackson2HttpMessageConverter())); return restTemplate; }
From source file:com.datatorrent.stram.util.WebServicesClientTest.java
public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException, IllegalAccessException { CredentialsProvider provider = getCredentialsProvider(); String httpScheme = AuthScope.ANY_SCHEME; if (authScheme == AuthScheme.BASIC) { httpScheme = AuthSchemes.BASIC;/*from ww w. jav a2s. c o m*/ } else if (authScheme == AuthScheme.DIGEST) { httpScheme = AuthSchemes.DIGEST; } AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme); Credentials credentials = provider.getCredentials(authScope); Assert.assertNotNull("Credentials", credentials); Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass())); UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials; Assert.assertEquals("Username", username, pwdCredentials.getUserName()); Assert.assertEquals("Password", password, pwdCredentials.getPassword()); }
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 va2s. co m // 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:de.cuseb.bilderbuch.helpers.HttpClientProviderService.java
public HttpClient getHttpClientWithBasicAuth(String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); }
From source file:org.wso2.developerstudio.appfactory.core.client.HttpsJenkinsClient.java
public static HttpResponse getBulildinfo(String applicationId, String version, String builderBaseUrl, int lastBuildNo) { DefaultHttpClient client = new DefaultHttpClient(); client = (DefaultHttpClient) HttpsJaggeryClient.wrapClient(client, builderBaseUrl); client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(Authenticator.getInstance().getCredentials().getUser(), Authenticator.getInstance().getCredentials().getPassword())); // 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); String getUrl = builderBaseUrl + "/job/" + applicationId + "-" + version + "-default/" + lastBuildNo + "/consoleText"; HttpGet get = new HttpGet(getUrl); try {/*www . j a v a2 s . c o m*/ // Execute your request with the given context HttpResponse response = client.execute(get, context); return response; } catch (Exception e) { log.error("Jenkins Client err", e); } return null; }
From source file:com.asakusafw.shafu.internal.core.net.ShafuCredentialsProvider.java
@Override public Credentials getCredentials(AuthScope authscope) { String host = authscope.getHost(); if (host != AuthScope.ANY_HOST) { Scope scope = new Scope(authscope.getScheme(), host, authscope.getPort(), authscope.getRealm()); for (IHttpCredentialsProvider provider : Activator.getExtensions().createHttpCredentialsProvider()) { try { IHttpCredentials creds = provider.find(scope); if (creds != null) { return new UsernamePasswordCredentials(creds.getUserName(), creds.getPassword()); }/*from w w w .j av a2 s .c o m*/ } catch (CoreException e) { LogUtil.log(e.getStatus()); } } } return super.getCredentials(authscope); }
From source file:net.evendanan.android.thumbremote.network.ReusableHttpClientBlocking.java
ReusableHttpClientBlocking(int timeout, String user, String password) { Log.d(TAG, "Creating a new HTTP client"); mHttpClient = new DefaultHttpClient(); if (!TextUtils.isEmpty(password)) { CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(user, password)); mHttpClient.setCredentialsProvider(credProvider); }/*w w w .j a v a 2 s .c om*/ mRequest = new HttpGet(); mRequest.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout)); mRequest.getParams().setParameter(CoreConnectionPNames.SO_LINGER, new Integer(timeout / 2)); mRequest.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout)); }