Eliminate OpenApiApplication class
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / jaxrs / WebInitializer.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.openapi.jaxrs;
9
10 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
11 import java.util.Set;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.servlet.ServletException;
16 import javax.ws.rs.core.Application;
17 import org.opendaylight.aaa.web.ResourceDetails;
18 import org.opendaylight.aaa.web.ServletDetails;
19 import org.opendaylight.aaa.web.WebContext;
20 import org.opendaylight.aaa.web.WebContextSecurer;
21 import org.opendaylight.aaa.web.WebServer;
22 import org.opendaylight.aaa.web.servlet.ServletSupport;
23 import org.opendaylight.restconf.openapi.api.OpenApiService;
24 import org.opendaylight.yangtools.concepts.Registration;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Deactivate;
28 import org.osgi.service.component.annotations.Reference;
29
30 /**
31  * Initializes the wep app.
32  *
33  * @author Thomas Pantelis
34  */
35 @Singleton
36 @Component(service = { })
37 public final class WebInitializer implements AutoCloseable {
38     private final Registration registration;
39
40     @Inject
41     @Activate
42     public WebInitializer(@Reference final WebServer webServer, @Reference final WebContextSecurer webContextSecurer,
43             @Reference final ServletSupport servletSupport, @Reference final OpenApiService openApiService)
44                 throws ServletException {
45         final var webContextBuilder = WebContext.builder()
46             .name("OpenAPI")
47             .contextPath("/openapi")
48             .supportsSessions(true)
49             .addServlet(ServletDetails.builder()
50                 .servlet(servletSupport.createHttpServletBuilder(new Application() {
51                     @Override
52                     public Set<Object> getSingletons() {
53                         return Set.of(openApiService, new JacksonJaxbJsonProvider());
54                     }
55                 }).build())
56                 .addUrlPattern("/api/v3/*")
57                 .build())
58             .addResource(ResourceDetails.builder().name("/explorer").build());
59
60         webContextSecurer.requireAuthentication(webContextBuilder, "/*");
61
62         registration = webServer.registerWebContext(webContextBuilder.build());
63     }
64
65     @Override
66     @Deactivate
67     @PreDestroy
68     public void close() {
69         registration.close();
70     }
71 }