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 | |
parent | d062c0446d4f288e523aa6ad4489250794babcd7 (diff) | |
parent | b66104edbfeec09edc61cbd43107b2fe82856c80 (diff) |
Merge "Introduce request-id headers enum"
3 files changed, 45 insertions, 29 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) } + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/filter/PromiseRequestIdFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/filter/PromiseRequestIdFilterTest.java index 5f80e04a0..d67bd618c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/filter/PromiseRequestIdFilterTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/filter/PromiseRequestIdFilterTest.java @@ -52,6 +52,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.mockito.stubbing.Answer; import org.onap.portalsdk.core.web.support.UserUtils; +import org.onap.vid.logging.RequestIdHeader; import org.springframework.mock.web.MockHttpServletResponse; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -182,37 +183,37 @@ public class PromiseRequestIdFilterTest { return new Object[][]{ { - "header is selected when single", transactionIdHeader, + "header is selected when single", RequestIdHeader.TRANSACTION_ID, ImmutableMap.of( transactionIdHeader, someTxId ) }, { - "header is selected when first", onapRequestIdHeader, + "header is selected when first", RequestIdHeader.ONAP_ID, ImmutableMap.of( onapRequestIdHeader, someTxId, "noise-header", anotherTxId, ECOMP_REQUEST_ID, anotherTxId ) }, { - "header is selected when last", onapRequestIdHeader, + "header is selected when last", RequestIdHeader.ONAP_ID, ImmutableMap.of( ECOMP_REQUEST_ID, anotherTxId, "noise-header", anotherTxId, onapRequestIdHeader, someTxId ) }, { - "header is selected when value is invalid uuid", onapRequestIdHeader, + "header is selected when value is invalid uuid", RequestIdHeader.ONAP_ID, ImmutableMap.of( onapRequestIdHeader, "invalid-uuid" ) }, { - "header is selected when no ecomp-request-id", onapRequestIdHeader, + "header is selected when no ecomp-request-id", RequestIdHeader.ONAP_ID, ImmutableMap.of( requestIdHeader, anotherTxId, onapRequestIdHeader, someTxId ) }, { - "ECOMP_REQUEST_ID is returned when no request-id header", ECOMP_REQUEST_ID, + "ECOMP_REQUEST_ID is returned when no request-id header", RequestIdHeader.ECOMP_ID, ImmutableMap.of( "tsamina-mina", anotherTxId, "waka-waka", anotherTxId @@ -222,13 +223,12 @@ public class PromiseRequestIdFilterTest { } @Test(dataProvider = "severalRequestIdHeaders") - public void highestPriorityHeader_givenSeveralRequestIdHeaders_correctHeaderIsUsed(String description, String expectedHeader, Map<String, String> incomingRequestHeaders) { - PromiseRequestIdFilter testSubject = promiseRequestIdFilter; + public void highestPriorityHeader_givenSeveralRequestIdHeaders_correctHeaderIsUsed(String description, RequestIdHeader expectedHeader, Map<String, String> incomingRequestHeaders) { HttpServletRequest mockedHttpServletRequest = createMockedHttpServletRequest(incomingRequestHeaders); assertThat(description, - testSubject.highestPriorityHeader(mockedHttpServletRequest), equalToIgnoringCase(expectedHeader)); + promiseRequestIdFilter.highestPriorityHeader(mockedHttpServletRequest), is(expectedHeader)); } |