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

Add header containing the request id if available #142

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

- New endpoint `/v6/digipress` for faster retrieval of the newspaper list (manifestations):
supports both filtering and sorting
- Client: Added header containing the request id if available

### Fixed

- Fixed Nullpointer in `Manifestation.dumpShortenedRelations`
- Fixed clearing of request id

## [9.2.1](https://github.com/dbmdz/metadata-service/releases/tag/9.2.1) - 2024-06-11

Expand Down
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;
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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()) {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.dbmdz.metadata.server.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.HandlerInterceptor;

public class MdcLoggingInterceptor implements HandlerInterceptor {
/** Clear MDC to avoid data leaking between two requests handled by the same thread. */
@Override
public void afterCompletion(
@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler,
Exception ex)
throws Exception {
MDC.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,8 @@
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class RequestIdLoggingInterceptor implements HandlerInterceptor {
/** Clear MDC to avoid data leaking between two requests handled by the same thread. */
@Override
public void postHandle(
@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler,
ModelAndView modelAndView) {
MDC.clear();
}

/**
* Register the request identifier (if received from client/frontend server) in the logging
Expand Down
Loading