List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:kuona.client.JenkinsHttpClient.java
/** * Create an authenticated Jenkins HTTP client * * @param uri Location of the jenkins server (ex. http://localhost:8080) * @param username Username to use when connecting * @param password Password or auth token to use when connecting *//* w ww . j a va2 s .c o m*/ public JenkinsHttpClient(Project project, URI uri, String username, String password) { this(project, uri); if (isNotBlank(username)) { CredentialsProvider provider = client.getCredentialsProvider(); AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), "realm"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); provider.setCredentials(scope, credentials); localContext = new BasicHttpContext(); localContext.setAttribute("preemptive-auth", new BasicScheme()); client.addRequestInterceptor(new PreemptiveAuth(), 0); } }
From source file:org.oscarehr.fax.core.FaxStatusUpdater.java
public void updateStatus() { List<FaxJob> faxJobList = faxJobDao.getInprogressFaxesByJobId(); FaxConfig faxConfig;//from ww w. j a v a2 s . com DefaultHttpClient client = new DefaultHttpClient(); FaxJob faxJobUpdated; log.info("CHECKING STATUS OF " + faxJobList.size() + " FAXES"); for (FaxJob faxJob : faxJobList) { faxConfig = faxConfigDao.getConfigByNumber(faxJob.getFax_line()); if (faxConfig == null) { log.error("Could not find faxConfig. Has the fax number changed?"); } else if (faxConfig.isActive()) { client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(faxConfig.getSiteUser(), faxConfig.getPasswd())); HttpGet mGet = new HttpGet(faxConfig.getUrl() + "/" + faxJob.getJobId()); mGet.setHeader("accept", "application/json"); mGet.setHeader("user", faxConfig.getFaxUser()); mGet.setHeader("passwd", faxConfig.getFaxPasswd()); try { HttpResponse response = client.execute(mGet); log.info("RESPONSE: " + response.getStatusLine().getStatusCode()); mGet.releaseConnection(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = response.getEntity(); String content = EntityUtils.toString(httpEntity); ObjectMapper mapper = new ObjectMapper(); faxJobUpdated = mapper.readValue(content, FaxJob.class); //faxJobUpdated = (FaxJob) JSONObject.toBean(jsonObject, FaxJob.class); faxJob.setStatus(faxJobUpdated.getStatus()); faxJob.setStatusString(faxJobUpdated.getStatusString()); log.info("UPDATED FAX JOB ID " + faxJob.getJobId() + " WITH STATUS " + faxJob.getStatus()); faxJobDao.merge(faxJob); } else { log.error("WEB SERVICE RESPONDED WITH " + response.getStatusLine().getStatusCode(), new IOException()); } } catch (ClientProtocolException e) { log.error("HTTP WS CLIENT ERROR", e); } catch (IOException e) { log.error("IO ERROR", e); } } } }
From source file:io.n7.calendar.caldav.BaseTestCase.java
public HttpClient createHttpClient() { HttpClient http = new HttpClient(); Credentials credentials = new UsernamePasswordCredentials(caldavCredential.user, caldavCredential.password); http.getState().setCredentials(new AuthScope(this.getCalDAVServerHost(), this.getCalDAVServerPort()), credentials);//from w w w . java 2s . c o m http.getParams().setAuthenticationPreemptive(true); return http; }
From source file:org.springframework.xd.shell.security.SecuredShellAccessWithSslTest.java
@Test public void testSpringXDTemplate() throws Exception { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "whosThere")); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider) .setSSLSocketFactory(new SSLConnectionSocketFactory(new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build())) .build());/*from w ww . j a va 2 s . co m*/ SpringXDTemplate template = new SpringXDTemplate(requestFactory, new URI("https://localhost:" + adminPort)); PagedResources<ModuleDefinitionResource> moduleDefinitions = template.moduleOperations() .list(RESTModuleType.sink); assertThat(moduleDefinitions.getLinks().size(), greaterThan(0)); }
From source file:com.microsoft.azure.hdinsight.spark.jobs.SparkRestUtil.java
public static HttpEntity getEntity(@NotNull IClusterDetail clusterDetail, @NotNull String restUrl) throws HDIException, IOException { provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(clusterDetail.getHttpUserName(), clusterDetail.getHttpPassword())); HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(provider).build(); String url = String.format(SPARK_REST_API_ENDPOINT, clusterDetail.getName(), restUrl); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); int code = response.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK || code == HttpStatus.SC_CREATED) { return response.getEntity(); } else {// www. ja v a2 s . co m throw new HDIException(response.getStatusLine().getReasonPhrase(), response.getStatusLine().getStatusCode()); } }
From source file:com.redwoodsystems.android.apps.utils.HttpUtil.java
public static void setBasicAuthCredentials(HttpClient client, String userName, String password) { //BASIC Authentication List<String> authPrefs = new ArrayList<String>(2); authPrefs.add(AuthPolicy.BASIC);//w w w . ja va2s. c o m client.getParams().setParameter("http.auth.scheme-pref", authPrefs); ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(org.apache.http.auth.AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); }
From source file:org.cvasilak.jboss.mobile.admin.net.TalkToJBossServerTask.java
public TalkToJBossServerTask(Context context, Server server, Callback callback) { this.context = context; this.client = CustomHTTPClient.getHttpClient(); this.server = server; this.callback = callback; this.gjson = ((JBossAdminApplication) context.getApplicationContext()).getJSONParser(); this.parser = new JsonParser(); // enable digest authentication if (server.getUsername() != null && !server.getUsername().equals("")) { Credentials credentials = new UsernamePasswordCredentials(server.getUsername(), server.getPassword()); client.getCredentialsProvider().setCredentials( new AuthScope(server.getHostname(), server.getPort(), AuthScope.ANY_REALM), credentials); }/* w w w.j ava 2 s . co m*/ }
From source file:org.opendaylight.defense4all.odl.controller.Connector.java
public void init() throws ExceptionControlApp { try {//from ww w. j av a 2 s. c om restPrefix = "http://" + odlOFC.ipAddrString + ":" + Integer.toString(odlOFC.port); // set authentication for rest template AuthScope authScope = new AuthScope(odlOFC.hostname, odlOFC.port, AuthScope.ANY_REALM); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(odlOFC.username, odlOFC.password); // DefaultHttpClient client = new DefaultHttpClient(); // client.getCredentialsProvider().setCredentials(authScope, credentials); // HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client); // restTemplate = new RestTemplate(factory); RestTemplateFactory.INSTANCE.setInsecureSsl(true); restTemplate = RestTemplateFactory.INSTANCE.createRestTemplate(authScope, credentials); if (restTemplate == null) throw new Exception("Failed to create restTemplate"); } catch (Throwable e) { log.error("Failed to init connector to " + odlOFC.hostname, e); FMHolder.get().getHealthTracker().reportHealthIssue(HealthTracker.MINOR_HEALTH_ISSUE); throw new ExceptionControlApp("Failed to init connector to " + odlOFC.hostname, e); } }
From source file:org.eobjects.datacleaner.monitor.pentaho.PentahoCarteClient.java
private HttpClient createHttpClient(PentahoJobType pentahoJobType) { final String hostname = pentahoJobType.getCarteHostname(); final Integer port = pentahoJobType.getCartePort(); final String username = pentahoJobType.getCarteUsername(); final String password = pentahoJobType.getCartePassword(); final DefaultHttpClient httpClient = new DefaultHttpClient(); final CredentialsProvider credentialsProvider = httpClient.getCredentialsProvider(); final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); final List<String> authpref = new ArrayList<String>(); authpref.add(AuthPolicy.BASIC);/*from w w w . j av a2s. co m*/ authpref.add(AuthPolicy.DIGEST); httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); credentialsProvider.setCredentials(new AuthScope(hostname, port), credentials); return httpClient; }
From source file:org.cvasilak.jboss.mobile.app.net.TalkToJBossServerTask.java
public TalkToJBossServerTask(Context context, Server server, Callback callback, boolean shouldProcessRequest) { this.context = context; this.server = server; this.callback = callback; this.shouldProcessRequest = shouldProcessRequest; this.client = CustomHTTPClient.getHttpClient(); this.jsonBuilder = ((JBossAdminApplication) context.getApplicationContext()).getJSONBuilder(); this.jsonParser = ((JBossAdminApplication) context.getApplicationContext()).getJSONParser(); Credentials credentials = new UsernamePasswordCredentials(server.getUsername(), server.getPassword()); client.getCredentialsProvider().setCredentials( new AuthScope(server.getHostname(), server.getPort(), AuthScope.ANY_REALM), credentials); }