2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.jellyfin.internal;
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;
20 import java.util.HashMap;
23 import javax.servlet.ServletException;
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;
46 * The {@link JellyfinHandlerFactory} is responsible for creating things and thing
49 * @author Miguel Álvarez - Initial contribution
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<>();
59 public JellyfinHandlerFactory(@Reference HttpService httpService) {
60 this.httpService = httpService;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
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);
76 if (THING_TYPE_CLIENT.equals(thingTypeUID)) {
77 return new JellyfinClientHandler(thing);
83 protected synchronized void removeHandler(ThingHandler thingHandler) {
84 if (thingHandler instanceof JellyfinServerHandler) {
85 var serverHandler = (JellyfinServerHandler) thingHandler;
86 unregisterAuthenticationServlet(serverHandler);
88 super.removeHandler(thingHandler);
91 private synchronized void registerAuthenticationServlet(JellyfinServerHandler bridgeHandler) {
92 var auth = new JellyfinBridgeServlet(bridgeHandler);
94 httpService.registerServlet(getAuthenticationServletPath(bridgeHandler), auth, null,
95 httpService.createDefaultHttpContext());
96 } catch (NamespaceException | ServletException e) {
97 logger.warn("Register servlet fails", e);
99 servletRegistrations.put(bridgeHandler.getThing().getUID(), auth);
102 private synchronized void unregisterAuthenticationServlet(JellyfinServerHandler bridgeHandler) {
103 var loginServlet = servletRegistrations.get(bridgeHandler.getThing().getUID());
104 if (loginServlet != null) {
105 httpService.unregister(getAuthenticationServletPath(bridgeHandler));
109 private String getAuthenticationServletPath(JellyfinServerHandler bridgeHandler) {
110 return new StringBuilder().append("/").append(BINDING_ID).append("/")
111 .append(bridgeHandler.getThing().getUID().getId()).toString();