Update Janusgraph to 0.5.0 in graphadmin
[aai/graphadmin.git] / 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 com.sun.jersey.api.client.filter.LoggingFilter;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.logging.Logger;
26 import java.util.stream.Collectors;
27 import javax.annotation.Priority;
28 import javax.ws.rs.container.ContainerRequestFilter;
29 import javax.ws.rs.container.ContainerResponseFilter;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.glassfish.jersey.servlet.ServletProperties;
32 import org.onap.aai.rest.AuditSqlDbConsumer;
33 import org.onap.aai.rest.QueryConsumer;
34 import org.onap.aai.rest.util.EchoResponse;
35 import org.onap.logging.filter.base.AuditLogContainerFilter;
36 import org.reflections8.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 @Component
43 public class JerseyConfiguration extends ResourceConfig {
44
45     private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
46
47     private Environment env;
48
49     @Autowired
50     public JerseyConfiguration(Environment env) {
51
52         this.env = env;
53
54         register(QueryConsumer.class);
55         register(AuditSqlDbConsumer.class);
56         register(EchoResponse.class);
57
58         //Filters
59         registerFilters(ContainerRequestFilter.class);
60         registerFilters(ContainerResponseFilter.class);
61         registerFilters(AuditLogContainerFilter.class);
62
63         property(ServletProperties.FILTER_FORWARD_ON_404, true);
64
65         // Following registers the request headers and response headers
66         // If the LoggingFilter second argument is set to true, it will print response value as well
67         if ("true".equalsIgnoreCase(env.getProperty("aai.request.logging.enabled"))) {
68             register(new LoggingFilter(log, 0));
69         }
70     }
71
72     public <T> void registerFilters(Class<T> type) {
73
74         // Find all the classes within the interceptors package
75         // TODO: do not rely on the transitive reflections dependency of janusgraph-core
76         Reflections loggingReflections = new Reflections("org.onap.aai.aailog.filter");
77                 Reflections reflections = new Reflections("org.onap.aai.interceptors");
78                 // Filter them based on the clazz that was passed in
79         Set<Class<? extends T>> filters = loggingReflections.getSubTypesOf(type);
80         filters.addAll(reflections.getSubTypesOf(type));
81
82         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
83         for (Class filterClass : filters) {
84             if (filterClass.getAnnotation(Priority.class) == null) {
85                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
86             }
87         }
88
89         // Turn the set back into a list
90         List<Class<? extends T>> filtersList = filters
91                 .stream()
92                 .filter(f -> {
93                     if (f.isAnnotationPresent(Profile.class)
94                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
95                         return false;
96                     }
97                     return true;
98                 })
99                 .collect(Collectors.toList());
100
101         // Sort them by their priority levels value
102         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
103
104         // Then register this to the jersey application
105         filtersList.forEach(this::register);
106     }
107 }