Split up RESTCONF contexts
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / web / 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.nb.rfc8040.web;
9
10 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.DATA_SUBSCRIPTION;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.NOTIFICATION_STREAM;
12 import static org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants.BASE_URI_PATTERN;
13
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import javax.servlet.ServletException;
18 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
19 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
20 import org.opendaylight.aaa.web.FilterDetails;
21 import org.opendaylight.aaa.web.ServletDetails;
22 import org.opendaylight.aaa.web.WebContext;
23 import org.opendaylight.aaa.web.WebContextSecurer;
24 import org.opendaylight.aaa.web.WebServer;
25 import org.opendaylight.aaa.web.servlet.ServletSupport;
26 import org.opendaylight.restconf.nb.rfc8040.DataStreamApplication;
27 import org.opendaylight.restconf.nb.rfc8040.RestconfApplication;
28 import org.opendaylight.restconf.nb.rfc8040.RootFoundApplication;
29 import org.opendaylight.restconf.nb.rfc8040.streams.WebSocketInitializer;
30 import org.opendaylight.yangtools.concepts.Registration;
31
32 /**
33  * Initializes the rfc8040 web app endpoint.
34  *
35  * @author Thomas Pantelis
36  */
37 @Singleton
38 public final class WebInitializer implements AutoCloseable {
39     private final Registration discoveryReg;
40     private final Registration restconfReg;
41
42     @Inject
43     public WebInitializer(final WebServer webServer, final WebContextSecurer webContextSecurer,
44             final ServletSupport servletSupport, final RestconfApplication webApp,
45             final DataStreamApplication webAppNotif,
46             final CustomFilterAdapterConfiguration customFilterAdapterConfig,
47             final WebSocketInitializer webSocketServlet) throws ServletException {
48         final var restconfBuilder = WebContext.builder()
49             .contextPath("/" + BASE_URI_PATTERN)
50             .supportsSessions(false)
51             .addServlet(ServletDetails.builder()
52                 .addUrlPattern("/*")
53                 .servlet(servletSupport.createHttpServletBuilder(webApp).build())
54                 .asyncSupported(true)
55                 .build())
56             .addServlet(ServletDetails.builder()
57                 .addUrlPattern("/notif/*")
58                 .servlet(servletSupport.createHttpServletBuilder(webAppNotif).build())
59                 .name("notificationServlet")
60                 .asyncSupported(true)
61                 .build())
62             .addServlet(ServletDetails.builder()
63                 .addUrlPattern("/" + DATA_SUBSCRIPTION + "/*")
64                 .addUrlPattern("/" + NOTIFICATION_STREAM + "/*")
65                 .servlet(webSocketServlet)
66                 .build())
67
68             // Allows user to add javax.servlet.Filter(s) in front of REST services
69             .addFilter(FilterDetails.builder()
70                 .addUrlPattern("/*")
71                 .filter(new CustomFilterAdapter(customFilterAdapterConfig))
72                 .asyncSupported(true)
73                 .build());
74
75         webContextSecurer.requireAuthentication(restconfBuilder, true, "/*");
76
77         restconfReg = webServer.registerWebContext(restconfBuilder.build());
78
79         final var discoveryBuilder = WebContext.builder()
80             .contextPath("/.well-known")
81             .supportsSessions(false)
82             .addServlet(ServletDetails.builder()
83                 .addUrlPattern("/*")
84                 .servlet(servletSupport.createHttpServletBuilder(new RootFoundApplication(BASE_URI_PATTERN)).build())
85                 .name("Rootfound")
86                 .build());
87
88         webContextSecurer.requireAuthentication(discoveryBuilder, true, "/*");
89
90         discoveryReg = webServer.registerWebContext(discoveryBuilder.build());
91     }
92
93     @PreDestroy
94     @Override
95     public void close() {
96         discoveryReg.close();
97         restconfReg.close();
98     }
99 }