Skip to content

Commit

Permalink
do not use httpclient Authenticator but add header Authorization so w…
Browse files Browse the repository at this point in the history
…e can get response status code rather than exception

Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy authored and markt-asf committed Oct 19, 2023
1 parent df7d2cf commit 68a60d7
Showing 1 changed file with 16 additions and 16 deletions.
Expand Up @@ -362,21 +362,6 @@ public HttpResponse execute() throws IOException {
url.append('?').append(query);
}

switch (_authType) {
case NO_AUTHENTICATION:
break;
case BASIC_AUTHENTICATION:
builder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
break;
case DIGEST_AUTHENTICATION:
throw new UnsupportedOperationException("Digest Authentication is not currently " + "supported");
}

HttpClient httpClient = null;

if(state) {
Expand Down Expand Up @@ -420,18 +405,33 @@ protected PasswordAuthentication getPasswordAuthentication() {
throw new RuntimeException("unknow method " + this.method);
}

switch (_authType) {
case NO_AUTHENTICATION:
break;
case BASIC_AUTHENTICATION:
httpRequestBuilder.header("Authorization", getBasicAuthenticationHeader(username, password));
break;
case DIGEST_AUTHENTICATION:
throw new UnsupportedOperationException("Digest Authentication is not currently " + "supported");
}

for(Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
entry.getValue().forEach(s -> httpRequestBuilder.header(entry.getKey(),s));
}

try {
java.net.http.HttpResponse<String> response = httpClient.send(httpRequestBuilder.build(), java.net.http.HttpResponse.BodyHandlers.ofString());
return new HttpResponse(host, port, _isSecure, this.method, response);
} catch (InterruptedException e) {
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}

private static String getBasicAuthenticationHeader(String username, String password) {
String valueToEncode = username + ":" + password;
return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes());
}

public String toString() {
StringBuilder sb = new StringBuilder(255);
sb.append("[REQUEST LINE] -> ").append(_requestLine).append('\n');
Expand Down

0 comments on commit 68a60d7

Please sign in to comment.