List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:com.fibon.maven.confluence.ExportPageConfluenceMojo.java
private HttpGet prepareExportPageRequest(Format format, Long pageId) throws MojoFailureException { HttpGet get = new HttpGet(url + format.url + "?pageId=" + pageId); AuthenticationInfo info = wagonManager.getAuthenticationInfo(serverId); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(info.getUserName(), info.getPassword());//from w ww .j a v a 2 s . c o m BasicScheme scheme = new BasicScheme(); try { Header authorizationHeader = scheme.authenticate(credentials, get); get.addHeader(authorizationHeader); return get; } catch (AuthenticationException e) { throw fail("Unable to set authentication data", e); } }
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 v a 2s.c o 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:org.jasig.schedassist.impl.caldav.oracle.OracleCommsCredentialsProviderImpl.java
@Override public Credentials getCredentials(AuthScope authscope) { if (!getTargetAuthScope().equals(authscope)) { return null; }// w w w .j a v a 2 s .c o m StringBuilder username = new StringBuilder(getAdminCredentials().getUserPrincipal().getName()); username.append(SEMICOLON); username.append(accountToProxy.getEmailAddress()); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(username.toString(), getAdminCredentials().getPassword()); return proxyCredentials; }
From source file:org.bitcoin.client.BitcoinClient.java
/** * Creates a BitcoinClient/*from ww w . j a v a 2 s . c o m*/ * * @param host the host machine where there's an executing bitcoind server * @param login the username to access the bitcoind server * @param password the password to access the bitcoind server * @param port the port number to the bitcoind server */ public BitcoinClient(String host, String login, String password, int port) { try { Credentials credentials = new UsernamePasswordCredentials(login, password); URI uri = new URI("http", null, host, port, null, null, null); session = new HttpSession(uri, credentials); } catch (URISyntaxException e) { throw new BitcoinClientException("This host probably doesn't have correct syntax: " + host, e); } }
From source file:com.arduino2fa.xbee.ReceivePacket.java
private ReceivePacket() throws Exception { XBee xbee = new XBee(); int count = 0; int errors = 0; // Transmit stuff final int timeout = 5000; int ackErrors = 0; int ccaErrors = 0; int purgeErrors = 0; long now;//w ww . ja v a2 s . c o m // HTTP stuff CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpHost httpHost = new HttpHost("ec2-54-186-213-97.us-west-2.compute.amazonaws.com", 3000, "http"); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials("admin", "admin") // TODO get from command line ); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); BasicScheme basicScheme = new BasicScheme(); authCache.put(httpHost, basicScheme); // Add AuthCache to the execution context HttpClientContext httpClientContext = HttpClientContext.create(); httpClientContext.setCredentialsProvider(credentialsProvider); httpClientContext.setAuthCache(authCache); HttpGet httpGet = new HttpGet("/token-requests/1"); CloseableHttpResponse httpResponse; BufferedReader br; StringBuffer result; String line; try { // Connect to the XBee xbee.open(XbeeConfig.PORT, XbeeConfig.BAUD_RATE); now = System.currentTimeMillis(); // Loop indefinitely; sleeps for a few seconds at the end of every iteration while (true) { // Check if there are queued tx requests on the server httpResponse = httpClient.execute(httpHost, httpGet, httpClientContext); br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); result = new StringBuffer(); while ((line = br.readLine()) != null) { result.append(line); } // Check if the result is a JSON object if (result.charAt(0) != '{') { log.error("Result " + result.toString() + " is not a JSON object"); continue; } JSONObject object = (JSONObject) JSONValue.parse(result.toString()); if (object != null) { Long time = (Long) object.get("time"); // Check if the request is a new one (created after last check) if (time > last) { String token = (String) object.get("token"); byte[] tokenHex = SimpleCrypto.toByte(token); int[] payload = new int[] { tokenHex[0], tokenHex[1], tokenHex[2] }; XBeeAddress16 destination = new XBeeAddress16(0xFF, 0xFF); TxRequest16 tx = new TxRequest16(destination, payload); try { log.info("sending tx request with payload: " + token); XBeeResponse response = xbee.sendSynchronous(tx, timeout); if (response.getApiId() != ApiId.TX_STATUS_RESPONSE) { log.debug("expected tx status but received " + response); } else { log.debug("got tx status"); if (((TxStatusResponse) response).getFrameId() != tx.getFrameId()) { throw new RuntimeException("frame id does not match"); } if (((TxStatusResponse) response).getStatus() != TxStatusResponse.Status.SUCCESS) { errors++; if (((TxStatusResponse) response).isAckError()) { ackErrors++; } else if (((TxStatusResponse) response).isCcaError()) { ccaErrors++; } else if (((TxStatusResponse) response).isPurged()) { purgeErrors++; } log.debug("Tx status failure with status: " + ((TxStatusResponse) response).getStatus()); } else { // success log.debug("Success. count is " + count + ", errors is " + errors + ", in " + (System.currentTimeMillis() - now) + ", ack errors " + ackErrors + ", ccaErrors " + ccaErrors + ", purge errors " + purgeErrors); } count++; } } catch (XBeeTimeoutException e) { e.printStackTrace(); } } } last = System.currentTimeMillis(); httpGet.releaseConnection(); // Delay Thread.sleep(2000); } } finally { xbee.close(); } }
From source file:org.jboss.as.test.integration.management.http.XCorrelationIdTestCase.java
@Before public void before() throws Exception { this.url = new URL("http", managementClient.getMgmtAddress(), MGMT_PORT, MGMT_CTX); this.httpContext = new BasicHttpContext(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(Authentication.USERNAME, Authentication.PASSWORD)); this.httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); }
From source file:com.google.resting.rest.client.HttpContext.java
public HttpContext setCredentials(String user, String password) { this.credentials = new UsernamePasswordCredentials(user, password); return this; }
From source file:io.cloudslang.content.httpclient.build.auth.CredentialsProviderBuilder.java
public CredentialsProvider buildCredentialsProvider() { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (!StringUtils.isEmpty(username)) { Credentials credentials;/* ww w .j a v a 2s . c om*/ if (authTypes.contains(AuthTypes.NTLM)) { String[] domainAndUsername = getDomainUsername(username); credentials = new NTCredentials(domainAndUsername[1], password, host, domainAndUsername[0]); } else { credentials = new UsernamePasswordCredentials(username, password); } credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), credentials); } else if (authTypes.contains(AuthTypes.KERBEROS)) { credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), new Credentials() { @Override public Principal getUserPrincipal() { return null; } @Override public String getPassword() { return null; } }); } if (!StringUtils.isEmpty(proxyUsername)) { int intProxyPort = 8080; if (!StringUtils.isEmpty(proxyPort)) { intProxyPort = Utils.validatePortNumber(proxyPort); } credentialsProvider.setCredentials(new AuthScope(proxyHost, intProxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); } return credentialsProvider; }
From source file:org.apache.cloudstack.storage.datastore.util.NexentaNmsClient.java
protected DefaultHttpClient getClient() { if (httpClient == null) { if (nmsUrl.getSchema().equalsIgnoreCase("http")) { httpClient = getHttpClient(); } else {//from w w w . j a v a 2 s . co m httpClient = getHttpsClient(); } AuthScope authScope = new AuthScope(nmsUrl.getHost(), nmsUrl.getPort(), AuthScope.ANY_SCHEME, "basic"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(nmsUrl.getUsername(), nmsUrl.getPassword()); httpClient.getCredentialsProvider().setCredentials(authScope, credentials); } return httpClient; }
From source file:org.cvasilak.jboss.mobile.admin.net.UploadToJBossServerTask.java
public UploadToJBossServerTask(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); }//from w w w. j av a 2s. c o m }