List of usage examples for org.apache.http.client CredentialsProvider setCredentials
void setCredentials(AuthScope authscope, Credentials credentials);
From source file:org.codice.alliance.nsili.endpoint.requests.FtpDestinationSink.java
@Override public void writeFile(InputStream fileData, long size, String name, String contentType, List<Metacard> metacards) throws IOException { CloseableHttpClient httpClient = null; String urlPath = protocol + "://" + fileLocation.host_name + ":" + port + "/" + fileLocation.path_name + "/" + name;/* www . j av a 2 s . co m*/ LOGGER.debug("Writing ordered file to URL: {}", urlPath); try { HttpPut putMethod = new HttpPut(urlPath); putMethod.addHeader(HTTP.CONTENT_TYPE, contentType); HttpEntity httpEntity = new InputStreamEntity(fileData, size); putMethod.setEntity(httpEntity); if (StringUtils.isNotEmpty(fileLocation.user_name) && fileLocation.password != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(fileLocation.host_name, port), new UsernamePasswordCredentials(fileLocation.user_name, fileLocation.password)); httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); } else { httpClient = HttpClients.createDefault(); } httpClient.execute(putMethod); fileData.close(); putMethod.releaseConnection(); } finally { if (httpClient != null) { httpClient.close(); } } }
From source file:org.jboss.as.test.integration.web.security.jaspi.WebSecurityJaspiTestCase.java
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); try (CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credentialsProvider).build()) { HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); if (entity != null) { log.trace("Response content length: " + entity.getContentLength()); }/*w ww .j av a 2 s . c o m*/ assertEquals(expectedStatusCode, statusLine.getStatusCode()); EntityUtils.consume(entity); } }
From source file:org.wildfly.test.integration.microprofile.health.MicroProfileHealthSecuredHTTPEndpointTestCase.java
@Test public void securedHTTPEndpoint() throws Exception { final String healthURL = "http://" + managementClient.getMgmtAddress() + ":" + managementClient.getMgmtPort() + "/health"; try (CloseableHttpClient client = HttpClients.createDefault()) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("testSuite", "testSuitePassword")); HttpClientContext hcContext = HttpClientContext.create(); hcContext.setCredentialsProvider(credentialsProvider); CloseableHttpResponse resp = client.execute(new HttpGet(healthURL), hcContext); assertEquals(200, resp.getStatusLine().getStatusCode()); String content = MicroProfileHealthHTTPEndpointTestCase.getContent(resp); resp.close();/*from w w w. j a v a 2s. c om*/ assertTrue("'UP' message is expected", content.contains("UP")); } }
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 w w w . ja va 2 s.c o m 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)); }
From source file:net.networksaremadeofstring.rhybudd.ZenossAPICore.java
@Override public boolean Login(ZenossCredentials credentials) throws Exception { if (credentials.URL.contains("https://")) { this.PrepareSSLHTTPClient(); } else {//from ww w . j ava 2s.c o m this.PrepareHTTPClient(); //httpclient = new DefaultHttpClient(); } if (!credentials.BAUser.equals("") || !credentials.BAPassword.equals("")) { //Log.i("Auth","We have some auth credentials"); CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(credentials.BAUser, credentials.BAPassword)); httpclient.setCredentialsProvider(credProvider); } HttpPost httpost = new HttpPost(credentials.URL + "/zport/acl_users/cookieAuthHelper/login"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("__ac_name", credentials.UserName)); nvps.add(new BasicNameValuePair("__ac_password", credentials.Password)); nvps.add(new BasicNameValuePair("submitted", "true")); nvps.add(new BasicNameValuePair("came_from", credentials.URL + "/zport/dmd")); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // Response from POST not needed, just the cookie HttpResponse response = httpclient.execute(httpost); // Consume so we can reuse httpclient response.getEntity().consumeContent(); //Set the variables for later this.ZENOSS_INSTANCE = credentials.URL; this.ZENOSS_USERNAME = credentials.UserName; this.ZENOSS_PASSWORD = credentials.Password; Log.e("CheckLoggedIn", Integer.toString(response.getStatusLine().getStatusCode())); reqCount++; return this.CheckLoggedIn(); }
From source file:org.datagator.api.client.backend.DataGatorService.java
public DataGatorService(UsernamePasswordCredentials auth) { this();/*from ww w. j a v a2 s .co m*/ log.info(String.format("Initializing service with authorization: %s", auth.getUserName())); // attach credentials to context HttpHost host = new HttpHost(environ.DATAGATOR_API_HOST, environ.DATAGATOR_API_PORT, environ.DATAGATOR_API_SCHEME); AuthScope scope = new AuthScope(host.getHostName(), host.getPort()); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(scope, auth); this.context.setCredentialsProvider(provider); // enable preemptive (pro-active) basic authentication AuthCache cache = new BasicAuthCache(); cache.put(host, new BasicScheme()); this.context.setAuthCache(cache); }
From source file:org.jboss.as.test.integration.web.security.authentication.BasicAuthenticationMechanismPicketboxRemovedTestCase.java
/** * Test checks if correct response is returned after the EJB is called from the secured servlet. * * @param url/*w w w . j a v a 2s . co m*/ * @throws Exception */ @Test public void test(@ArquillianResource URL url) throws Exception { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(USER, PASSWORD)); try (CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credentialsProvider).build()) { HttpGet httpget = new HttpGet(url.toExternalForm() + "SecuredEJBServlet/"); HttpResponse response = httpclient.execute(httpget); assertNotNull("Response is 'null', we expected non-null response!", response); String text = Utils.getContent(response); assertEquals(200, response.getStatusLine().getStatusCode()); assertTrue("User principal different from what we expected!", text.contains("Principal: " + USER)); assertTrue("Remote user different from what we expected!", text.contains("Remote User: " + USER)); assertTrue("Authentication type different from what we expected!", text.contains("Authentication Type: BASIC")); } }
From source file:com.garethahealy.resteastpathparamescape.utils.RestFactory.java
protected HttpClientContext getBasicAuthContext(URI uri, String userName, String password) { HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(userName, password)); // Create AuthCache instance // Generate BASIC scheme object and add it to the local auth cache AuthCache authCache = new BasicAuthCache(); authCache.put(targetHost, new BasicScheme()); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache);//from ww w. j av a 2 s. c om return context; }
From source file:org.wildfly.test.integration.elytron.http.PasswordMechTestBase.java
@Test public void testInsufficientRole() throws Exception { HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role2")); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .build()) {/*from w ww .j av a 2s . c o m*/ try (CloseableHttpResponse response = httpClient.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); assertEquals("Unexpected status code in HTTP response.", SC_FORBIDDEN, statusCode); } } }
From source file:org.wildfly.test.integration.elytron.http.PasswordMechTestBase.java
@Test public void testInvalidPrincipal() throws Exception { HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1")); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1wrong", "password1"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .build()) {//from w w w.j ava2s . c o m try (CloseableHttpResponse response = httpClient.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); assertEquals("Unexpected status code in HTTP response.", SC_UNAUTHORIZED, statusCode); } } }