Convert web-impl-osgi to WhiteBoard
[aaa.git] / web / impl-osgi / src / main / java / org / opendaylight / aaa / web / osgi / WhiteboardWebServer.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.web.osgi;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.stream.Collectors;
17 import javax.servlet.Filter;
18 import javax.servlet.Servlet;
19 import javax.servlet.ServletContextListener;
20 import javax.servlet.ServletException;
21 import org.opendaylight.aaa.web.FilterDetails;
22 import org.opendaylight.aaa.web.ResourceDetails;
23 import org.opendaylight.aaa.web.ServletDetails;
24 import org.opendaylight.aaa.web.WebContext;
25 import org.opendaylight.aaa.web.WebServer;
26 import org.opendaylight.yangtools.concepts.AbstractRegistration;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.osgi.service.component.ComponentContext;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Deactivate;
35 import org.osgi.service.component.annotations.Reference;
36 import org.osgi.service.component.annotations.ServiceScope;
37 import org.osgi.service.http.context.ServletContextHelper;
38 import org.osgi.service.http.runtime.HttpServiceRuntime;
39 import org.osgi.service.http.runtime.HttpServiceRuntimeConstants;
40 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
41 import org.osgi.service.http.whiteboard.annotations.RequireHttpWhiteboard;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * {@link WebServer} implementation based on
47  * <a href="https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html">OSGi HTTP Whiteboard</a>.
48  */
49 @RequireHttpWhiteboard
50 @Component(scope = ServiceScope.BUNDLE)
51 public final class WhiteboardWebServer implements WebServer {
52     private static final Logger LOG = LoggerFactory.getLogger(WhiteboardWebServer.class);
53
54     private final Bundle bundle;
55     @Reference
56     private volatile ServiceReference<HttpServiceRuntime> serviceRuntime;
57
58     /**
59      * Construct a {@link WhiteboardWebServer} to a {@link ComponentContext}.
60      *
61      * @param componentContext A {@link ComponentContext}
62      */
63     @Activate
64     public WhiteboardWebServer(final ComponentContext componentContext) {
65         bundle = componentContext.getUsingBundle();
66         LOG.debug("Activated WebServer for bundle {}", bundle);
67     }
68
69     @Deactivate
70     void deactivate() {
71         LOG.debug("Deactivated WebServer for bundle {}", bundle);
72     }
73
74     @Override
75     public String getBaseURL() {
76         final var endpoint = serviceRuntime.getProperty(HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT);
77         if (endpoint instanceof String) {
78             return (String) endpoint;
79         } else if (endpoint instanceof String[]) {
80             return getBaseURL(Arrays.asList((String[]) endpoint));
81         } else if (endpoint instanceof Collection) {
82             // Safe as per OSGi Compendium R7 section 140.15.3.1
83             @SuppressWarnings("unchecked")
84             final var cast = (Collection<String>) endpoint;
85             return getBaseURL(cast);
86         } else {
87             throw new IllegalStateException("Unhandled endpoint " + endpoint);
88         }
89     }
90
91     private static String getBaseURL(final Collection<String> endpoints) {
92         for (var endpoint : endpoints) {
93             if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
94                 return endpoint;
95             }
96         }
97         throw new IllegalStateException("Cannot select base URL from " + endpoints);
98     }
99
100     @Override
101     public Registration registerWebContext(final WebContext webContext) throws ServletException {
102         final var bundleContext = bundle.getBundleContext();
103         final var builder = ImmutableList.<ServiceRegistration<?>>builder();
104
105         // The order in which we set things up here matters...
106
107         // 1. ServletContextHelper, to which all others are bound to
108         final var contextPath = absolutePath(webContext.contextPath());
109         // TODO: can we create a better name?
110         final var contextName = contextPath + ".id";
111
112         final var contextProps = contextProperties(contextName, contextPath, webContext.contextParams());
113         LOG.debug("Registering context {} with properties {}", contextName, contextProps);
114         builder.add(bundleContext.registerService(ServletContextHelper.class,
115             new WhiteboardServletContextHelper(bundle), new MapDictionary<>(contextProps)));
116
117         // 2. Listeners - because they could set up things that filters and servlets need
118         final var contextSelect = "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + contextName + ")";
119         for (var listener : webContext.listeners()) {
120             final var props = Map.of(
121                 HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, contextSelect,
122                 HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, Boolean.TRUE);
123             LOG.debug("Registering listener {} with properties {}", listener, props);
124             builder.add(bundleContext.registerService(ServletContextListener.class, listener,
125                 new MapDictionary<>(props)));
126         }
127
128         // 3. Filters - because subsequent servlets should already be covered by the filters
129         for (var filter : webContext.filters()) {
130             final var props = filterProperties(contextPath, contextSelect, filter);
131             LOG.debug("Registering filter {} with properties {}", filter, props);
132             builder.add(bundleContext.registerService(Filter.class, filter.filter(), new MapDictionary<>(props)));
133         }
134
135         // 4. Servlets - 'bout time for 'em by now, don't you think? ;)
136         for (var servlet : webContext.servlets()) {
137             final var props = servletProperties(contextPath, contextSelect, servlet);
138             LOG.debug("Registering servlet {} with properties {}", servlet, props);
139             builder.add(bundleContext.registerService(Servlet.class, servlet.servlet(), new MapDictionary<>(props)));
140         }
141
142         // 5. Resources
143         for (var resource : webContext.resources()) {
144             final var props = resourceProperties(contextPath, contextSelect, resource);
145             LOG.debug("Registering resource {} with properties {}", resource, props);
146             builder.add(bundleContext.registerService(Object.class, WhiteboardResource.INSTANCE,
147                 new MapDictionary<>(props)));
148         }
149
150         final var services = builder.build();
151         LOG.info("Bundle {} registered context path {} with {} service(s)", bundle, contextPath, services.size());
152         return new AbstractRegistration() {
153             @Override
154             protected void removeRegistration() {
155                 // The order does not have to be reversed: we unregister ServletContextHelper first, hence everybody
156                 // becomes unbound
157                 services.forEach(ServiceRegistration::unregister);
158             }
159         };
160     }
161
162     private static Map<String, Object> contextProperties(final String contextName, final String contextPath,
163             final Map<String, String> params) {
164         final var builder = ImmutableMap.<String, Object>builder()
165             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, contextName)
166             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, contextPath);
167
168         for (var e : params.entrySet()) {
169             builder.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + e.getKey(), e.getValue());
170         }
171
172         return builder.build();
173     }
174
175     private static Map<String, Object> filterProperties(final String contextPath, final String contextSelect,
176             final FilterDetails filter) {
177         final var builder = ImmutableMap.<String, Object>builder()
178             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, contextSelect)
179             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_ASYNC_SUPPORTED, filter.getAsyncSupported())
180             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_NAME, filter.name())
181             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN,
182                 absolutePatterns(contextPath, filter.urlPatterns()));
183
184         for (var e : filter.initParams().entrySet()) {
185             builder.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + e.getKey(), e.getValue());
186         }
187
188         return builder.build();
189     }
190
191     private static Map<String, Object> resourceProperties(final String contextPath, final String contextSelect,
192             final ResourceDetails resource) {
193         final var path = absolutePath(resource.name());
194         return Map.of(
195             HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, contextSelect,
196             HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN, contextPath + absolutePath(resource.alias()),
197             HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX, path);
198     }
199
200     private static Map<String, Object> servletProperties(final String contextPath, final String contextSelect,
201             final ServletDetails servlet) {
202         final var builder = ImmutableMap.<String, Object>builder()
203             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, contextSelect)
204             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ASYNC_SUPPORTED, servlet.getAsyncSupported())
205             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, servlet.name())
206             .put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN,
207                 absolutePatterns(contextPath, servlet.urlPatterns()));
208
209         for (var e : servlet.initParams().entrySet()) {
210             builder.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + e.getKey(), e.getValue());
211         }
212
213         return builder.build();
214     }
215
216     private static String absolutePath(final String path) {
217         return path.startsWith("/") ? path : "/" + path;
218     }
219
220     private static List<String> absolutePatterns(final String contextPath, final List<String> urlPatterns) {
221         return urlPatterns.stream()
222             // Reject duplicates
223             .distinct()
224             // Ease of debugging
225             .sorted()
226             .map(urlPattern -> contextPath + urlPattern)
227             .collect(Collectors.toUnmodifiableList());
228     }
229 }