Add the queries endpoint and files
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / 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.schemaservice.web;
21
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.onap.aai.schemaservice.edges.EdgeResource;
24 import org.onap.aai.schemaservice.healthcheck.EchoResource;
25 import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource;
26 import org.onap.aai.schemaservice.query.QueryResource;
27 import org.onap.aai.schemaservice.versions.VersionResource;
28 import org.reflections.Reflections;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Profile;
31 import org.springframework.core.env.Environment;
32 import org.springframework.stereotype.Component;
33
34 import javax.annotation.Priority;
35 import javax.ws.rs.container.ContainerRequestFilter;
36 import javax.ws.rs.container.ContainerResponseFilter;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.logging.Logger;
40 import java.util.stream.Collectors;
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(VersionResource.class);
55         register(EchoResource.class);
56         register(NodeSchemaResource.class);
57         register(QueryResource.class);
58         register(EdgeResource.class);
59
60         //Request Filters
61         registerFiltersForRequests();
62         // Response Filters
63         registerFiltersForResponses();
64
65     }
66
67     public void registerFiltersForRequests() {
68
69         // Find all the classes within the interceptors package
70         Reflections reflections = new Reflections("org.onap.aai.schemaservice.interceptors");
71         // Filter them based on the clazz that was passed in
72         Set<Class<? extends ContainerRequestFilter>> filters = reflections.getSubTypesOf(ContainerRequestFilter.class);
73
74
75         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
76         for (Class filterClass : filters) {
77             if (filterClass.getAnnotation(Priority.class) == null) {
78                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
79             }
80         }
81
82         // Turn the set back into a list
83         List<Class<? extends ContainerRequestFilter>> filtersList = filters
84                 .stream()
85                 .filter(f -> {
86                     if (f.isAnnotationPresent(Profile.class)
87                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
88                         return false;
89                     }
90                     return true;
91                 })
92                 .collect(Collectors.toList());
93
94         // Sort them by their priority levels value
95         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
96
97         // Then register this to the jersey application
98         filtersList.forEach(this::register);
99     }
100
101     public void registerFiltersForResponses() {
102
103         // Find all the classes within the interceptors package
104         Reflections reflections = new Reflections("org.onap.aai.schemaservice.interceptors");
105         // Filter them based on the clazz that was passed in
106         Set<Class<? extends ContainerResponseFilter>> filters = reflections.getSubTypesOf(ContainerResponseFilter.class);
107
108
109         // Check to ensure that each of the filter has the @Priority annotation and if not throw exception
110         for (Class filterClass : filters) {
111             if (filterClass.getAnnotation(Priority.class) == null) {
112                 throw new RuntimeException("Container filter " + filterClass.getName() + " does not have @Priority annotation");
113             }
114         }
115
116         // Turn the set back into a list
117         List<Class<? extends ContainerResponseFilter>> filtersList = filters.stream()
118                 .filter(f -> {
119                     if (f.isAnnotationPresent(Profile.class)
120                             && !env.acceptsProfiles(f.getAnnotation(Profile.class).value())) {
121                         return false;
122                     }
123                     return true;
124                 })
125                 .collect(Collectors.toList());
126
127         // Sort them by their priority levels value
128         filtersList.sort((c1, c2) -> Integer.valueOf(c1.getAnnotation(Priority.class).value()).compareTo(c2.getAnnotation(Priority.class).value()));
129
130         // Then register this to the jersey application
131         filtersList.forEach(this::register);
132     }
133 }