2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.controller.filter;
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
25 import static org.apache.commons.lang3.StringUtils.isNotEmpty;
26 import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID;
28 import com.google.common.collect.ImmutableList;
29 import java.io.IOException;
30 import java.util.Collections;
31 import java.util.Enumeration;
32 import java.util.UUID;
33 import java.util.function.Supplier;
34 import java.util.regex.Pattern;
35 import javax.servlet.FilterChain;
36 import javax.servlet.ServletException;
37 import javax.servlet.ServletRequest;
38 import javax.servlet.ServletResponse;
39 import javax.servlet.annotation.WebFilter;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletRequestWrapper;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.validation.constraints.NotNull;
44 import org.onap.logging.ref.slf4j.ONAPLogConstants.MDCs;
45 import org.onap.vid.logging.Headers;
46 import org.onap.vid.logging.RequestIdHeader;
48 import org.springframework.web.filter.GenericFilterBean;
50 @WebFilter(urlPatterns = "/*")
51 public class PromiseRequestIdFilter extends GenericFilterBean {
53 // The wrapped request is guaranteed to have the transaction id as the value
54 // of the header PROMISED_HEADER_NAME.
55 // PROMISED_HEADER_NAME is set to ECOMP_REQUEST_ID as long as
56 // org.onap.portalsdk...UserUtils.getRequestId() is using the header
57 // "X-ECOMP-RequestID".
58 private static final RequestIdHeader PROMISED_HEADER = RequestIdHeader.ECOMP_ID;
59 private static final String REQUEST_ID_RESPONSE_HEADER = ECOMP_REQUEST_ID + "-echo";
61 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);
64 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
65 throws IOException, ServletException {
67 if (request instanceof HttpServletRequest) {
68 request = wrapIfNeeded(request);
70 if (response instanceof HttpServletResponse) {
71 final String actualRequestId = PROMISED_HEADER.getHeaderValue((HttpServletRequest) request);
72 ((HttpServletResponse) response).addHeader(REQUEST_ID_RESPONSE_HEADER, actualRequestId);
76 chain.doFilter(request, response);
79 public ServletRequest wrapIfNeeded(ServletRequest request) {
80 final HttpServletRequest httpRequest = (HttpServletRequest) request;
82 final RequestIdHeader highestPriorityHeader = highestPriorityHeader(httpRequest);
83 final String originalRequestId = highestPriorityHeader.getHeaderValue(httpRequest);
85 if (isWrapNeeded(highestPriorityHeader, originalRequestId)) {
86 // Copy originalRequestId to the promised header value
87 request = new PromiseRequestIdRequestWrapper(httpRequest,
88 firstValidUuidOrElse(originalRequestId, requestIdFromMDC(), UUID::randomUUID));
94 private String requestIdFromMDC() {
95 return MDC.get(MDCs.REQUEST_ID);
98 private boolean verifyAndValidateUuid(String value) {
99 return isNotEmpty(value) && uuidRegex.matcher(value).matches();
102 private boolean isWrapNeeded(RequestIdHeader highestPriorityHeader, String originalRequestId) {
103 boolean headerExistsAndValid =
104 PROMISED_HEADER == highestPriorityHeader && verifyAndValidateUuid(originalRequestId);
106 return !headerExistsAndValid;
109 UUID firstValidUuidOrElse(String uuid1, String uuid2, Supplier<UUID> uuidSupplier) {
110 return toUuidOrElse(uuid1, () -> toUuidOrElse(uuid2, uuidSupplier));
113 UUID toUuidOrElse(String uuid, Supplier<UUID> uuidSupplier) {
114 if (verifyAndValidateUuid(uuid)) {
116 return UUID.fromString(uuid);
117 } catch (IllegalArgumentException e) {
118 return uuidSupplier.get();
121 return uuidSupplier.get();
125 RequestIdHeader highestPriorityHeader(HttpServletRequest httpRequest) {
126 return defaultIfNull(Headers.highestPriorityHeader(httpRequest), PROMISED_HEADER);
129 private static class PromiseRequestIdRequestWrapper extends HttpServletRequestWrapper {
131 private final UUID requestId;
133 PromiseRequestIdRequestWrapper(HttpServletRequest request, @NotNull UUID requestId) {
135 this.requestId = requestId;
139 public String getHeader(String name) {
140 return isRequestIdHeaderName(name) ?
141 requestId.toString() : super.getHeader(name);
145 public Enumeration<String> getHeaders(String name) {
146 if (isRequestIdHeaderName(name)) {
147 return Collections.enumeration(Collections.singleton(requestId.toString()));
149 return super.getHeaders(name);
154 public Enumeration<String> getHeaderNames() {
156 if (null == super.getHeader(PROMISED_HEADER.getHeaderName())) {
157 return Collections.enumeration(ImmutableList.<String>builder()
158 .add(PROMISED_HEADER.getHeaderName())
159 .addAll(Collections.list(super.getHeaderNames()))
163 return super.getHeaderNames();
166 private boolean isRequestIdHeaderName(String name) {
167 return PROMISED_HEADER.stringEquals(name);