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.schemaservice.web;
22 import java.util.List;
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;
44 public class JerseyConfiguration extends ResourceConfig {
46 private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
48 private Environment env;
51 public JerseyConfiguration(Environment env) {
55 register(VersionResource.class);
56 register(EchoResource.class);
57 register(NodeSchemaResource.class);
58 register(QueryResource.class);
59 register(EdgeResource.class);
62 registerFilters(ContainerRequestFilter.class);
63 registerFilters(ContainerResponseFilter.class);
64 registerFilters(AuditLogContainerFilter.class);
68 public <T> void registerFilters(Class<T> type) {
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));
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");
83 // Turn the set back into a list
84 List<Class<? extends T>> filtersList = filters
87 if (f.isAnnotationPresent(Profile.class)
88 && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
93 .collect(Collectors.toList());
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()));
98 // Then register this to the jersey application
99 filtersList.forEach(this::register);