List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:de.simu.decomap.component.polling.impl.helper.RestClient.java
/** * /*from www .j a v a2s . c om*/ * @param username * The username for the authentification * @param password * The password for the authentification */ public RestClient(String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password)); // HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); //setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); }
From source file:com.ibm.CloudResourceBundle.java
private static Rows getServerResponse(CloudDataConnection connect) throws Exception { Rows rows = null;/*from w w w .ja v a 2 s.c om*/ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("provide.castiron.com", 443), new UsernamePasswordCredentials(connect.getUserid(), connect.getPassword())); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try { // Call the service and get all the strings for all the languages HttpGet httpget = new HttpGet(connect.getURL()); httpget.addHeader("API_SECRET", connect.getSecret()); CloseableHttpResponse response = httpclient.execute(httpget); try { InputStream in = response.getEntity().getContent(); ObjectMapper mapper = new ObjectMapper(); rows = mapper.readValue(new InputStreamReader(in, "UTF-8"), Rows.class); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } return rows; }
From source file:org.apache.jena.jdbc.remote.connections.TestRemoteEndpointConnectionWithAuth.java
/** * Setup for the tests by allocating a Fuseki instance to work with * @throws IOException /*from ww w .j a v a 2 s.c o m*/ */ @BeforeClass public static void setup() throws IOException { SecurityHandler sh = FusekiTestAuth.makeSimpleSecurityHandler("/*", USER, PASSWORD); FusekiTestAuth.setupServer(true, sh); BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD)); client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build(); }
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 {/*from w w w .ja v a 2 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:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static String get(URL url, String accept, String user, String passwd) throws Exception { //System.out.println("GET "+url); HttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url.toURI()); if (accept != null) request.addHeader("Accept", accept); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); /*// 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 context.setCredentialsProvider(credsProvider); }/*from w ww. j ava2s . c om*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code != 200) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); Reader reader = null; try { reader = new InputStreamReader(response.getEntity().getContent()); StringBuilder sb = new StringBuilder(); { int read; char[] cbuf = new char[1024]; while ((read = reader.read(cbuf)) != -1) { sb.append(cbuf, 0, read); } } return sb.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.foxykeep.datadroidpoc.data.operation.AuthenticationOperation.java
@Override public Bundle execute(Context context, Request request) throws ConnectionException, DataException { NetworkConnection networkConnection = new NetworkConnection(context, WSConfig.WS_AUTHENTICATION_URL); if (request.getBoolean(PARAM_WITH_AUTHENTICATE)) { networkConnection.setCredentials(new UsernamePasswordCredentials(LOGIN, PASSWD)); }/* w w w .ja v a 2 s . com*/ ConnectionResult result = networkConnection.execute(); Bundle bundle = new Bundle(); bundle.putString(PoCRequestFactory.BUNDLE_EXTRA_AUTHENTICATION_RESULT, result.body); return bundle; }
From source file:com.mycompany.projecta.JenkinsScraper.java
public String scrape(String urlString, String username, String password) throws ClientProtocolException, IOException { URI uri = URI.create(urlString); HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password)); // 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 w ww. j av a 2 s .c om CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpGet httpGet = new HttpGet(uri); // Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); HttpResponse response = httpClient.execute(host, httpGet, localContext); String resp = response.toString(); return EntityUtils.toString(response.getEntity()); }
From source file:org.elasticsearch.xpack.watcher.common.http.auth.basic.ApplicableBasicAuth.java
@Override public void apply(CredentialsProvider credsProvider, AuthScope authScope) { credsProvider.setCredentials(authScope, new UsernamePasswordCredentials(auth.username, new String(auth.password.text(cryptoService)))); }
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); }//from www . j a va2s. 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)); }