Add descriptive name for /auth
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / web / env / WebInitializer.java
1 /*
2  * Copyright (c) 2018 Red Hat, Inc. 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.aaa.shiro.web.env;
9
10 import javax.annotation.PreDestroy;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import javax.servlet.ServletException;
14 import org.opendaylight.aaa.api.ClaimCache;
15 import org.opendaylight.aaa.api.IIDMStore;
16 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
17 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
18 import org.opendaylight.aaa.shiro.idm.IdmLightApplication;
19 import org.opendaylight.aaa.web.FilterDetails;
20 import org.opendaylight.aaa.web.ServletDetails;
21 import org.opendaylight.aaa.web.WebContext;
22 import org.opendaylight.aaa.web.WebContextSecurer;
23 import org.opendaylight.aaa.web.WebServer;
24 import org.opendaylight.aaa.web.servlet.ServletSupport;
25 import org.opendaylight.yangtools.concepts.Registration;
26
27 /**
28  * Initializer for web components.
29  * This class is the equivalent of a declarative web.xml,
30  * and is not OSGi specific; it can also be used e.g. in standalone
31  * Java environments, such as tests.
32  *
33  * @author Michael Vorburger.ch
34  */
35 @Singleton
36 public class WebInitializer {
37     private final Registration registraton;
38
39     @Inject
40     public WebInitializer(final WebServer webServer, final ClaimCache claimCache, final IIDMStore iidMStore,
41             final WebContextSecurer webContextSecurer, final ServletSupport servletSupport,
42             final CustomFilterAdapterConfiguration customFilterAdapterConfig) throws ServletException {
43
44         final var webContextBuilder = WebContext.builder()
45             .name("OpenDaylight IDM realm management")
46             .contextPath("/auth")
47             .supportsSessions(true)
48
49             .addServlet(ServletDetails.builder()
50                 .servlet(servletSupport.createHttpServletBuilder(new IdmLightApplication(iidMStore, claimCache))
51                     .build())
52                 .addUrlPattern("/*")
53                 .build())
54
55             // Allows user to add javax.servlet.Filter(s) in front of REST services
56             .addFilter(FilterDetails.builder()
57                 .filter(new CustomFilterAdapter(customFilterAdapterConfig))
58                 .addUrlPattern("/*")
59                 .build());
60
61         webContextSecurer.requireAuthentication(webContextBuilder, "/*", "/moon/*");
62
63         registraton = webServer.registerWebContext(webContextBuilder.build());
64     }
65
66     @PreDestroy
67     public void close() {
68         registraton.close();
69     }
70 }