826cf4d92b45d5133ce0faf3b7de24b358f54059
[netconf.git] / restconf / restconf-nb-rfc8040 / 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 java.util.List;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import javax.servlet.ServletException;
15 import org.apache.aries.blueprint.annotation.service.Reference;
16 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
17 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
18 import org.opendaylight.aaa.web.FilterDetails;
19 import org.opendaylight.aaa.web.ServletDetails;
20 import org.opendaylight.aaa.web.WebContext;
21 import org.opendaylight.aaa.web.WebContextBuilder;
22 import org.opendaylight.aaa.web.WebContextRegistration;
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.rests.utils.RestconfStreamsConstants;
30 import org.opendaylight.restconf.nb.rfc8040.streams.websockets.WebSocketInitializer;
31 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
32
33 /**
34  * Initializes the rfc8040 web app endpoint.
35  *
36  * @author Thomas Pantelis
37  */
38 @Singleton
39 public class WebInitializer {
40     private final WebContextRegistration registration;
41
42     @Inject
43     public WebInitializer(@Reference final WebServer webServer, @Reference final WebContextSecurer webContextSecurer,
44             @Reference final ServletSupport servletSupport, final RestconfApplication webApp,
45             final DataStreamApplication webAppNotif,
46             @Reference final CustomFilterAdapterConfiguration customFilterAdapterConfig,
47             final WebSocketInitializer webSocketServlet) throws ServletException {
48         WebContextBuilder webContextBuilder = WebContext.builder()
49             .contextPath("/")
50             .supportsSessions(false)
51             .addServlet(ServletDetails.builder()
52                 .addUrlPattern(RestconfConstants.BASE_URI_PATTERN + "/*")
53                 .servlet(servletSupport.createHttpServletBuilder(webApp).build())
54                 .build())
55             .addServlet(ServletDetails.builder()
56                 .addUrlPattern(RestconfConstants.BASE_URI_PATTERN + "/notif/*")
57                 .servlet(servletSupport.createHttpServletBuilder(webAppNotif).build())
58                 .name("notificationServlet")
59                 .asyncSupported(true)
60                 .build())
61             .addServlet(ServletDetails.builder()
62                 .addAllUrlPatterns(List.of(
63                     RestconfConstants.BASE_URI_PATTERN + RestconfStreamsConstants.DATA_CHANGE_EVENT_STREAM_PATTERN,
64                     RestconfConstants.BASE_URI_PATTERN + RestconfStreamsConstants.YANG_NOTIFICATION_STREAM_PATTERN))
65                 .servlet(webSocketServlet)
66                 .build())
67             .addServlet(ServletDetails.builder()
68                 .addUrlPattern(".well-known/*")
69                 .servlet(servletSupport.createHttpServletBuilder(
70                     new RootFoundApplication(RestconfConstants.BASE_URI_PATTERN))
71                     .build())
72                 .name("Rootfound")
73                 .build())
74
75             // Allows user to add javax.servlet.Filter(s) in front of REST services
76             .addFilter(FilterDetails.builder()
77                 .addUrlPattern("/*")
78                 .filter(new CustomFilterAdapter(customFilterAdapterConfig))
79                 .asyncSupported(true)
80                 .build());
81
82         webContextSecurer.requireAuthentication(webContextBuilder, true, RestconfConstants.BASE_URI_PATTERN + "/*");
83
84         registration = webServer.registerWebContext(webContextBuilder.build());
85     }
86
87     @PreDestroy
88     public void close() {
89         if (registration != null) {
90             registration.close();
91         }
92     }
93 }