]> git.basschouten.com Git - openhab-addons.git/blob
e3a5fedcf526e08044ae258ed999874f940f2bc0
[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.shelly.internal.manager;
14
15 import static org.openhab.binding.shelly.internal.manager.ShellyManagerConstants.*;
16 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.PrintWriter;
21 import java.util.Map;
22
23 import javax.servlet.Servlet;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.shelly.internal.ShellyHandlerFactory;
32 import org.openhab.binding.shelly.internal.api.ShellyApiException;
33 import org.openhab.binding.shelly.internal.manager.ShellyManagerPage.ShellyMgrResponse;
34 import org.openhab.binding.shelly.internal.provider.ShellyTranslationProvider;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.net.HttpServiceUtil;
37 import org.openhab.core.net.NetworkAddressService;
38 import org.osgi.service.cm.ConfigurationAdmin;
39 import org.osgi.service.component.ComponentContext;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.ConfigurationPolicy;
43 import org.osgi.service.component.annotations.Deactivate;
44 import org.osgi.service.component.annotations.Reference;
45 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
46 import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * {@link ShellyManagerServlet} implements the Shelly Manager - a simple device overview/management
52  *
53  * @author Markus Michels - Initial contribution
54  */
55 @NonNullByDefault
56 @Component(service = Servlet.class, configurationPolicy = ConfigurationPolicy.OPTIONAL)
57 @HttpWhiteboardServletName(ShellyManagerServlet.SERVLET_URI)
58 @HttpWhiteboardServletPattern(ShellyManagerServlet.SERVLET_URI + "/*")
59 public class ShellyManagerServlet extends HttpServlet {
60     private static final long serialVersionUID = 1393403713585449126L;
61     private final Logger logger = LoggerFactory.getLogger(ShellyManagerServlet.class);
62
63     public static final String SERVLET_URI = SHELLY_MANAGER_URI;
64     private final ShellyManager manager;
65     private final String className;
66
67     @Activate
68     public ShellyManagerServlet(@Reference ConfigurationAdmin configurationAdmin,
69             @Reference NetworkAddressService networkAddressService, @Reference HttpClientFactory httpClientFactory,
70             @Reference ShellyHandlerFactory handlerFactory, @Reference ShellyTranslationProvider translationProvider,
71             ComponentContext componentContext, Map<String, Object> config) {
72         className = substringAfterLast(getClass().toString(), ".");
73         String localIp = getString(networkAddressService.getPrimaryIpv4HostAddress());
74         Integer localPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
75         this.manager = new ShellyManager(configurationAdmin, translationProvider,
76                 httpClientFactory.getCommonHttpClient(), localIp, localPort, handlerFactory);
77
78         // Promote Shelly Manager usage
79         logger.info("{}", translationProvider.get("status.managerstarted", localIp, localPort.toString()));
80     }
81
82     @Deactivate
83     protected void deactivate() {
84         logger.debug("{} stopped", className);
85     }
86
87     @SuppressWarnings("resource")
88     @Override
89     protected void service(@Nullable HttpServletRequest request, @Nullable HttpServletResponse response)
90             throws ServletException, IOException, IllegalArgumentException {
91         if ((request == null) || (response == null)) {
92             logger.debug("request or resp must not be null!");
93             return;
94         }
95
96         String path = getString(request.getRequestURI()).toLowerCase();
97         String ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
98         ShellyMgrResponse output = new ShellyMgrResponse();
99         PrintWriter print = null;
100         OutputStream bin = null;
101         try {
102             if (ipAddress == null) {
103                 ipAddress = request.getRemoteAddr();
104             }
105             Map<String, String[]> parameters = request.getParameterMap();
106             logger.debug("{}: {} Request from {}:{}{}?{}", className, request.getProtocol(), ipAddress,
107                     request.getRemotePort(), path, parameters.toString());
108             if (!path.toLowerCase().startsWith(SERVLET_URI)) {
109                 logger.warn("{} received unknown request: path = {}", className, path);
110                 return;
111             }
112
113             output = manager.generateContent(path, parameters);
114             response.setContentType(output.mimeType);
115             if ("text/html".equals(output.mimeType)) {
116                 // Make sure it's UTF-8 encoded
117                 response.setCharacterEncoding(UTF_8);
118                 print = response.getWriter();
119                 print.write((String) output.data);
120             } else {
121                 // binary data
122                 byte[] data = (byte[]) output.data;
123                 if (data != null) {
124                     response.setContentLength(data.length);
125                     bin = response.getOutputStream();
126                     bin.write(data, 0, data.length);
127                 }
128             }
129         } catch (ShellyApiException | RuntimeException e) {
130             logger.debug("{}: Exception uri={}, parameters={}", className, path, request.getParameterMap().toString(),
131                     e);
132             response.setContentType("text/html");
133             print = response.getWriter();
134             print.write("Exception:" + e.toString() + "<br/>Check openhab.log for details."
135                     + "<p/><a href=\"/shelly/manager\">Return to Overview</a>");
136             logger.debug("{}: {}", className, output);
137             response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
138         } finally {
139             if (print != null) {
140                 print.close();
141             }
142             if (bin != null) {
143                 bin.close();
144             }
145         }
146     }
147 }