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 org.glassfish.jersey.filter.LoggingFilter;
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.servlet.ServletProperties;
25 import org.onap.aai.rest.QueryConsumer;
26 import org.onap.aai.rest.retired.V3ThroughV7Consumer;
27 import org.onap.aai.rest.search.ModelAndNamedQueryRestProvider;
28 import org.onap.aai.rest.search.SearchProvider;
29 import org.onap.aai.rest.util.EchoResponse;
30 import org.reflections.Reflections;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.core.env.Environment;
34 import org.springframework.stereotype.Component;
36 import javax.annotation.Priority;
37 import javax.ws.rs.ApplicationPath;
38 import javax.ws.rs.container.ContainerRequestFilter;
39 import javax.ws.rs.container.ContainerResponseFilter;
40 import java.util.List;
42 import java.util.logging.Logger;
43 import java.util.stream.Collectors;
46 @ApplicationPath("/aai")
47 public class JerseyConfiguration extends ResourceConfig {
49 private static final Logger log = Logger.getLogger(JerseyConfiguration.class.getName());
51 private Environment env;
54 public JerseyConfiguration(Environment env) {
58 register(SearchProvider.class);
59 register(ModelAndNamedQueryRestProvider.class);
60 register(QueryConsumer.class);
62 register(V3ThroughV7Consumer.class);
63 register(EchoResponse.class);
66 registerFiltersForRequests();
68 registerFiltersForResponses();
70 property(ServletProperties.FILTER_FORWARD_ON_404, true);
72 // Following registers the request headers and response headers
73 // If the LoggingFilter second argument is set to true, it will print response value as well
74 if ("true".equalsIgnoreCase(env.getProperty("aai.request.logging.enabled"))) {
75 register(new LoggingFilter(log, false));
79 public void registerFiltersForRequests() {
81 // Find all the classes within the interceptors package
82 Reflections reflections = new Reflections("org.onap.aai.interceptors");
83 // Filter them based on the clazz that was passed in
84 Set<Class<? extends ContainerRequestFilter>> filters = reflections.getSubTypesOf(ContainerRequestFilter.class);
87 // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
88 for (Class filterClass : filters) {
89 if (filterClass.getAnnotation(Priority.class) == null) {
90 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
94 // Turn the set back into a list
95 List<Class<? extends ContainerRequestFilter>> filtersList = filters
98 if (f.isAnnotationPresent(Profile.class)
99 && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
104 .collect(Collectors.toList());
106 // Sort them by their priority levels value
107 filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
109 // Then register this to the jersey application
110 filtersList.forEach(this::register);
113 public void registerFiltersForResponses() {
115 // Find all the classes within the interceptors package
116 Reflections reflections = new Reflections("org.onap.aai.interceptors");
117 // Filter them based on the clazz that was passed in
118 Set<Class<? extends ContainerResponseFilter>> filters = reflections.getSubTypesOf(ContainerResponseFilter.class);
121 // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
122 for (Class filterClass : filters) {
123 if (filterClass.getAnnotation(Priority.class) == null) {
124 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
128 // Turn the set back into a list
129 List<Class<? extends ContainerResponseFilter>> filtersList = filters.stream()
131 if (f.isAnnotationPresent(Profile.class)
132 && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
137 .collect(Collectors.toList());
139 // Sort them by their priority levels value
140 filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
142 // Then register this to the jersey application
143 filtersList.forEach(this::register);