Reduce exception guard
[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.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
16 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
17 import org.opendaylight.aaa.web.FilterDetails;
18 import org.opendaylight.aaa.web.ServletDetails;
19 import org.opendaylight.aaa.web.WebContext;
20 import org.opendaylight.aaa.web.WebContextBuilder;
21 import org.opendaylight.aaa.web.WebContextSecurer;
22 import org.opendaylight.aaa.web.WebServer;
23 import org.opendaylight.aaa.web.servlet.ServletSupport;
24 import org.opendaylight.restconf.nb.rfc8040.DataStreamApplication;
25 import org.opendaylight.restconf.nb.rfc8040.RestconfApplication;
26 import org.opendaylight.restconf.nb.rfc8040.RootFoundApplication;
27 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
28 import org.opendaylight.restconf.nb.rfc8040.streams.WebSocketInitializer;
29 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
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 class WebInitializer {
39     private final Registration registration;
40
41     @Inject
42     public WebInitializer(final WebServer webServer, final WebContextSecurer webContextSecurer,
43             final ServletSupport servletSupport, final RestconfApplication webApp,
44             final DataStreamApplication webAppNotif,
45             final CustomFilterAdapterConfiguration customFilterAdapterConfig,
46             final WebSocketInitializer webSocketServlet) throws ServletException {
47         WebContextBuilder webContextBuilder = WebContext.builder()
48             .contextPath("/")
49             .supportsSessions(false)
50             .addServlet(ServletDetails.builder()
51                 .addUrlPattern(RestconfConstants.BASE_URI_PATTERN + "/*")
52                 .servlet(servletSupport.createHttpServletBuilder(webApp).build())
53                 .asyncSupported(true)
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_SUBSCRIPTION + "/*",
64                     RestconfConstants.BASE_URI_PATTERN + "/" + RestconfStreamsConstants.NOTIFICATION_STREAM + "/*"))
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 }