diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-09-19 13:10:26 +0300 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2019-09-19 14:28:08 +0300 |
commit | 2be2e77d30a3cd547c1a37fb2f280b5fabf1ecd6 (patch) | |
tree | eaba9dc69dca7777dc6c6ac0e12b8ff26fa643f1 /vid-app-common/src/main/java | |
parent | 497e040b5569b0b0139a5cbc8115a63bc7f99244 (diff) |
Rename PromiseRequestIdFilter, extract isWrapNeeded and incomingRequestId methods
Change-Id: Idb39f7d5e723598e34e737522c4f73d8de3df93c
Issue-ID: VID-253
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main/java')
-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 |
1 files changed, 30 insertions, 22 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(); } |