diff options
author | Amichai Hemli <amichai.hemli@intl.att.com> | 2019-09-22 14:45:17 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-09-22 14:45:17 +0000 |
commit | f218636cc2c7ca337d6e23f24fc282bbbacd79f1 (patch) | |
tree | 975303b0b75853e93baee962226fb9e6f7c93742 | |
parent | 608c7c4ba8b3fa8ec22d0237a8fffc0da14cefc2 (diff) | |
parent | 2be2e77d30a3cd547c1a37fb2f280b5fabf1ecd6 (diff) |
Merge "Rename PromiseRequestIdFilter, extract isWrapNeeded and incomingRequestId methods"
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java (renamed from vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java) | 52 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java (renamed from vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java) | 48 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java | 6 |
3 files changed, 61 insertions, 45 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java index 37622a215..c6b18785a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseEcompRequestIdFilter.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/filter/PromiseRequestIdFilter.java @@ -21,10 +21,14 @@ package org.onap.vid.controller.filter; -import com.google.common.collect.ImmutableList; -import org.apache.commons.lang3.StringUtils; -import org.springframework.web.filter.GenericFilterBean; +import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; +import com.google.common.collect.ImmutableList; +import java.io.IOException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.UUID; +import java.util.regex.Pattern; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -33,18 +37,15 @@ import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Collections; -import java.util.Enumeration; -import java.util.UUID; - -import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.filter.GenericFilterBean; @WebFilter(urlPatterns = "/*") -public class PromiseEcompRequestIdFilter extends GenericFilterBean { +public class PromiseRequestIdFilter extends GenericFilterBean { 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}"); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) @@ -62,28 +63,35 @@ public class PromiseEcompRequestIdFilter extends GenericFilterBean { chain.doFilter(request, response); } - public static ServletRequest wrapIfNeeded(ServletRequest request) { + public ServletRequest wrapIfNeeded(ServletRequest request) { final HttpServletRequest httpRequest = (HttpServletRequest) request; - final String originalRequestId = httpRequest.getHeader(ECOMP_REQUEST_ID); + final String originalRequestId = incomingRequestId(httpRequest); - if (StringUtils.isEmpty(originalRequestId) || !verifyAndValidateUuid(originalRequestId)) { - request = new PromiseEcompRequestIdRequestWrapper(httpRequest); + if (isWrapNeeded(originalRequestId)) { + request = new PromiseRequestIdRequestWrapper(httpRequest); } return request; } - public static boolean verifyAndValidateUuid(String value) - { - String uuidRegex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; - return value.matches(uuidRegex); + private boolean verifyAndValidateUuid(String value) { + return uuidRegex.matcher(value).matches(); } - private static class PromiseEcompRequestIdRequestWrapper extends HttpServletRequestWrapper { + protected boolean isWrapNeeded(String originalRequestId) { + return StringUtils.isEmpty(originalRequestId) + || !verifyAndValidateUuid(originalRequestId); + } + + protected String incomingRequestId(HttpServletRequest httpRequest) { + return httpRequest.getHeader(ECOMP_REQUEST_ID); + } + + private static class PromiseRequestIdRequestWrapper extends HttpServletRequestWrapper { private final UUID requestId; - PromiseEcompRequestIdRequestWrapper(HttpServletRequest request) { + PromiseRequestIdRequestWrapper(HttpServletRequest request) { super(request); requestId = UUID.randomUUID(); } @@ -107,11 +115,11 @@ public class PromiseEcompRequestIdFilter extends GenericFilterBean { public Enumeration<String> getHeaderNames() { if (null == super.getHeader(ECOMP_REQUEST_ID)) { - return Collections.enumeration(ImmutableList.<String>builder() + return Collections.enumeration(ImmutableList.<String>builder() .add(ECOMP_REQUEST_ID) .addAll(Collections.list(super.getHeaderNames())) .build()); - } + } return super.getHeaderNames(); } diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java index 39638c305..2304d46f3 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/PromiseEcompRequestIdFilterTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/PromiseRequestIdFilterTest.java @@ -20,35 +20,41 @@ package org.onap.vid.controller; -import com.google.common.collect.ImmutableMap; -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.controller.filter.PromiseEcompRequestIdFilter; -import org.springframework.mock.web.MockHttpServletResponse; -import org.testng.annotations.Test; +import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.emptyOrNullString; +import static org.hamcrest.Matchers.equalToIgnoringCase; +import static org.hamcrest.Matchers.not; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +import java.util.function.Function; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.*; -import java.util.function.Function; - -import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.argThat; -import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID; +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.controller.filter.PromiseRequestIdFilter; +import org.springframework.mock.web.MockHttpServletResponse; +import org.testng.annotations.Test; @Test -public class PromiseEcompRequestIdFilterTest { +public class PromiseRequestIdFilterTest { private final String anotherHeader = "ANDREI_RUBLEV"; private final String anotherValue = "foo value"; @@ -119,7 +125,7 @@ public class PromiseEcompRequestIdFilterTest { // // doFilter() is the function under test // - new PromiseEcompRequestIdFilter().doFilter(servletRequest, servletResponse, capturingFilterChain); + new PromiseRequestIdFilter().doFilter(servletRequest, servletResponse, capturingFilterChain); // ////////////////// diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java index 20a05e334..ac05ea706 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java @@ -50,7 +50,7 @@ import org.mockito.MockitoAnnotations; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.aai.util.ServletRequestHelper; import org.onap.vid.aai.util.SystemPropertyHelper; -import org.onap.vid.controller.filter.PromiseEcompRequestIdFilter; +import org.onap.vid.controller.filter.PromiseRequestIdFilter; import org.onap.vid.testUtils.TestUtils; import org.onap.vid.utils.Logging; import org.onap.vid.utils.Unchecked; @@ -65,6 +65,7 @@ import org.testng.annotations.Test; public class OutgoingRequestHeadersTest { + private static final PromiseRequestIdFilter promiseRequestIdFilter = new PromiseRequestIdFilter(); // @InjectMocks // private RestMsoImplementation restMsoImplementation; @@ -96,7 +97,8 @@ public class OutgoingRequestHeadersTest { } public static void putRequestInSpringContext() { - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes((HttpServletRequest) PromiseEcompRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest()))); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes( + (HttpServletRequest) promiseRequestIdFilter.wrapIfNeeded(new MockHttpServletRequest()))); } // @DataProvider |