211350ac36b74b3827e418e2a56da6a2fe2144b0
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / interceptors / pre / RequestHeaderManipulation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.schemaservice.interceptors.pre;
21
22 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
23 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
24
25 import javax.annotation.Priority;
26 import javax.ws.rs.container.ContainerRequestContext;
27 import javax.ws.rs.container.ContainerRequestFilter;
28 import javax.ws.rs.container.PreMatching;
29 import javax.ws.rs.core.MultivaluedMap;
30 import java.util.Collections;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 @PreMatching
35 @Priority(AAIRequestFilterPriority.HEADER_MANIPULATION)
36 public class RequestHeaderManipulation extends AAIContainerFilter implements ContainerRequestFilter {
37
38     public static final Pattern EXTRACT_VERSION_PATTERN = Pattern.compile("^(v[1-9][0-9]*).*$");
39
40     @Override
41     public void filter(ContainerRequestContext requestContext) {
42
43         String uri = requestContext.getUriInfo().getPath();
44         this.addRequestContext(uri, requestContext.getHeaders());
45
46     }
47
48     private void addRequestContext(String uri, MultivaluedMap<String, String> requestHeaders) {
49
50         String rc = "";
51
52         Matcher match = EXTRACT_VERSION_PATTERN.matcher(uri);
53         if (match.find()) {
54             rc = match.group(1);
55         }
56
57         if (requestHeaders.containsKey(AAIHeaderProperties.REQUEST_CONTEXT)) {
58             requestHeaders.remove(AAIHeaderProperties.REQUEST_CONTEXT);
59         }
60         requestHeaders.put(AAIHeaderProperties.REQUEST_CONTEXT, Collections.singletonList(rc));
61     }
62
63 }