179c488da4d4d774f67e12c8b6dfecb23274bb8b
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / yanglib / impl / 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.yanglib.impl;
9
10 import javax.servlet.ServletException;
11 import javax.ws.rs.core.Application;
12 import org.opendaylight.aaa.web.ServletDetails;
13 import org.opendaylight.aaa.web.WebContext;
14 import org.opendaylight.aaa.web.WebContextSecurer;
15 import org.opendaylight.aaa.web.WebServer;
16 import org.opendaylight.aaa.web.servlet.ServletSupport;
17 import org.opendaylight.yangtools.concepts.Registration;
18
19 /**
20  * Initializes the wep app.
21  *
22  * @author Thomas Pantelis
23  */
24 public final class WebInitializer implements AutoCloseable {
25     private final Registration registration;
26
27     public WebInitializer(final WebServer webServer,  final WebContextSecurer webContextSecurer,
28             final ServletSupport servletSupport, final Application webApp) throws ServletException {
29         final var webContextBuilder = WebContext.builder()
30             .name("RFC8525 YANG Library")
31             .contextPath("/yanglib")
32             .supportsSessions(true)
33             .addServlet(ServletDetails.builder()
34                 .servlet(servletSupport.createHttpServletBuilder(webApp).build())
35                 .addUrlPattern("/*")
36                 .build());
37
38         webContextSecurer.requireAuthentication(webContextBuilder, "/*");
39
40         registration = webServer.registerWebContext(webContextBuilder.build());
41     }
42
43     @Override
44     public void close() {
45         registration.close();
46     }
47 }