915cdf0b2bfcc8e156115f649624bec74070e335
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 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
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.common.rest;
24
25 import java.io.IOException;
26 import java.util.UUID;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import org.springframework.core.annotation.Order;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 @Order(2)
39 public class RequestResponseLoggingFilter implements Filter {
40
41     private static final String VERSION_MINOR_NAME = "X-MinorVersion";
42     private static final String VERSION_PATCH_NAME = "X-PatchVersion";
43     private static final String VERSION_LATEST_NAME = "X-LatestVersion";
44     public static final String API_VERSION = "1.0.0";
45     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
46
47     @Override
48     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
49             throws IOException, ServletException {
50
51
52         HttpServletResponse res = (HttpServletResponse) response;
53         HttpServletRequest req = (HttpServletRequest) request;
54
55         /*
56          * Disabling sonar because of ONAP requires the request ID to be copied from the request
57          * to the response.
58          */
59         String requestId = req.getHeader(REQUEST_ID_NAME);
60         res.addHeader(REQUEST_ID_NAME, requestId != null ? requestId : UUID.randomUUID().toString()); // NOSONAR
61
62         res.addHeader(VERSION_MINOR_NAME, "0");
63         res.addHeader(VERSION_PATCH_NAME, "0");
64         res.addHeader(VERSION_LATEST_NAME, API_VERSION);
65
66         chain.doFilter(request, response);
67     }
68
69 }