Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / interceptors / pre / RequestModification.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.aai.interceptors.pre;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29
30 import javax.annotation.Priority;
31 import javax.ws.rs.container.ContainerRequestContext;
32 import javax.ws.rs.container.ContainerRequestFilter;
33 import javax.ws.rs.container.PreMatching;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.UriBuilder;
36
37 import org.onap.aai.interceptors.AAIContainerFilter;
38
39 @PreMatching
40 @Priority(AAIRequestFilterPriority.HEADER_VALIDATION)
41 public class RequestModification extends AAIContainerFilter implements ContainerRequestFilter {
42
43         @Override
44         public void filter(ContainerRequestContext requestContext) throws IOException {
45
46                 this.cleanDME2QueryParams(requestContext);
47
48         }
49         
50         private void cleanDME2QueryParams(ContainerRequestContext request) {
51                 UriBuilder builder = request.getUriInfo().getRequestUriBuilder();
52                 MultivaluedMap<String, String> queries = request.getUriInfo().getQueryParameters();
53
54                 String[] blacklist = { "version", "envContext", "routeOffer" };
55                 Set<String> blacklistSet = Arrays.stream(blacklist).collect(Collectors.toSet());
56
57                 boolean remove = true;
58
59                 for (String param : blacklistSet) {
60                         if (!queries.containsKey(param)) {
61                                 remove = false;
62                                 break;
63                         }
64                 }
65
66                 if (remove) {
67                         for (Map.Entry<String, List<String>> query : queries.entrySet()) {
68                                 String key = query.getKey();
69                                 if (blacklistSet.contains(key)) {
70                                         builder.replaceQueryParam(key);
71                                 }
72                         }
73                 }
74                 request.setRequestUri(builder.build());
75         }
76
77 }