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