Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / web / JerseyConfiguration.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.web;
21
22 import org.glassfish.jersey.filter.LoggingFilter;
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.servlet.ServletProperties;
25 import org.onap.aai.rest.QueryConsumer;
26 import org.onap.aai.rest.retired.V3ThroughV7Consumer;
27 import org.onap.aai.rest.search.ModelAndNamedQueryRestProvider;
28 import org.onap.aai.rest.search.SearchProvider;
29 import org.onap.aai.rest.util.EchoResponse;
30 import org.reflections.Reflections;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.core.env.Environment;
34 import org.springframework.stereotype.Component;
35
36 import javax.annotation.Priority;
37 import javax.ws.rs.ApplicationPath;
38 import javax.ws.rs.container.ContainerRequestFilter;
39 import javax.ws.rs.container.ContainerResponseFilter;
40 import java.util.List;
41 import java.util.Set;
42 import java.util.logging.Logger;
43 import java.util.stream.Collectors;
44
45 @Component
46 @ApplicationPath("/aai")
47 public class JerseyConfiguration extends ResourceConfig {
48
49     private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
50
51     private Environment env;
52
53     @Autowired
54     public JerseyConfiguration(Environment env) {
55
56         this.env = env;
57
58         register(SearchProvider.class);
59         register(ModelAndNamedQueryRestProvider.class);
60         register(QueryConsumer.class);
61
62         register(V3ThroughV7Consumer.class);
63         register(EchoResponse.class);
64
65         //Request Filters
66         registerFiltersForRequests();
67         // Response Filters
68         registerFiltersForResponses();
69
70         property(ServletProperties.FILTER_FORWARD_ON_404, true);
71
72         // Following registers the request headers and response headers
73         // If the LoggingFilter second argument is set to true, it will print response value as well
74         if ("true".equalsIgnoreCase(env.getProperty("aai.request.logging.enabled"))) {
75             register(new LoggingFilter(log, false));
76         }
77     }
78
79     public void registerFiltersForRequests() {
80
81         // Find all the classes within the interceptors package
82         Reflections reflections = new Reflections("org.onap.aai.interceptors");
83         // Filter them based on the clazz that was passed in
84         Set<Class<? extends ContainerRequestFilter>> filters = reflections.getSubTypesOf(ContainerRequestFilter.class);
85
86
87         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
88         for (Class filterClass : filters) {
89             if (filterClass.getAnnotation(Priority.class) == null) {
90                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
91             }
92         }
93
94         // Turn the set back into a list
95         List<Class<? extends ContainerRequestFilter>> filtersList = filters
96                 .stream()
97                 .filter(f -> {
98                     if (f.isAnnotationPresent(Profile.class)
99                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
100                         return false;
101                     }
102                     return true;
103                 })
104                 .collect(Collectors.toList());
105
106         // Sort them by their priority levels value
107         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
108
109         // Then register this to the jersey application
110         filtersList.forEach(this::register);
111     }
112
113     public void registerFiltersForResponses() {
114
115         // Find all the classes within the interceptors package
116         Reflections reflections = new Reflections("org.onap.aai.interceptors");
117         // Filter them based on the clazz that was passed in
118         Set<Class<? extends ContainerResponseFilter>> filters = reflections.getSubTypesOf(ContainerResponseFilter.class);
119
120
121         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
122         for (Class filterClass : filters) {
123             if (filterClass.getAnnotation(Priority.class) == null) {
124                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
125             }
126         }
127
128         // Turn the set back into a list
129         List<Class<? extends ContainerResponseFilter>> filtersList = filters.stream()
130                 .filter(f -> {
131                     if (f.isAnnotationPresent(Profile.class)
132                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
133                         return false;
134                     }
135                     return true;
136                 })
137                 .collect(Collectors.toList());
138
139         // Sort them by their priority levels value
140         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
141
142         // Then register this to the jersey application
143         filtersList.forEach(this::register);
144     }
145
146 }