List of usage examples for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider
public synchronized final CredentialsProvider getCredentialsProvider()
From source file:com.alcatel_lucent.nz.wnmsextract.reader.GTAHttpReader.java
/** * Use the hardcoded URL/usr/pass to set up auth on httpclient * @param client http client class used throughout this connection instance *//* w ww .j a v a 2 s. co m*/ private void setCredentials(DefaultHttpClient client) { try { client.getCredentialsProvider().setCredentials(new AuthScope((new URL(GTA_URL)).getAuthority(), 80), new UsernamePasswordCredentials(GTA_USR, GTA_PWD)); } catch (MalformedURLException murle) { System.err.println("Something wrong with the suplied URL" + murle); } }
From source file:org.altchain.neo4j.bitcoind.BitcoinD.java
private JSONObject invokeRPC(String JSONRequestString) throws BitcoindNotRunningException { DefaultHttpClient httpclient = new DefaultHttpClient(); JSONObject responseJsonObj = null;/*from ww w. ja va2s .c o m*/ try { httpclient.getCredentialsProvider().setCredentials(new AuthScope(bitcoindHost, bitcoindPort), new UsernamePasswordCredentials("generated_by_armory", "6nkugwdEacEAgqjbCvvVyrgXcZj5Cxr38vTbZ513QJrf")); StringEntity myEntity = new StringEntity(JSONRequestString); logger.debug("JSON Request Object: " + JSONRequestString); HttpPost httppost = new HttpPost("http://" + this.bitcoindHost + ":" + this.bitcoindPort); httppost.setEntity(myEntity); logger.debug("executing request: " + httppost.getRequestLine()); HttpEntity entity = null; try { HttpResponse response = httpclient.execute(httppost); entity = response.getEntity(); logger.debug("HTTP response: " + response.getStatusLine()); } catch (Exception e) { logger.error("CANNOT CONNECT TO BITCOIND. IS BITCOIN RUNNING?"); throw new BitcoindNotRunningException(); } if (entity != null) { logger.debug("Response content length: " + entity.getContentLength()); } JSONParser parser = new JSONParser(); responseJsonObj = (JSONObject) parser.parse(EntityUtils.toString(entity)); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (org.json.simple.parser.ParseException e) { e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); } return responseJsonObj; }
From source file:org.opennms.core.test.JUnitHttpServerTest.java
@Test @JUnitHttpServer(port = 9162, basicAuth = true, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") }) public void testBasicAuthFailure() throws Exception { final DefaultHttpClient client = new DefaultHttpClient(); final HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/monkey"); final CredentialsProvider cp = client.getCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "sucks"); cp.setCredentials(new AuthScope("localhost", 9162), credentials); final HttpResponse response = client.execute(method); assertEquals(401, response.getStatusLine().getStatusCode()); }
From source file:read.taz.TazDownloader.java
private void downloadFile() throws ClientProtocolException, IOException { if (tazFile.file.exists()) { Log.w(getClass().getSimpleName(), "File " + tazFile + " exists."); return;/*w w w . ja v a 2 s . c o m*/ } HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 30000); HttpConnectionParams.setSoTimeout(httpParams, 15000); DefaultHttpClient client = new DefaultHttpClient(httpParams); Credentials defaultcreds = new UsernamePasswordCredentials(uname, passwd); client.getCredentialsProvider() .setCredentials(new AuthScope(/* FIXME DRY */"dl.taz.de", 80, AuthScope.ANY_REALM), defaultcreds); URI uri = tazFile.getDownloadURI(); Log.d(getClass().getSimpleName(), "Downloading taz from " + uri); HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException("Download failed with HTTP Error" + response.getStatusLine().getStatusCode()); } String contentType = response.getEntity().getContentType().getValue(); if (contentType.startsWith("text/html")) { Log.d(getClass().getSimpleName(), "Content type: " + contentType + " encountered. Assuming a non exisiting file"); ByteArrayOutputStream htmlOut = new ByteArrayOutputStream(); response.getEntity().writeTo(htmlOut); String resp = new String(htmlOut.toByteArray()); Log.d(getClass().getSimpleName(), "Response: " + resp); throw new FileNotFoundException("No taz found for date " + tazFile.date.getTime()); } else { FileOutputStream tazOut = new FileOutputStream(tazFile.file); try { response.getEntity().writeTo(tazOut); } finally { tazOut.close(); } } // InputStream docStream = response.getEntity().getContent(); // FileOutputStream out = null; // try { // out = tazOut; // byte[] buf = new byte[4096]; // for (int read = docStream.read(buf); read != -1; read = docStream // .read(buf)) { // out.write(buf, 0, read); // } // } finally { // docStream.close(); // if (out != null) { // out.close(); // } // } }
From source file:org.talend.librariesmanager.maven.ArtifactsDeployer.java
private void installToRemote(HttpEntity entity, URL targetURL) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); try {/* www .java 2 s .c o m*/ httpClient.getCredentialsProvider().setCredentials( new AuthScope(targetURL.getHost(), targetURL.getPort()), new UsernamePasswordCredentials(nexusServer.getUserName(), nexusServer.getPassword())); HttpPut httpPut = new HttpPut(targetURL.toString()); httpPut.setEntity(entity); HttpResponse response = httpClient.execute(httpPut); StatusLine statusLine = response.getStatusLine(); int responseCode = statusLine.getStatusCode(); EntityUtils.consume(entity); if (responseCode > 399) { if (responseCode == 500) { // ignor this error , if .pom already exist on server and deploy again will get this error } else if (responseCode == 401) { throw new BusinessException("Authrity failed"); } else { throw new BusinessException( "Deploy failed: " + responseCode + ' ' + statusLine.getReasonPhrase()); } } } catch (Exception e) { throw new Exception(targetURL.toString(), e); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:org.opennms.core.test.JUnitHttpServerTest.java
@Test @JUnitHttpServer(port = 9162, basicAuth = true, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") }) public void testBasicAuthSuccess() throws Exception { final DefaultHttpClient client = new DefaultHttpClient(); final HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/monkey"); final CredentialsProvider cp = client.getCredentialsProvider(); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "istrator"); cp.setCredentials(new AuthScope("localhost", 9162), credentials); final HttpResponse response = client.execute(method); final String responseString = EntityUtils.toString(response.getEntity()); LogUtils.debugf(this, "got response:\n%s", responseString); assertEquals(200, response.getStatusLine().getStatusCode()); assertTrue(responseString.contains("You are reading this from a servlet!")); }
From source file:org.picketbox.test.authentication.http.jetty.DelegatingSecurityFilterHTTPBasicUnitTestCase.java
@Test public void testBasicAuth() throws Exception { URL url = new URL(urlStr); DefaultHttpClient httpclient = null; try {/*w ww.ja v a 2s .co m*/ String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); HttpGet httpget = new HttpGet(url.toExternalForm()); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(200, statusLine.getStatusCode()); EntityUtils.consume(entity); } 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:org.picketbox.http.test.authentication.jetty.DelegatingSecurityFilterHTTPBasicUnitTestCase.java
@Test public void testBasicAuth() throws Exception { URL url = new URL(this.urlStr); DefaultHttpClient httpclient = null; try {// w w w . j a va 2 s .co m String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); HttpGet httpget = new HttpGet(url.toExternalForm()); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(200, statusLine.getStatusCode()); EntityUtils.consume(entity); } 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:org.opencastproject.remotetest.server.DigestAuthenticationTest.java
@Test public void testDigestAuthenticatedGet() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("matterhorn_system_account", "CHANGE_ME"); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet get = new HttpGet(BASE_URL + "/welcome.html"); get.addHeader("X-Requested-Auth", "Digest"); try {//from w w w. j av a2 s . c o m httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); HttpResponse response = httpclient.execute(get); String content = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); Assert.assertTrue(content.contains("Opencast Matterhorn")); } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:org.jboss.as.test.integration.security.loginmodules.IdentityLoginModuleTestCase.java
/** * Calls {@link PrincipalPrintingServlet} and checks if the returned principal name is the expected one. * /*from w ww .j a v a 2s. co m*/ * @param url * @param expectedPrincipal * @return Principal name returned from {@link PrincipalPrintingServlet} */ private String assertPrincipal(URL url, String expectedPrincipal) { DefaultHttpClient httpclient = new DefaultHttpClient(); Credentials creds = new UsernamePasswordCredentials("anyUsername"); httpclient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), creds); HttpGet httpget = new HttpGet(url.toExternalForm() + PrincipalPrintingServlet.SERVLET_PATH); String text; try { HttpResponse response = httpclient.execute(httpget); assertEquals("Unexpected status code", HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); text = EntityUtils.toString(response.getEntity()); } catch (IOException e) { throw new RuntimeException("Servlet response IO exception", e); } assertEquals("Unexpected principal name assigned by IdentityLoinModule", expectedPrincipal, text); return text; }