2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.web;
22 import com.sun.jersey.api.client.filter.LoggingFilter;
23 import java.util.List;
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;
43 public class JerseyConfiguration extends ResourceConfig {
45 private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
47 private Environment env;
50 public JerseyConfiguration(Environment env) {
54 register(QueryConsumer.class);
55 register(AuditSqlDbConsumer.class);
56 register(EchoResponse.class);
59 registerFilters(ContainerRequestFilter.class);
60 registerFilters(ContainerResponseFilter.class);
61 registerFilters(AuditLogContainerFilter.class);
63 property(ServletProperties.FILTER_FORWARD_ON_404, true);
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));
72 public <T> void registerFilters(Class<T> type) {
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));
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");
89 // Turn the set back into a list
90 List<Class<? extends T>> filtersList = filters
93 if (f.isAnnotationPresent(Profile.class)
94 && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
99 .collect(Collectors.toList());
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()));
104 // Then register this to the jersey application
105 filtersList.forEach(this::register);