-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the API-Version HTTP header to every response, with the version n…
…umber provided as a property or environment variable.
- Loading branch information
Showing
4 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
examples/nlgov-adr/src/main/java/example/nlgov_adr/ApiVersionFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters