use of org.apache.http.auth.UsernamePasswordCredentials in project samourai-wallet-android by Samourai-Wallet.
the class JSONRPC method doRPC.
private JSONObject doRPC(String id, String method, JSONArray params) {
DefaultHttpClient httpclient = new DefaultHttpClient();
JSONObject responseJsonObj = null;
try {
JSONObject json = new JSONObject();
json.put("id", id);
json.put("method", method);
if (null != params && params.length() > 0) {
json.put("params", params);
}
httpclient.getCredentialsProvider().setCredentials(new AuthScope(node, port), new UsernamePasswordCredentials(user, password.toString()));
StringEntity myEntity = new StringEntity(json.toString());
Log.d("JSONRPC", json.toString());
HttpPost httppost = new HttpPost("http://" + node + ":" + port);
httppost.setEntity(myEntity);
Log.d("JSONRPC", "Request:" + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Log.d("JSONRPC", response.getStatusLine().toString());
// Log.d("JSONRPC", entity == null ? "entity is null" : "entity is not null, content length:" + entity.getContentLength());
if (response.getStatusLine().getStatusCode() != 200) {
return null;
}
String inputLine = null;
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
try {
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (sb.length() > 0) {
responseJsonObj = new JSONObject(sb.toString());
} else {
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
return responseJsonObj;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project keycloak by keycloak.
the class ProxyMappingsAwareRoutePlanner method determineProxy.
@Override
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
String targetHostName = target.getHostName();
ProxyMapping proxyMapping = proxyMappings.getProxyFor(targetHostName);
LOG.debugf("Returning proxyMapping=%s for targetHost=%s", proxyMapping, targetHostName);
UsernamePasswordCredentials proxyCredentials = proxyMapping.getProxyCredentials();
HttpHost proxyHost = proxyMapping.getProxyHost();
if (proxyCredentials != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), proxyCredentials);
context.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider);
}
return proxyHost;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project NoraUi by NoraUi.
the class MailSteps method createRestTemplate.
private static RestTemplate createRestTemplate() {
String proxyUser = "";
String proxyPassword = "";
String proxyUrl = "";
int proxyPort = 0;
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (!"".equals(proxyUrl)) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
HttpHost myProxy = new HttpHost(proxyUrl, proxyPort);
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
}
HttpClient httpClient = clientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
return restTemplate;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project archiva by apache.
the class RepositoryModelResolver method connectToRepository.
/**
* Using wagon, connect to the remote repository.
*
* @param wagon the wagon instance to establish the connection on.
* @return true if the connection was successful. false if not connected.
*/
private boolean connectToRepository(Wagon wagon, RemoteRepository remoteRepository) {
boolean connected;
final NetworkProxy proxyConnector = this.networkProxyMap.get(remoteRepository.getId());
ProxyInfo networkProxy = null;
if (proxyConnector != null) {
networkProxy = new ProxyInfo();
networkProxy.setType(proxyConnector.getProtocol());
networkProxy.setHost(proxyConnector.getHost());
networkProxy.setPort(proxyConnector.getPort());
networkProxy.setUserName(proxyConnector.getUsername());
networkProxy.setPassword(proxyConnector.getPassword());
String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getLocation();
if (networkProxy.getNonProxyHosts() != null) {
msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
}
if (StringUtils.isNotBlank(networkProxy.getUserName())) {
msg += "; as user: " + networkProxy.getUserName();
}
log.debug(msg);
}
AuthenticationInfo authInfo = null;
RepositoryCredentials creds = remoteRepository.getLoginCredentials();
String username = "";
String password = "";
if (creds instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds;
username = c.getUserName();
password = c.getPassword();
}
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getLocation());
authInfo = new AuthenticationInfo();
authInfo.setUserName(username);
authInfo.setPassword(password);
}
int timeoutInMilliseconds = ((int) remoteRepository.getTimeout().getSeconds()) * 1000;
// FIXME olamy having 2 config values
// Set timeout
wagon.setReadTimeout(timeoutInMilliseconds);
wagon.setTimeout(timeoutInMilliseconds);
try {
org.apache.maven.wagon.repository.Repository wagonRepository = new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
if (networkProxy != null) {
wagon.connect(wagonRepository, authInfo, networkProxy);
} else {
wagon.connect(wagonRepository, authInfo);
}
connected = true;
} catch (ConnectionException | AuthenticationException e) {
log.error("Could not connect to {}:{} ", remoteRepository.getName(), e.getMessage());
connected = false;
}
return connected;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project ANNIS by korpling.
the class Helper method createRESTClient.
/**
* Creates an authentificiated REST client
*
* @param userName
* @param password
* @return A newly created client.
*/
public static Client createRESTClient(String userName, String password) {
DefaultApacheHttpClient4Config rc = new DefaultApacheHttpClient4Config();
rc.getClasses().add(SaltProjectProvider.class);
ThreadSafeClientConnManager clientConnMgr = new ThreadSafeClientConnManager();
clientConnMgr.setDefaultMaxPerRoute(10);
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, clientConnMgr);
if (userName != null && password != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider);
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true);
}
Client c = ApacheHttpClient4.create(rc);
return c;
}
Aggregations