List of usage examples for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider
public synchronized final CredentialsProvider getCredentialsProvider()
From source file:org.dasein.security.joyent.AuthClientFactory.java
@Override public @Nonnull HttpClient getClient(String endpoint) throws CloudException, InternalException { if (endpoint == null) { throw new CloudException("No cloud endpoint was defined"); }//w ww . j a v a 2s.c o m boolean ssl = endpoint.startsWith("https"); int targetPort; URI uri; try { uri = new URI(endpoint); targetPort = uri.getPort(); if (targetPort < 1) { targetPort = (ssl ? 443 : 80); } } catch (URISyntaxException e) { throw new CloudException(e); } HttpHost targetHost = new HttpHost(uri.getHost(), targetPort, uri.getScheme()); DefaultHttpClient client = (DefaultHttpClient) super.getClient(endpoint); try { String userName = new String(getProviderContext().getAccessPublic(), "utf-8"); String password = new String(getProviderContext().getAccessPrivate(), "utf-8"); client.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(userName, password)); } catch (UnsupportedEncodingException e) { throw new InternalException(e); } return client; }
From source file:com.openmeap.http.HttpRequestExecuterImpl.java
protected void setProxy(DefaultHttpClient httpclient, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword) {//from w w w . j a v a 2 s .co m if (proxyUser != null) { httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); } HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); }
From source file:br.cefetrj.sagitarii.teapot.comm.WebClient.java
public String doGet(String action, String parameter) throws Exception { String resposta = "SEM_RESPOSTA"; String mhpHost = gf.getHostURL(); String url = mhpHost + "/" + action + "?" + parameter; DefaultHttpClient httpClient = new DefaultHttpClient(); if (gf.useProxy()) { if (gf.getProxyInfo() != null) { HttpHost httpproxy = new HttpHost(gf.getProxyInfo().getHost(), gf.getProxyInfo().getPort()); httpClient.getCredentialsProvider().setCredentials( new AuthScope(gf.getProxyInfo().getHost(), gf.getProxyInfo().getPort()), new UsernamePasswordCredentials(gf.getProxyInfo().getUser(), gf.getProxyInfo().getPassword())); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpproxy); }//from ww w.ja va 2 s . co m } httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); HttpGet getRequest = new HttpGet(url); getRequest.addHeader("accept", "application/json"); getRequest.addHeader("Content-Type", "plain/text; charset=utf-8"); HttpResponse response = httpClient.execute(getRequest); response.setHeader("Content-Type", "plain/text; charset=UTF-8"); int stCode = response.getStatusLine().getStatusCode(); if (stCode != 200) { System.out.println("Sagitarii nao recebeu meu anuncio. Codigo de erro " + stCode); System.out.println(url); } else { HttpEntity entity = response.getEntity(); InputStreamReader isr = new InputStreamReader(entity.getContent(), "UTF-8"); resposta = convertStreamToString(isr); Charset.forName("UTF-8").encode(resposta); httpClient.getConnectionManager().shutdown(); isr.close(); } return resposta; }
From source file:es.ujaen.dae.restClient.controllers.SessionBackingBean.java
public String login() { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user.getName(), user.getPassword())); ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient); template = new RestTemplate(rf); try {//from w ww. j a va 2 s . c om template.getForObject(URL + "users/loginTest", String.class); loggedIn = true; return "userMenu?faces-redirect=true"; } catch (HttpClientErrorException e) { loggedIn = false; user.setPassword(""); return "loginError?faces-redirect=true"; } }
From source file:org.oscarehr.fax.core.FaxImporter.java
public void poll() { List<FaxConfig> faxConfigList = faxConfigDao.findAll(null, null); DefaultHttpClient client = new DefaultHttpClient(); for (FaxConfig faxConfig : faxConfigList) { if (faxConfig.isActive()) { client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(faxConfig.getSiteUser(), faxConfig.getPasswd())); HttpGet mGet = new HttpGet(faxConfig.getUrl() + PATH + "/" + faxConfig.getFaxUser()); 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()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = response.getEntity(); String content = EntityUtils.toString(httpEntity); log.info("CONTENT: " + content); ObjectMapper mapper = new ObjectMapper(); List<FaxJob> faxList = mapper.readValue(content, new TypeReference<List<FaxJob>>() { });//from w w w. j ava 2s . co m FaxJob faxFile; for (FaxJob receivedFax : faxList) { if ((faxFile = downloadFax(client, faxConfig, receivedFax)) != null) { if (saveAndInsertIntoQueue(faxConfig, receivedFax, faxFile)) { deleteFax(client, faxConfig, receivedFax); } } } } mGet.releaseConnection(); } catch (ClientProtocolException e) { log.error("HTTP WS CLIENT ERROR", e); } catch (IOException e) { log.error("IO ERROR", e); } catch (Exception e) { log.error("UNKNOWN ERROR ", e); } finally { mGet.releaseConnection(); } } } }
From source file:com.liferay.portal.search.solr.http.BasicAuthPoolingHttpClientFactory.java
@Override protected void configure(DefaultHttpClient defaultHttpClient) { if (Validator.isBlank(_username)) { return;/*w w w . j a v a 2 s .co m*/ } if (_authScope == null) { _authScope = AuthScope.ANY; } if (Validator.isNull(_password)) { _password = StringPool.BLANK; } CredentialsProvider credentialsProvider = defaultHttpClient.getCredentialsProvider(); credentialsProvider.setCredentials(_authScope, new UsernamePasswordCredentials(_username, _password)); }
From source file:com.sap.core.odata.testutil.tool.core.AbstractTestClient.java
private DefaultHttpClient createHttpClient(final CallerConfig config) { DefaultHttpClient httpClient = new DefaultHttpClient(); if (config.isProxySet()) { initProxy(httpClient, config);/* w w w .ja v a2 s . com*/ } if (config.isBasicAuthCredentialsSet()) { final URI baseUri = config.getBaseUri(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()), new UsernamePasswordCredentials(config.getBasicAuthCredentials())); } return httpClient; }
From source file:com.alu.e3.gateway.loadbalancer.E3HttpClientConfigurer.java
/** * Sets global PROXY for HttpClient/*from w w w . j a v a2 s. c o m*/ * @param client HttpClient * @param params of the PROXY */ private void setForwardProxy(HttpClient client, HttpParams params) { if (forwardProxy != null) { if (client instanceof DefaultHttpClient) { String host = forwardProxy.getProxyHost(); Integer port = new Integer(forwardProxy.getProxyPort()); if (host != null && port != null) { HttpHost httpHost = new HttpHost(host, port); ConnRouteParamBean connRouteParamBean = new ConnRouteParamBean(params); connRouteParamBean.setDefaultProxy(httpHost); String user = forwardProxy.getProxyUser(); String pass = forwardProxy.getProxyPass(); if (user != null && pass != null) { Credentials credentials = null; String ntdomain = null; //targetHost.getForwardProxy().getProxyNtDomain; String ntworkstation = null; //targetHost.getForwardProxy().getProxyNtWorkstation(); if (ntdomain != null || ntworkstation != null) { credentials = new NTCredentials(user, pass, ntworkstation, ntdomain); } else { credentials = new UsernamePasswordCredentials(user, pass); } AuthScope authscope = new AuthScope(host, port); DefaultHttpClient defaultHttpClient = (DefaultHttpClient) client; defaultHttpClient.getCredentialsProvider().setCredentials(authscope, credentials); } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug("The proxy is set with no user or password information"); } } } else { LOGGER.error("Unable to set proxy settings: Host or Port are NULL"); } } else { LOGGER.error("Unable to set proxy settings: Unsupported HttpClient implementation"); } } }