From 1ed57cca9ff50709bff69d8c54b558fddb68ba03 Mon Sep 17 00:00:00 2001 From: arucard21 Date: Fri, 15 Nov 2024 12:42:16 +0100 Subject: [PATCH] Add the API-Version HTTP header to every response, with the version number provided as a property or environment variable. --- examples/nlgov-adr/README.md | 3 +- examples/nlgov-adr/build.gradle | 1 + .../example/nlgov_adr/ApiVersionFilter.java | 37 +++++++++++++++++++ .../nlgov_adr/ExampleResourceConfig.java | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 examples/nlgov-adr/src/main/java/example/nlgov_adr/ApiVersionFilter.java diff --git a/examples/nlgov-adr/README.md b/examples/nlgov-adr/README.md index 62eb2fa..9e632cf 100644 --- a/examples/nlgov-adr/README.md +++ b/examples/nlgov-adr/README.md @@ -31,9 +31,8 @@ API-20 Yes API-48 Yes API-51 No CORS policy is invalid API-56 Yes -API-57 No API-Version header not found +API-57 Yes Validation failed ``` There is currently nothing configured for CORS in the API so it is expected to fail that test. -The API-Version header is currently only documented in the OpenAPI Specification but it is not yet returned by the API. This can be easily fixed with a JAX-RS filter that adds this header to every response. diff --git a/examples/nlgov-adr/build.gradle b/examples/nlgov-adr/build.gradle index abdd9d8..5281254 100644 --- a/examples/nlgov-adr/build.gradle +++ b/examples/nlgov-adr/build.gradle @@ -19,6 +19,7 @@ application{ bootRun { environment "server.port", "8888" environment "SIMPLYRESTFUL_URI_HTTP_HEADER", "xoriginalurl" + environment "API_VERSION" "0.0.1" } dependencies { diff --git a/examples/nlgov-adr/src/main/java/example/nlgov_adr/ApiVersionFilter.java b/examples/nlgov-adr/src/main/java/example/nlgov_adr/ApiVersionFilter.java new file mode 100644 index 0000000..3246292 --- /dev/null +++ b/examples/nlgov-adr/src/main/java/example/nlgov_adr/ApiVersionFilter.java @@ -0,0 +1,37 @@ +package example.nlgov_adr; + +import java.io.IOException; + +import jakarta.inject.Named; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerResponseContext; +import jakarta.ws.rs.container.ContainerResponseFilter; + +/** + * Add an HTTP header named "API-Version" to every response. + * + * It contains the version of the API which can be defined in a property or + * environment variable called "API_VERSION". + */ +@Named +public class ApiVersionFilter implements ContainerResponseFilter { + public static final String API_VERSION_HTTP_HEADER_NAME = "API-Version"; + public static final String API_VERSION_ENV_NAME = "API_VERSION"; + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + String apiVersion = getApiVersion(); + if(apiVersion != null) { + responseContext.getHeaders().add(API_VERSION_HTTP_HEADER_NAME, apiVersion); + } + } + + private String getApiVersion() { + String apiVersion = System.getProperty(API_VERSION_ENV_NAME); + if (apiVersion == null) { + apiVersion = System.getenv(API_VERSION_ENV_NAME); + } + return apiVersion; + } +} diff --git a/examples/nlgov-adr/src/main/java/example/nlgov_adr/ExampleResourceConfig.java b/examples/nlgov-adr/src/main/java/example/nlgov_adr/ExampleResourceConfig.java index 9fcf3df..a8022cd 100644 --- a/examples/nlgov-adr/src/main/java/example/nlgov_adr/ExampleResourceConfig.java +++ b/examples/nlgov-adr/src/main/java/example/nlgov_adr/ExampleResourceConfig.java @@ -12,6 +12,7 @@ public class ExampleResourceConfig extends ResourceConfig { public ExampleResourceConfig() { register(ExampleWebResource.class); + register(ApiVersionFilter.class); property(ServletProperties.FILTER_FORWARD_ON_404, true); } }