Skip to content

Commit

Permalink
Add header containing the request id if available
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheus-87 committed Jul 4, 2024
1 parent f0287bb commit 61c90ab
Showing 1 changed file with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.common.base.Strings;
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.exception.http.HttpErrorDecoder;
import de.digitalcollections.model.list.ListRequest;
Expand Down Expand Up @@ -36,6 +37,7 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

@SuppressFBWarnings(value = "EI_EXPOSE_REP2")
public abstract class BaseRestClient<T extends Object> {
Expand Down Expand Up @@ -78,15 +80,13 @@ public T create() throws TechnicalException {
private HttpRequest createDeleteRequest(String requestUrl) {
final URI url = createFullUri(requestUrl);
LOGGER.debug("DELETE " + url);
HttpRequest req =
HttpRequest.newBuilder()
.DELETE()
.uri(url)
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
HttpRequest.Builder req =
HttpRequest.newBuilder().DELETE().uri(url).header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

public URI createFullUri(String requestUrl) {
Expand All @@ -96,93 +96,96 @@ public URI createFullUri(String requestUrl) {
private HttpRequest createGetRequest(String requestUrl) {
final URI url = createFullUri(requestUrl);
LOGGER.debug("GET " + url);
HttpRequest req =
HttpRequest.newBuilder()
.GET()
.uri(url)
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
HttpRequest.Builder req =
HttpRequest.newBuilder().GET().uri(url).header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

private HttpRequest createPatchRequest(String requestUrl) {
final URI url = createFullUri(requestUrl);
LOGGER.debug("PATCH " + url);
HttpRequest req =
HttpRequest.Builder req =
HttpRequest.newBuilder()
.method("PATCH", HttpRequest.BodyPublishers.noBody())
.uri(url)
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
.header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

private HttpRequest createPatchRequest(String requestUrl, Object bodyObject)
throws JsonProcessingException {
final URI url = createFullUri(requestUrl);
LOGGER.debug("PATCH " + url + " with body");
HttpRequest req =
HttpRequest.Builder req =
HttpRequest.newBuilder()
.method(
"PATCH", HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(bodyObject)))
.uri(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
.header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

private HttpRequest createPostRequest(String requestUrl) throws JsonProcessingException {
final URI url = createFullUri(requestUrl);
LOGGER.debug("POST " + url);
HttpRequest req =
HttpRequest.Builder req =
HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.noBody())
.uri(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
.header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

private HttpRequest createPostRequest(String requestUrl, Object bodyObject)
throws JsonProcessingException {
final URI url = createFullUri(requestUrl);
LOGGER.debug("POST " + url + " with body");
HttpRequest req =
HttpRequest.Builder req =
HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(bodyObject)))
.uri(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
.header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

private HttpRequest createPutRequest(String requestUrl, Object bodyObject)
throws JsonProcessingException {
final URI url = createFullUri(requestUrl);
LOGGER.debug("PUT " + url + " with body");
HttpRequest req =
HttpRequest.Builder req =
HttpRequest.newBuilder()
.PUT(HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(bodyObject)))
.uri(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
.build();
return req;
.header("Accept", "application/json");
String requestId = MDC.get("request_id");
if (!Strings.nullToEmpty(requestId).trim().isBlank()) {
req.header("X-Request-Id", requestId);
}
return req.build();
}

protected String doDeleteRequestForString(String requestUrl) throws TechnicalException {
Expand Down Expand Up @@ -284,8 +287,6 @@ protected <U> List<U> doGetRequestForObjectList(
requestUrl += (requestUrl.contains("?") ? "&" : "?") + getSortParams(listRequest);
}
HttpRequest req = createGetRequest(requestUrl);
// TODO add creation of a request id if needed
// .header("X-Request-Id", request.getRequestId())
try {
HttpResponse<byte[]> response = http.send(req, HttpResponse.BodyHandlers.ofByteArray());
Integer statusCode = response.statusCode();
Expand Down

0 comments on commit 61c90ab

Please sign in to comment.