Skip to content

Commit

Permalink
AAA-5204 :: here-aaa-java-sdk does not support adding X-Correlation-I…
Browse files Browse the repository at this point in the history
…d as a header on the token request
  • Loading branch information
stwwalton committed Jun 27, 2019
1 parent b1d5a34 commit c97f46e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
75 changes: 60 additions & 15 deletions here-oauth-client/src/main/java/com/here/account/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
package com.here.account.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.logging.Logger;

import com.here.account.http.HttpConstants;
import com.here.account.http.HttpException;
import com.here.account.http.HttpProvider;
import com.here.account.http.HttpProvider.HttpRequest;
Expand All @@ -30,6 +23,12 @@
import com.here.account.util.CloseUtil;
import com.here.account.util.Serializer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.logging.Logger;

/**
* A Client that talks to a Resource Server, in OAuth2-speak.
* It is expected that a wrapper class invokes methods on an instance
Expand Down Expand Up @@ -177,15 +176,10 @@ public <R, T, U> T sendMessage(
clientAuthorizer, method, url, jsonBody);
}

if (null != additionalHeaders) {
for (Map.Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
String name = additionalHeader.getKey();
String value = additionalHeader.getValue();
httpRequest.addHeader(name, value);
}
}
// If there's additional headers, add them to the request
HttpRequest httpRequestWithAdditonalHeaders = addAdditionalHeaders(httpRequest, additionalHeaders);

return sendMessage(httpRequest, responseClass,
return sendMessage(httpRequestWithAdditonalHeaders, responseClass,
errorResponseClass, newExceptionFunction);
}

Expand Down Expand Up @@ -249,4 +243,55 @@ public <T, U> T sendMessage(HttpRequest httpRequest, Class<T> responseClass,
}
}

/**
* Sends the requested HTTP Message to the Server, with additional headers
* This method is useful if you have already constructed your
* HttpRequest, and your API supports
* Content-Type: application/json response documents.
*
* @param httpRequest the HTTP Request
* @param additionalHeaders additional headers to add to the request,
* beyond that (those) possibly added by your HttpRequestAuthorizer.
* @param responseClass the Response class
* @param errorResponseClass the class for Error Responses
* @param newExceptionFunction the new RuntimeException-creating function
* that takes a statusCode and an Error Response object.
* @param <T> the Response parameterized type
* @param <U> the Response Error parameterized type
* @return the Response of type T
* @throws RequestExecutionException if trouble executing the request
* @throws ResponseParsingException if trouble serializing the request,
* or deserializing the response
*/

public <T, U> T sendMessage(HttpRequest httpRequest, Map<String, String> additionalHeaders,
Class<T> responseClass, Class<U> errorResponseClass,
BiFunction<Integer, U, RuntimeException> newExceptionFunction)
throws RequestExecutionException, ResponseParsingException {

// If there's additional headers, add them to the request
HttpRequest httpRequestWithAdditionalHeaders = addAdditionalHeaders(httpRequest, additionalHeaders);

return sendMessage(httpRequestWithAdditionalHeaders, responseClass,
errorResponseClass, newExceptionFunction);
}

/**
* Add additional headers to the request
*
* @param httpRequest the request
* @param additionalHeaders additional headers
* @return httpRequest with additional headers
*/
private HttpProvider.HttpRequest addAdditionalHeaders(HttpProvider.HttpRequest httpRequest,
Map<String, String> additionalHeaders) {
if (null != additionalHeaders) {
for (Map.Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
String name = additionalHeader.getKey();
String value = additionalHeader.getValue();
httpRequest.addHeader(name, value);
}
}
return httpRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public abstract class AccessTokenRequest {
private Long expiresIn;

private String scope;
private Map<String, String> additionalHeaders;

protected AccessTokenRequest(String grantType) {
this.grantType = grantType;
Expand Down Expand Up @@ -154,6 +155,18 @@ public AccessTokenRequest setScope(String scope) {
return this;
}

/**
* Get any additional headers that will be added to the token request.
*
* @return the additional headers
*/
public Map<String, String> getAdditionalHeaders() { return additionalHeaders; }

public AccessTokenRequest setAdditionalHeaders(Map<String, String> additionalHeaders) {
this.additionalHeaders = additionalHeaders;
return this;
}

/**
* Converts this request, into its JSON body representation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private static Clock reuseClock(ClientAuthorizationRequestProvider clientAuthori
* Otherwise, if clientAuthorizationRequestProvider is null, or its clock is
* null, a SettableSystemClock is returned.
*
* @param clientAuthorizationRequestProvider the authorization provider
* @param clientCredentialsProvider the authorization provider
* @return the clock to use
*/
private static Clock reuseClock(ClientCredentialsProvider clientCredentialsProvider) {
Expand Down Expand Up @@ -403,8 +403,9 @@ protected AccessTokenResponse requestTokenHttp(AccessTokenRequest authorizationR
clientAuthorizer, method, url, authorizationRequest.toFormParams());

try {
return client.sendMessage(httpRequest, AccessTokenResponse.class,
ErrorResponse.class, (statusCode, errorResponse) -> {
return client.sendMessage(httpRequest, authorizationRequest.getAdditionalHeaders(),
AccessTokenResponse.class, ErrorResponse.class,
(statusCode, errorResponse) -> {
return new AccessTokenException(statusCode, errorResponse);
});
} catch (AccessTokenException e) {
Expand Down Expand Up @@ -500,7 +501,8 @@ public Fresh<AccessTokenResponse> requestAutoRefreshingToken(AccessTokenRequest
return requestAutoRefreshingToken(() -> {
return new ClientCredentialsGrantRequest()
.setExpiresIn(request.getExpiresIn())
.setScope(request.getScope());
.setScope(request.getScope())
.setAdditionalHeaders(request.getAdditionalHeaders());
});
}

Expand Down

0 comments on commit c97f46e

Please sign in to comment.