Search in sources :

Example 6 with BasicScheme

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);
    }
}
Also used : WMSHeaderParam(org.geosdi.geoplatform.services.request.WMSHeaderParam) BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 7 with BasicScheme

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);
}
Also used : BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme)

Example 8 with BasicScheme

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()));
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 9 with BasicScheme

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);
    }
}
Also used : BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) FileNotFoundException(java.io.FileNotFoundException) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

BasicScheme (org.apache.hc.client5.http.impl.auth.BasicScheme)9 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)8 HttpHost (org.apache.hc.core5.http.HttpHost)8 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)7 IOException (java.io.IOException)6 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)6 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)4 HttpEntity (org.apache.hc.core5.http.HttpEntity)3 FileNotFoundException (java.io.FileNotFoundException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 AuthCache (org.apache.hc.client5.http.auth.AuthCache)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)2 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)2 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 InputStream (java.io.InputStream)1 TRUE (java.lang.Boolean.TRUE)1