use of org.apache.http.auth.UsernamePasswordCredentials in project wildfly by wildfly.
the class TransportGuaranteeTestCase method checkGetURL.
/**
* Check response on given url
*
* @param url
* @param responseSubstring - if null we are checking response code only
* @return
* @throws Exception
*/
private boolean checkGetURL(String url, String responseSubstring, String user, String pass) throws Exception {
log.trace("Checking URL=" + url);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, pass));
CloseableHttpClient httpClient;
if (url.startsWith("https")) {
httpClient = TestHttpClientUtils.getHttpsClient(credentialsProvider);
} else {
httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
}
HttpGet get = new HttpGet(url);
HttpResponse hr;
try {
try {
hr = httpClient.execute(get);
} catch (Exception e) {
if (// in case substring is defined, rethrow exception so, we can easier analyze the cause
responseSubstring == null) // in case substring is defined, rethrow exception so, we can easier analyze the cause
{
return false;
} else {
throw new Exception(e);
}
}
int statusCode = hr.getStatusLine().getStatusCode();
if (statusCode != 200) {
log.trace("statusCode not expected. statusCode=" + statusCode + ", URL=" + url);
return false;
}
if (responseSubstring == null) {
// this indicates that negative test had problems
log.trace("statusCode==200 on URL=" + url);
return true;
}
String response = EntityUtils.toString(hr.getEntity());
if (response.indexOf(responseSubstring) != -1) {
return true;
} else {
log.trace("Response doesn't contain expected substring (" + responseSubstring + ")");
return false;
}
} finally {
if (httpClient != null) {
httpClient.close();
}
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project wildfly by wildfly.
the class WebSecurityBASICTestCase method makeCall.
@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
if (entity != null) {
log.trace("Response content length: " + entity.getContentLength());
}
assertEquals(expectedStatusCode, statusLine.getStatusCode());
EntityUtils.consume(entity);
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project undertow by undertow-io.
the class HttpTunnelingViaConnectTestCase method testConnectViaProxy.
@Test
public void testConnectViaProxy() throws Exception {
final HttpHost proxy = new HttpHost(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default") + 1, "http");
final HttpHost target = new HttpHost(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default"), "http");
ProxyClient proxyClient = new ProxyClient();
Socket socket = proxyClient.tunnel(proxy, target, new UsernamePasswordCredentials("a", "b"));
try {
Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
out.write("GET / HTTP/1.1\r\n");
out.write("Host: " + target.toHostString() + "\r\n");
out.write("Connection: close\r\n");
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
String line = null;
boolean found = false;
while ((line = in.readLine()) != null) {
System.out.println(line);
if (line.equals("MyHeader: MyValue")) {
found = true;
}
}
Assert.assertTrue(found);
} finally {
socket.close();
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project ThinkAndroid by white-cat.
the class AsyncHttpClient method setBasicAuth.
/**
* Sets basic authentication for the request. You should pass in your
* AuthScope for security. It should be like this
* setBasicAuth("username","password", new
* AuthScope("host",port,AuthScope.ANY_REALM))
*
* @param username
* @param password
* @param scope
* - an AuthScope object
*
*/
public void setBasicAuth(String user, String pass, AuthScope scope) {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);
this.httpClient.getCredentialsProvider().setCredentials(scope, credentials);
}
use of org.apache.http.auth.UsernamePasswordCredentials in project intellij-community by JetBrains.
the class GithubConnectionBuilder method setupCredentialsProvider.
@NotNull
private CredentialsProvider setupCredentialsProvider(@NotNull HttpClientBuilder builder) {
CredentialsProvider provider = new BasicCredentialsProvider();
// Basic authentication
GithubAuthData.BasicAuth basicAuth = myAuth.getBasicAuth();
if (basicAuth != null) {
AuthScope authScope = getBasicAuthScope();
provider.setCredentials(authScope, new UsernamePasswordCredentials(basicAuth.getLogin(), basicAuth.getPassword()));
builder.addInterceptorFirst(new PreemptiveBasicAuthInterceptor(authScope));
}
builder.setDefaultCredentialsProvider(provider);
if (myAuth.isUseProxy()) {
IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(provider, myApiURL);
}
return provider;
}
Aggregations