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());
}
}
}
}
Aggregations