Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HttpClient to handle user info provided in URI #4664

Closed
valfirst opened this issue Dec 14, 2023 · 0 comments · Fixed by #5013
Closed

Refactor HttpClient to handle user info provided in URI #4664

valfirst opened this issue Dec 14, 2023 · 0 comments · Fixed by #5013
Assignees
Labels
task Technical debt
Milestone

Comments

@valfirst
Copy link
Collaborator

org.vividus.http.client.HttpClient#doHttpGet(java.net.URI) and org.vividus.http.client.HttpClient#doHttpHead(java.net.URI) should handle user info in the fully automated way:

  • the methods should convert user info provided as a part of URI to basic auth configuration;
  • these methods should be overloaded to accept parameter usePreemptiveBasicAuthIfAvailable;
  • the default behavior should be non-preemptive basic auth;
  • other methods accepting ClassicHttpRequest and/or HttpContext should be kept untouched;
  • Javadocs for updated and created methods should be created.

Once it's done, one more refactoring should be performed: add a new field to org.vividus.http.client.HttpResponse - org.apache.hc.core5.http.protocol.HttpContext used for HTTP request execution.

The following parts of the codebase should be refactored after the implementation:

  1. URI pageUriToCheck = pageUrl;
    UserInfo userInfo = UriUtils.getUserInfo(pageUriToCheck);
    ContextBuilder contextBuilder = ContextBuilder.create();
    if (userInfo != null)
    {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo.user(),
    userInfo.password().toCharArray());
    HttpHost host = HttpHost.create(pageUriToCheck);
    contextBuilder = contextBuilder.preemptiveBasicAuth(host, credentials);
    pageUriToCheck = UriUtils.removeUserInfo(pageUriToCheck);
    }
    HttpClientContext context = contextBuilder.build();
    httpClient.doHttpHead(pageUriToCheck, context);
  2. ContextBuilder contextBuilder = ContextBuilder.create();
    UriUtils.UserInfo userInfo = UriUtils.getUserInfo(siteMapUrl);
    URI targetSiteMapUrl;
    if (userInfo != null)
    {
    targetSiteMapUrl = UriUtils.removeUserInfo(siteMapUrl);
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    AuthScope authScope = new AuthScope(HttpHost.create(targetSiteMapUrl));
    credentialsProvider.setCredentials(authScope,
    new UsernamePasswordCredentials(userInfo.user(), userInfo.password().toCharArray()));
    contextBuilder.useCredentialsProvider(credentialsProvider);
    }
    else
    {
    targetSiteMapUrl = siteMapUrl;
    }
    HttpClientContext context = contextBuilder.build();
    HttpResponse response = httpClient.doHttpGet(targetSiteMapUrl, context);
@valfirst valfirst added the task Technical debt label Dec 14, 2023
@valfirst valfirst self-assigned this Apr 29, 2024
@valfirst valfirst added this to the 0.6.10 milestone Apr 29, 2024
@valfirst valfirst linked a pull request Apr 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task Technical debt
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant