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