Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 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=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.filters;
|
| 24 |
|
| 25 |
|
| 26 | import com.google.common.collect.ImmutableList;
|
| 27 | import org.apache.commons.lang3.StringUtils;
|
| 28 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 29 | import org.springframework.web.filter.GenericFilterBean;
|
| 30 |
|
| 31 | import javax.servlet.FilterChain;
|
| 32 | import javax.servlet.ServletException;
|
| 33 | import javax.servlet.ServletRequest;
|
| 34 | import javax.servlet.ServletResponse;
|
| 35 | import javax.servlet.annotation.WebFilter;
|
| 36 | import javax.servlet.http.HttpServletRequest;
|
| 37 | import javax.servlet.http.HttpServletRequestWrapper;
|
| 38 | import javax.servlet.http.HttpServletResponse;
|
| 39 | import java.io.IOException;
|
| 40 | import java.util.Collections;
|
| 41 | import java.util.Enumeration;
|
| 42 | import java.util.UUID;
|
| 43 |
|
| 44 | import static org.onap.portalsdk.core.util.SystemProperties.ECOMP_REQUEST_ID;
|
| 45 |
|
| 46 | @WebFilter(urlPatterns = "/*")
|
| 47 | public class PromiseEcompRequestIdFilter extends GenericFilterBean {
|
| 48 |
|
| 49 | private final static EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PromiseEcompRequestIdFilter.class);
|
| 50 | private final static String REQUEST_ID_RESPONSE_HEADER = ECOMP_REQUEST_ID + "-echo";
|
| 51 |
|
| 52 |
|
| 53 | @Override
|
| 54 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
| 55 | throws IOException, ServletException {
|
| 56 |
|
| 57 | if (request instanceof HttpServletRequest) {
|
| 58 | request = wrapIfNeeded(request);
|
| 59 |
|
| 60 | if (response instanceof HttpServletResponse) {
|
| 61 | final String actualRequestId = ((HttpServletRequest) request).getHeader(ECOMP_REQUEST_ID);
|
| 62 | ((HttpServletResponse) response).addHeader(REQUEST_ID_RESPONSE_HEADER, actualRequestId);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | chain.doFilter(request, response);
|
| 67 | }
|
| 68 |
|
| 69 | public static ServletRequest wrapIfNeeded(ServletRequest request) {
|
| 70 | final HttpServletRequest httpRequest = (HttpServletRequest) request;
|
| 71 | final String originalRequestId = httpRequest.getHeader(ECOMP_REQUEST_ID);
|
| 72 |
|
| 73 | if (StringUtils.isEmpty(originalRequestId)) {
|
| 74 | request = new PromiseEcompRequestIdRequestWrapper(httpRequest);
|
| 75 | }
|
| 76 |
|
| 77 | return request;
|
| 78 | }
|
| 79 |
|
| 80 | private static class PromiseEcompRequestIdRequestWrapper extends HttpServletRequestWrapper {
|
| 81 |
|
| 82 | private final UUID requestId;
|
| 83 |
|
| 84 | PromiseEcompRequestIdRequestWrapper(HttpServletRequest request) {
|
| 85 | super(request);
|
| 86 | requestId = UUID.randomUUID();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public String getHeader(String name) {
|
| 91 | return isRequestIdHeaderName(name) ?
|
| 92 | requestId.toString() : super.getHeader(name);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public Enumeration<String> getHeaders(String name) {
|
| 97 | if (isRequestIdHeaderName(name)) {
|
| 98 | return Collections.enumeration(Collections.singleton(requestId.toString()));
|
| 99 | } else {
|
| 100 | return super.getHeaders(name);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public Enumeration<String> getHeaderNames() {
|
| 106 | return Collections.enumeration(ImmutableList.<String>builder()
|
| 107 | .add(ECOMP_REQUEST_ID)
|
| 108 | .addAll(Collections.list(super.getHeaderNames()))
|
| 109 | .build());
|
| 110 | }
|
| 111 |
|
| 112 | private boolean isRequestIdHeaderName(String name) {
|
| 113 | return ECOMP_REQUEST_ID.equalsIgnoreCase(name);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|