182203dbe4e9164619dbfa2db26b1ede11d992dd
[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 java.util.List;
23 import java.util.Set;
24 import java.util.logging.Logger;
25 import java.util.stream.Collectors;
26 import javax.annotation.Priority;
27 import javax.ws.rs.container.ContainerRequestFilter;
28 import javax.ws.rs.container.ContainerResponseFilter;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.onap.aai.schemaservice.edges.EdgeResource;
31 import org.onap.aai.schemaservice.healthcheck.EchoResource;
32 import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource;
33 import org.onap.aai.schemaservice.query.QueryResource;
34 import org.onap.aai.schemaservice.versions.VersionResource;
35 import org.onap.logging.filter.base.AuditLogContainerFilter;
36 import org.reflections.Reflections;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.core.env.Environment;
40 import org.springframework.stereotype.Component;
41
42
43 @Component
44 public class JerseyConfiguration extends ResourceConfig {
45
46     private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
47
48     private Environment env;
49
50     @Autowired
51     public JerseyConfiguration(Environment env) {
52
53         this.env = env;
54
55         register(VersionResource.class);
56         register(EchoResource.class);
57         register(NodeSchemaResource.class);
58         register(QueryResource.class);
59         register(EdgeResource.class);
60
61         //Request Filters
62         registerFilters(ContainerRequestFilter.class);
63         registerFilters(ContainerResponseFilter.class);
64         registerFilters(AuditLogContainerFilter.class);
65
66     }
67
68     public <T> void registerFilters(Class<T> type) {
69
70         Reflections loggingReflections = new Reflections("org.onap.aai.aailog.filter");
71         Reflections reflections = new Reflections("org.onap.aai.schemaservice.interceptors");
72         // Filter them based on the clazz that was passed in
73         Set<Class<? extends T>> filters = loggingReflections.getSubTypesOf(type);
74         filters.addAll(reflections.getSubTypesOf(type));
75
76         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
77         for (Class filterClass : filters) {
78             if (filterClass.getAnnotation(Priority.class) == null) {
79                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
80             }
81         }
82
83         // Turn the set back into a list
84         List<Class<? extends T>> filtersList = filters
85                 .stream()
86                 .filter(f -> {
87                     if (f.isAnnotationPresent(Profile.class)
88                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
89                         return false;
90                     }
91                     return true;
92                 })
93                 .collect(Collectors.toList());
94
95         // Sort them by their priority levels value
96         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
97
98         // Then register this to the jersey application
99         filtersList.forEach(this::register);
100     }
101
102
103 }