Add swagger generation for jaxrs api
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 import javax.ws.rs.ApplicationPath;
36 import javax.ws.rs.core.Application;
37
38 import org.springframework.beans.factory.config.BeanDefinition;
39 import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
40 import org.springframework.core.type.filter.AnnotationTypeFilter;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 @ApplicationPath("/restservices/clds/v1")
45 public class JaxrsApplication extends Application {
46
47     private static final EELFLogger logger = EELFManager.getInstance().getLogger(JaxrsApplication.class);
48
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(io.swagger.v3.jaxrs2.integration.resources.OpenApiResource.class);
62         resources.addAll(scan());
63         return resources;
64     }
65
66     private List<Class<?>> scan() {
67         ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
68         scanner.addIncludeFilter(new AnnotationTypeFilter(javax.ws.rs.Path.class));
69         return scanner.findCandidateComponents("org.onap.clamp.clds").stream()
70                 .map(beanDefinitionToClass)
71                 .filter(Optional::isPresent)
72                 .map(Optional::get)
73                 .collect(Collectors.toList());
74     }
75
76 }