Update to spring boot 2
[aai/resources.git] / aai-resources / 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 static java.lang.Boolean.parseBoolean;
23 import static java.util.Comparator.comparingInt;
24
25 import com.google.common.collect.Sets;
26 import java.lang.reflect.AnnotatedElement;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.Set;
30 import java.util.logging.Logger;
31 import javax.annotation.Priority;
32 import javax.ws.rs.container.ContainerRequestFilter;
33 import javax.ws.rs.container.ContainerResponseFilter;
34 import com.sun.jersey.api.client.filter.LoggingFilter;
35 import org.glassfish.jersey.server.ResourceConfig;
36 import org.onap.aai.rest.BulkAddConsumer;
37 import org.onap.aai.rest.BulkProcessConsumer;
38 import org.onap.aai.rest.ExampleConsumer;
39 import org.onap.aai.rest.LegacyMoxyConsumer;
40 import org.onap.aai.rest.URLFromVertexIdConsumer;
41 import org.onap.aai.rest.VertexIdConsumer;
42 import org.onap.aai.rest.bulk.BulkSingleTransactionConsumer;
43 import org.onap.aai.rest.util.EchoResponse;
44 import org.onap.logging.filter.base.AuditLogContainerFilter;
45 import org.reflections.Reflections;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.context.annotation.Configuration;
49 import org.springframework.context.annotation.Profile;
50 import org.springframework.core.env.Environment;
51
52 @Configuration
53 public class JerseyConfiguration {
54
55     private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
56
57     private static final String LOGGING_ENABLED_PROPERTY = "aai.request.logging.enabled";
58     private static final String INTERCEPTOR_PACKAGE = "org.onap.aai.interceptors";
59     private static final String LOGGING_INTERCEPTOR_PACKAGE = "org.onap.aai.aailog.filter";
60     private static final boolean ENABLE_RESPONSE_LOGGING = false;
61
62     private final Reflections reflections = new Reflections(INTERCEPTOR_PACKAGE);
63     private final Reflections loggingReflections = new Reflections(LOGGING_INTERCEPTOR_PACKAGE);
64     private final Environment environment;
65
66     @Autowired
67     public JerseyConfiguration(Environment environment) {
68         this.environment = environment;
69     }
70
71     @Bean
72     public ResourceConfig resourceConfig() {
73         ResourceConfig resourceConfig = new ResourceConfig();
74
75         Set<Class<?>> classes = Sets.newHashSet(
76             EchoResponse.class,
77             VertexIdConsumer.class,
78             ExampleConsumer.class,
79             BulkAddConsumer.class,
80             BulkProcessConsumer.class,
81             BulkSingleTransactionConsumer.class,
82             LegacyMoxyConsumer.class,
83             URLFromVertexIdConsumer.class
84         );
85         resourceConfig.registerClasses(classes);
86         registerFiltersForClasses(resourceConfig, ContainerRequestFilter.class, ContainerResponseFilter.class, AuditLogContainerFilter.class);
87
88         if (isLoggingEnabled()) {
89             logRequests(resourceConfig);
90         }
91
92         return resourceConfig;
93     }
94
95     private void registerFiltersForClasses(ResourceConfig resourceConfig, Class<?>... classes) {
96         for (Class<?> clazz : classes) {
97             registerFiltersFor(clazz, resourceConfig);
98         }
99     }
100
101     private <T> void registerFiltersFor(Class<T> clazz, ResourceConfig resourceConfig) {
102         Set<Class<? extends T>> filters = loggingReflections.getSubTypesOf(clazz);
103         filters.addAll(reflections.getSubTypesOf(clazz));
104         throwIfPriorityAnnotationAbsent(filters);
105
106         filters.stream()
107             .filter(this::isEnabledByActiveProfiles)
108             .sorted(priorityComparator())
109             .forEach(resourceConfig::register);
110     }
111
112     private <T> void throwIfPriorityAnnotationAbsent(Collection<Class<? extends T>> classes) {
113         for (Class clazz : classes) {
114             if (!clazz.isAnnotationPresent(Priority.class)) {
115                 throw new MissingFilterPriorityException(clazz);
116             }
117         }
118     }
119
120     private <T> Comparator<Class<? extends T>> priorityComparator() {
121         return comparingInt(clazz -> clazz.getAnnotation(Priority.class).value());
122     }
123
124     private void logRequests(ResourceConfig resourceConfig) {
125         resourceConfig.register(new LoggingFilter(log, ENABLE_RESPONSE_LOGGING ? null : 0));
126     }
127
128     private boolean isLoggingEnabled() {
129         return parseBoolean(environment.getProperty(LOGGING_ENABLED_PROPERTY));
130     }
131
132     private boolean isEnabledByActiveProfiles(AnnotatedElement annotatedElement) {
133         return !annotatedElement.isAnnotationPresent(Profile.class) ||
134             environment.acceptsProfiles(annotatedElement.getAnnotation(Profile.class).value());
135     }
136
137     private class MissingFilterPriorityException extends RuntimeException {
138         private MissingFilterPriorityException(Class<?> clazz) {
139             super("Container filter " + clazz.getName() + " does not have @Priority annotation");
140         }
141     }
142 }