3f9db33a823f41752d069ef80b297b008bf08191
[clamp.git] / src / main / java / org / onap / clamp / clds / service / JaxrsApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * 
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.function.Function;
34 import java.util.stream.Collectors;
35
36 import javax.ws.rs.ApplicationPath;
37 import javax.ws.rs.core.Application;
38
39 import org.springframework.beans.factory.config.BeanDefinition;
40 import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
41 import org.springframework.core.type.filter.AnnotationTypeFilter;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 @ApplicationPath("/restservices/clds/v1")
46 public class JaxrsApplication extends Application {
47
48     private static final EELFLogger logger = EELFManager.getInstance().getLogger(JaxrsApplication.class);
49     private Function<BeanDefinition, Optional<Class<?>>> beanDefinitionToClass = b -> {
50         try {
51             return Optional.of(Class.forName(b.getBeanClassName()));
52         } catch (ClassNotFoundException e) {
53             logger.error("Could not get class annotated with @Path for swagger documentation generation", e);
54             return Optional.empty();
55         }
56     };
57
58     @Override
59     public Set<Class<?>> getClasses() {
60         Set<Class<?>> resources = new HashSet<>();
61         resources.add(JacksonObjectMapperProvider.class);
62         resources.add(io.swagger.v3.jaxrs2.integration.resources.OpenApiResource.class);
63         resources.addAll(scan());
64         return resources;
65     }
66
67     private List<Class<?>> scan() {
68         ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
69         scanner.addIncludeFilter(new AnnotationTypeFilter(javax.ws.rs.Path.class));
70         return scanner.findCandidateComponents("org.onap.clamp.clds").stream().map(beanDefinitionToClass)
71                 .filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
72     }
73 }