]> git.basschouten.com Git - openhab-addons.git/blob
d1d5dd1f462af4dd63fc28e7c3c35bf67fd3fb08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.jellyfin.internal;
14
15 import static org.openhab.binding.jellyfin.internal.JellyfinBindingConstants.BINDING_ID;
16 import static org.openhab.binding.jellyfin.internal.JellyfinBindingConstants.SUPPORTED_THING_TYPES;
17 import static org.openhab.binding.jellyfin.internal.JellyfinBindingConstants.THING_TYPE_CLIENT;
18 import static org.openhab.binding.jellyfin.internal.JellyfinBindingConstants.THING_TYPE_SERVER;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.servlet.ServletException;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.jellyfin.internal.handler.JellyfinClientHandler;
28 import org.openhab.binding.jellyfin.internal.handler.JellyfinServerHandler;
29 import org.openhab.binding.jellyfin.internal.servlet.JellyfinBridgeServlet;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40 import org.osgi.service.http.HttpService;
41 import org.osgi.service.http.NamespaceException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link JellyfinHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Miguel Álvarez - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(configurationPid = "binding.jellyfin", service = ThingHandlerFactory.class)
53 public class JellyfinHandlerFactory extends BaseThingHandlerFactory {
54     private final HttpService httpService;
55     private final Logger logger = LoggerFactory.getLogger(JellyfinHandlerFactory.class);
56     private final Map<ThingUID, JellyfinBridgeServlet> servletRegistrations = new HashMap<>();
57
58     @Activate
59     public JellyfinHandlerFactory(@Reference HttpService httpService) {
60         this.httpService = httpService;
61     }
62
63     @Override
64     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
66     }
67
68     @Override
69     protected @Nullable ThingHandler createHandler(Thing thing) {
70         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
71         if (THING_TYPE_SERVER.equals(thingTypeUID)) {
72             var serverHandler = new JellyfinServerHandler((Bridge) thing);
73             registerAuthenticationServlet(serverHandler);
74             return serverHandler;
75         }
76         if (THING_TYPE_CLIENT.equals(thingTypeUID)) {
77             return new JellyfinClientHandler(thing);
78         }
79         return null;
80     }
81
82     @Override
83     protected synchronized void removeHandler(ThingHandler thingHandler) {
84         if (thingHandler instanceof JellyfinServerHandler) {
85             var serverHandler = (JellyfinServerHandler) thingHandler;
86             unregisterAuthenticationServlet(serverHandler);
87         }
88         super.removeHandler(thingHandler);
89     }
90
91     private synchronized void registerAuthenticationServlet(JellyfinServerHandler bridgeHandler) {
92         var auth = new JellyfinBridgeServlet(bridgeHandler);
93         try {
94             httpService.registerServlet(getAuthenticationServletPath(bridgeHandler), auth, null,
95                     httpService.createDefaultHttpContext());
96         } catch (NamespaceException | ServletException e) {
97             logger.warn("Register servlet fails", e);
98         }
99         servletRegistrations.put(bridgeHandler.getThing().getUID(), auth);
100     }
101
102     private synchronized void unregisterAuthenticationServlet(JellyfinServerHandler bridgeHandler) {
103         var loginServlet = servletRegistrations.get(bridgeHandler.getThing().getUID());
104         if (loginServlet != null) {
105             httpService.unregister(getAuthenticationServletPath(bridgeHandler));
106         }
107     }
108
109     private String getAuthenticationServletPath(JellyfinServerHandler bridgeHandler) {
110         return new StringBuilder().append("/").append(BINDING_ID).append("/")
111                 .append(bridgeHandler.getThing().getUID().getId()).toString();
112     }
113 }