diff options
author | Amichai Hemli <amichai.hemli@intl.att.com> | 2019-09-25 10:43:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-09-25 10:43:09 +0000 |
commit | 41c8e5685b70387c203b4c225a1573697d28b6d1 (patch) | |
tree | fd3a66ad195bc589071d402837b8357bbe063388 /vid-app-common/src/main | |
parent | d062c0446d4f288e523aa6ad4489250794babcd7 (diff) | |
parent | b66104edbfeec09edc61cbd43107b2fe82856c80 (diff) |
Merge "Introduce request-id headers enum"
Diffstat (limited to 'vid-app-common/src/main')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java | 24 | ||||
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/logging/Headers.kt | 32 |
2 files changed, 36 insertions, 20 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java index ac93d8751..faaf55ae7 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java @@ -22,7 +22,6 @@ package org.onap.vid.controller.filter; import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; -import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; import static org.apache.commons.lang3.StringUtils.isNotEmpty; import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; @@ -43,6 +42,7 @@ import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotNull; import org.onap.vid.logging.Headers; +import org.onap.vid.logging.RequestIdHeader; import org.springframework.web.filter.GenericFilterBean; @WebFilter(urlPatterns = "/*") @@ -53,7 +53,7 @@ public class PromiseRequestIdFilter extends GenericFilterBean { // PROMISED_HEADER_NAME is set to ECOMP_REQUEST_ID as long as // org.onap.portalsdk...UserUtils.getRequestId() is using the header // "X-ECOMP-RequestID". - private static final String PROMISED_HEADER_NAME = ECOMP_REQUEST_ID; + private static final RequestIdHeader PROMISED_HEADER = RequestIdHeader.ECOMP_ID; private static final String REQUEST_ID_RESPONSE_HEADER = ECOMP_REQUEST_ID + "-echo"; private static final Pattern uuidRegex = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", Pattern.CASE_INSENSITIVE); @@ -66,7 +66,7 @@ public class PromiseRequestIdFilter extends GenericFilterBean { request = wrapIfNeeded(request); if (response instanceof HttpServletResponse) { - final String actualRequestId = ((HttpServletRequest) request).getHeader(PROMISED_HEADER_NAME); + final String actualRequestId = PROMISED_HEADER.getHeaderValue((HttpServletRequest) request); ((HttpServletResponse) response).addHeader(REQUEST_ID_RESPONSE_HEADER, actualRequestId); } } @@ -77,8 +77,8 @@ public class PromiseRequestIdFilter extends GenericFilterBean { public ServletRequest wrapIfNeeded(ServletRequest request) { final HttpServletRequest httpRequest = (HttpServletRequest) request; - final String highestPriorityHeader = highestPriorityHeader(httpRequest); - final String originalRequestId = httpRequest.getHeader(highestPriorityHeader); + final RequestIdHeader highestPriorityHeader = highestPriorityHeader(httpRequest); + final String originalRequestId = highestPriorityHeader.getHeaderValue(httpRequest); if (isWrapNeeded(highestPriorityHeader, originalRequestId)) { // Copy originalRequestId to the promised header value @@ -92,9 +92,9 @@ public class PromiseRequestIdFilter extends GenericFilterBean { return isNotEmpty(value) && uuidRegex.matcher(value).matches(); } - private boolean isWrapNeeded(String highestPriorityHeader, String originalRequestId) { + private boolean isWrapNeeded(RequestIdHeader highestPriorityHeader, String originalRequestId) { boolean headerExistsAndValid = - equalsIgnoreCase(highestPriorityHeader, PROMISED_HEADER_NAME) && verifyAndValidateUuid(originalRequestId); + PROMISED_HEADER == highestPriorityHeader && verifyAndValidateUuid(originalRequestId); return !headerExistsAndValid; } @@ -111,8 +111,8 @@ public class PromiseRequestIdFilter extends GenericFilterBean { } } - String highestPriorityHeader(HttpServletRequest httpRequest) { - return defaultIfNull(Headers.highestPriorityHeader(httpRequest), PROMISED_HEADER_NAME); + RequestIdHeader highestPriorityHeader(HttpServletRequest httpRequest) { + return defaultIfNull(Headers.highestPriorityHeader(httpRequest), PROMISED_HEADER); } private static class PromiseRequestIdRequestWrapper extends HttpServletRequestWrapper { @@ -142,9 +142,9 @@ public class PromiseRequestIdFilter extends GenericFilterBean { @Override public Enumeration<String> getHeaderNames() { - if (null == super.getHeader(PROMISED_HEADER_NAME)) { + if (null == super.getHeader(PROMISED_HEADER.getHeaderName())) { return Collections.enumeration(ImmutableList.<String>builder() - .add(PROMISED_HEADER_NAME) + .add(PROMISED_HEADER.getHeaderName()) .addAll(Collections.list(super.getHeaderNames())) .build()); } @@ -153,7 +153,7 @@ public class PromiseRequestIdFilter extends GenericFilterBean { } private boolean isRequestIdHeaderName(String name) { - return equalsIgnoreCase(name, PROMISED_HEADER_NAME); + return PROMISED_HEADER.stringEquals(name); } } } diff --git a/vid-app-common/src/main/java/org/onap/vid/logging/Headers.kt b/vid-app-common/src/main/java/org/onap/vid/logging/Headers.kt index e710fd973..a0bbceea1 100644 --- a/vid-app-common/src/main/java/org/onap/vid/logging/Headers.kt +++ b/vid-app-common/src/main/java/org/onap/vid/logging/Headers.kt @@ -2,17 +2,33 @@ package org.onap.vid.logging -import org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID +import org.onap.portalsdk.core.util.SystemProperties +import org.onap.vid.logging.RequestIdHeader.* import javax.servlet.http.HttpServletRequest +enum class RequestIdHeader(val headerName: String) { + + ONAP_ID("X-ONAP-RequestID"), + REQUEST_ID("X-RequestID"), + TRANSACTION_ID("X-TransactionID"), + ECOMP_ID(SystemProperties.ECOMP_REQUEST_ID), + ; + + fun stringEquals(header: String) = headerName.equals(header, true) + + fun getHeaderValue(request: HttpServletRequest): String? = request.getHeader(headerName) +} + fun prioritizedRequestIdHeaders() = listOf( - "X-ONAP-RequestID", - "X-RequestID", - "X-TransactionID", - ECOMP_REQUEST_ID + ONAP_ID, + REQUEST_ID, + TRANSACTION_ID, + ECOMP_ID ) -fun highestPriorityHeader(httpRequest: HttpServletRequest): String? { - val headers = httpRequest.headerNames.asSequence().toSet().map { it.toUpperCase() } - return prioritizedRequestIdHeaders().firstOrNull { headers.contains(it.toUpperCase()) } +fun highestPriorityHeader(httpRequest: HttpServletRequest): RequestIdHeader? { + val headers = httpRequest.headerNames.asSequence().toSet() + return prioritizedRequestIdHeaders().firstOrNull { + requestIdHeader -> headers.any { requestIdHeader.stringEquals(it) } + } } |