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