use of org.apache.hc.client5.http.impl.auth.BasicScheme in project geo-platform by geosdi.
the class GeoSDIHttpClient5 method get.
/**
* Executes an HTTP GET request against the provided URL and returns the server response.
*
* <p>If an HTTP authentication {@link #getUser() user} and {@link #getPassword() password} is
* set, the appropriate authentication HTTP header will be sent with the request.
*
* <p>If a {@link #getConnectTimeout() connection timeout} is set, the http connection will be
* set to respect that timeout.
*
* <p>If a {@link #getReadTimeout() read timeout} is set, the http connection will be set to
* respect it.
*
* @param url the URL to retrieve
* @return an {@link HTTPResponse} encapsulating the response to the HTTP GET request
*/
@Override
public HTTPResponse get(URL url) throws IOException {
logger.info("Inject OpenAM Cookie Method [GET]");
HttpGet httpGet = new HttpGet(url.toExternalForm());
if (this.tryGzip) {
httpGet.setHeader("Accept-Encoding", "gzip");
}
logger.info("HEADERS : " + this.headers);
if ((this.headers != null) && !(this.headers.isEmpty())) {
for (WMSHeaderParam wmsHeaderParam : this.headers) {
httpGet.setHeader(wmsHeaderParam.getHeaderKey(), wmsHeaderParam.getHeaderValue());
}
}
CloseableHttpResponse response = null;
if (((this.user != null) && !(this.user.trim().isEmpty())) && ((this.password != null) && !(this.password.trim().isEmpty()))) {
logger.trace("############################Using BasicAuth with user : {} - password : {}\n", this.user, this.password);
try {
URI uri = url.toURI();
HttpClientContext localContext = create();
HttpHost targetHost = new HttpHost(uri.getScheme(), uri.getHost(), this.retrieveNoSetPort(uri));
BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(new UsernamePasswordCredentials(this.user, this.password.toCharArray()));
localContext.resetAuthExchange(targetHost, basicAuth);
response = this.httpClient.execute(targetHost, httpGet, localContext);
} catch (URISyntaxException ex) {
throw new IOException("URISyntaxException error : " + ex.getMessage() + " for URL " + url.toExternalForm());
}
} else {
response = this.httpClient.execute(httpGet);
}
int responseCode = response.getCode();
if (200 != responseCode) {
response.close();
throw new IOException("Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
} else {
return new GeoSDIHttpClient5.HttpMethodResponse(response);
}
}
use of org.apache.hc.client5.http.impl.auth.BasicScheme in project geo-platform by geosdi.
the class BasicPreemptiveSecurityConnector method bindCredentials.
/**
* @param targetHost
* @param targetURI
* @throws Exception
*/
@Override
protected void bindCredentials(@Nonnull(when = NEVER) HttpHost targetHost, @Nonnull(when = NEVER) URI targetURI) throws Exception {
super.bindCredentials(targetHost, targetURI);
BasicScheme basicAuth = this.createScheme();
basicAuth.initPreemptive(this.usernamePasswordCredentials);
localContext.resetAuthExchange(targetHost, basicAuth);
}
use of org.apache.hc.client5.http.impl.auth.BasicScheme in project mercury by yellow013.
the class ClientPreemptiveBasicAuthentication method main.
public static void main(final String[] args) throws Exception {
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
// Generate Basic scheme object and add it to the local auth cache
final BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(new UsernamePasswordCredentials("user", "passwd".toCharArray()));
final HttpHost target = new HttpHost("http", "httpbin.org", 80);
// Add AuthCache to the execution context
final HttpClientContext localContext = HttpClientContext.create();
localContext.resetAuthExchange(target, basicAuth);
final HttpGet httpget = new HttpGet("http://httpbin.org/hidden-basic-auth/user/passwd");
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
for (int i = 0; i < 3; i++) {
try (final CloseableHttpResponse response = httpclient.execute(httpget, localContext)) {
System.out.println("----------------------------------------");
System.out.println(response.getCode() + " " + response.getReasonPhrase());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
}
use of org.apache.hc.client5.http.impl.auth.BasicScheme in project alfresco-process-services-project-sdk by OpenPj.
the class IntegrationTestUtils method executePrivateGETRequest.
public static void executePrivateGETRequest(String username, String password, String protocol, String hostname, int port, String endpoint) {
try {
System.out.println("Start executing private GET request: " + endpoint);
final BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(new UsernamePasswordCredentials(username, password.toCharArray()));
final HttpHost target = new HttpHost(protocol, hostname, port);
final HttpClientContext localContext = HttpClientContext.create();
localContext.resetAuthExchange(target, basicAuth);
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
final HttpGet httpGet = new HttpGet(endpoint);
final HttpEntity reqEntity = MultipartEntityBuilder.create().build();
httpGet.setEntity(reqEntity);
System.out.println("Get URL: " + httpGet);
try (final CloseableHttpResponse response = httpclient.execute(httpGet, localContext)) {
System.out.println("----------------------------------------");
System.out.println(response);
final HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
}
}
System.out.println("End of executing private GET request: " + endpoint);
} catch (FileNotFoundException e) {
fail(e.getMessage(), e);
} catch (IOException e) {
fail(e.getMessage(), e);
}
}
Aggregations