Skip to content

Commit

Permalink
fixes #1895 Update SalesforceHandler to add more trace statements for…
Browse files Browse the repository at this point in the history
… diagnosing (#1896)
  • Loading branch information
stevehu authored Sep 22, 2023
1 parent eee234c commit 50fdfe3
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {

if(logger.isTraceEnabled()) logger.trace("found with requestPath = " + requestPath + " prefix = " + pathPrefixAuth.getPathPrefix());
// matched the prefix found. handler it with the config for this prefix.
if(logger.isTraceEnabled()) logger.trace("current time = " + System.currentTimeMillis() + " expiration = " + pathPrefixAuth.getExpiration() + " waitLength = " + pathPrefixAuth.getWaitLength());
if(System.currentTimeMillis() >= (pathPrefixAuth.getExpiration() - pathPrefixAuth.getWaitLength())) { // leave 5 seconds room by default
Result<TokenResponse> result;
if(logger.isTraceEnabled()) logger.trace("grant type = " + pathPrefixAuth.getGrantType());
if("password".equals(pathPrefixAuth.getGrantType())) {
result = getPasswordToken(pathPrefixAuth);
} else {
Expand Down Expand Up @@ -342,15 +344,15 @@ private Result<TokenResponse> getAccessToken(String serverUrl, String jwt) throw
.stream()
.map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));

if(logger.isTraceEnabled()) logger.trace("request body = " + form);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(serverUrl))
.headers("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(form))
.build();

HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body().toString());
if(logger.isTraceEnabled()) logger.trace("response status = " + response.statusCode() + " response body = " + response.body().toString());
if(response.statusCode() == 200) {
// construct a token response and return it.
Map<String, Object> map = JsonMapper.string2Map(response.body().toString());
Expand Down Expand Up @@ -395,23 +397,35 @@ private void invokeApi(HttpServerExchange exchange, String authorization, String

} else if(method.equalsIgnoreCase("POST")) {
String bodyString = exchange.getAttachment(AttachmentConstants.REQUEST_BODY_STRING);
if(bodyString == null && logger.isDebugEnabled()) logger.debug("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
if(bodyString == null) {
if(logger.isTraceEnabled()) logger.trace("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
} else {
if(logger.isTraceEnabled()) logger.trace("request body = " + bodyString);
}
request = HttpRequest.newBuilder()
.uri(new URI(requestHost + requestPath))
.headers("Authorization", authorization, "Content-Type", contentType)
.POST(bodyString == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(bodyString))
.build();
} else if(method.equalsIgnoreCase("PUT")) {
String bodyString = exchange.getAttachment(AttachmentConstants.REQUEST_BODY_STRING);
if(bodyString == null && logger.isDebugEnabled()) logger.debug("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
if(bodyString == null) {
if(logger.isTraceEnabled()) logger.trace("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
} else {
if(logger.isTraceEnabled()) logger.trace("request body = " + bodyString);
}
request = HttpRequest.newBuilder()
.uri(new URI(requestHost + requestPath))
.headers("Authorization", authorization, "Content-Type", contentType)
.PUT(bodyString == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(bodyString))
.build();
} else if(method.equalsIgnoreCase("PATCH")) {
String bodyString = exchange.getAttachment(AttachmentConstants.REQUEST_BODY_STRING);
if(bodyString == null && logger.isDebugEnabled()) logger.debug("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
if(bodyString == null) {
if(logger.isTraceEnabled()) logger.trace("The request body is null and the request path might be missing in request-injection.appliedBodyInjectionPathPrefixes.");
} else {
if(logger.isTraceEnabled()) logger.trace("request body = " + bodyString);
}
request = HttpRequest.newBuilder()
.uri(new URI(requestHost + requestPath))
.headers("Authorization", authorization, "Content-Type", contentType)
Expand All @@ -434,7 +448,7 @@ private void invokeApi(HttpServerExchange exchange, String authorization, String
}
}
}
if(logger.isTraceEnabled()) logger.trace("response body = " + responseBody);
if(logger.isTraceEnabled()) logger.trace("response body = " + (responseBody == null ? null : new String(responseBody, StandardCharsets.UTF_8)));
exchange.getResponseSender().send(ByteBuffer.wrap(responseBody));
if(config.isMetricsInjection() && metricsHandler != null) {
if(logger.isTraceEnabled()) logger.trace("injecting metrics for " + config.getMetricsName());
Expand Down

0 comments on commit 50fdfe3

Please sign in to comment.