Search in sources :

Example 1 with ServerErrorRetryStrategy

use of org.apache.sling.testing.clients.util.ServerErrorRetryStrategy in project sling-org-apache-sling-testing-clients by apache.

the class FormBasedAuthInterceptor method doLogin.

private void doLogin(HttpRequest request, HttpContext context) throws IOException {
    // get host
    final HttpHost host = HttpClientContext.adapt(context).getTargetHost();
    // get the username and password from the credentials provider
    final CredentialsProvider credsProvider = HttpClientContext.adapt(context).getCredentialsProvider();
    final AuthScope scope = new AuthScope(host.getHostName(), host.getPort());
    final String username = credsProvider.getCredentials(scope).getUserPrincipal().getName();
    final String password = credsProvider.getCredentials(scope).getPassword();
    List<NameValuePair> parameters = new LinkedList<>();
    parameters.add(new BasicNameValuePair("j_username", username));
    parameters.add(new BasicNameValuePair("j_password", password));
    HttpEntity httpEntity = new UrlEncodedFormEntity(parameters, "utf-8");
    URI loginURI = URI.create(request.getRequestLine().getUri()).resolve(loginPath);
    HttpPost loginPost = new HttpPost(loginURI);
    loginPost.setEntity(httpEntity);
    try (CloseableHttpClient client = HttpClientBuilder.create().setServiceUnavailableRetryStrategy(new ServerErrorRetryStrategy()).disableRedirectHandling().build()) {
        try (CloseableHttpResponse response = client.execute(host, loginPost, context)) {
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() >= 400) {
                LOG.error("Got error login response code {} from '{}'", sl.getStatusCode(), loginURI.toString());
                LOG.error("Dumping headers: ");
                for (Header header : response.getAllHeaders()) {
                    LOG.error("\t '{}' = '{}'", header.getName(), header.getValue());
                }
                try (InputStream inputStream = response.getEntity().getContent()) {
                    String responseText = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
                    LOG.error("Error response body was : '{}'", responseText);
                }
            } else if (getLoginCookie(context, loginTokenName) == null) {
                LOG.error("Login response {} from '{}' did not include cookie '{}'.", sl.getStatusCode(), loginURI.toString(), loginTokenName);
            } else {
                LOG.debug("Login response {} from '{}'", sl.getStatusCode(), loginURI.toString());
            }
        }
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) ServerErrorRetryStrategy(org.apache.sling.testing.clients.util.ServerErrorRetryStrategy) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CredentialsProvider(org.apache.http.client.CredentialsProvider) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) LinkedList(java.util.LinkedList) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 LinkedList (java.util.LinkedList)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpHost (org.apache.http.HttpHost)1 NameValuePair (org.apache.http.NameValuePair)1 StatusLine (org.apache.http.StatusLine)1 AuthScope (org.apache.http.auth.AuthScope)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 ServerErrorRetryStrategy (org.apache.sling.testing.clients.util.ServerErrorRetryStrategy)1