eb08391a98c3ac4ef995f6f5afeda66d3e3bccac
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.main.web;
22
23 import java.io.IOException;
24 import java.util.UUID;
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.springframework.core.annotation.Order;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 @Order(2)
37 public class RequestResponseLoggingFilter implements Filter {
38
39     private static final String VERSION_MINOR_NAME = "X-MinorVersion";
40     private static final String VERSION_PATCH_NAME = "X-PatchVersion";
41     private static final String VERSION_LATEST_NAME = "X-LatestVersion";
42     public static final String API_VERSION = "1.0.0";
43     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
44
45     @Override
46     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
47             throws IOException, ServletException {
48
49
50         HttpServletResponse res = (HttpServletResponse) response;
51         HttpServletRequest req = (HttpServletRequest) request;
52
53         String requestId = req.getHeader(REQUEST_ID_NAME);
54         res.addHeader(REQUEST_ID_NAME, requestId != null ? requestId : UUID.randomUUID().toString());
55
56         res.addHeader(VERSION_MINOR_NAME, "0");
57         res.addHeader(VERSION_PATCH_NAME, "0");
58         res.addHeader(VERSION_LATEST_NAME, API_VERSION);
59
60         chain.doFilter(request, response);
61     }
62
63 }