[shelly] Fix missing /shelly/manager (#14592)

Using Whiteboard annotations seems to fix this issue.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2023-03-12 20:42:12 +01:00 committed by GitHub
parent 6e0fe0e0ad
commit 2b63b984dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 21 deletions

View File

@ -20,6 +20,7 @@ import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Map; import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -41,8 +42,8 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy; import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpService; import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
import org.osgi.service.http.NamespaceException; import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -52,42 +53,34 @@ import org.slf4j.LoggerFactory;
* @author Markus Michels - Initial contribution * @author Markus Michels - Initial contribution
*/ */
@NonNullByDefault @NonNullByDefault
@Component(service = HttpServlet.class, configurationPolicy = ConfigurationPolicy.OPTIONAL) @Component(service = Servlet.class, configurationPolicy = ConfigurationPolicy.OPTIONAL)
@HttpWhiteboardServletName(ShellyManagerServlet.SERVLET_URI)
@HttpWhiteboardServletPattern(ShellyManagerServlet.SERVLET_URI + "/*")
public class ShellyManagerServlet extends HttpServlet { public class ShellyManagerServlet extends HttpServlet {
private static final long serialVersionUID = 1393403713585449126L; private static final long serialVersionUID = 1393403713585449126L;
private final Logger logger = LoggerFactory.getLogger(ShellyManagerServlet.class); private final Logger logger = LoggerFactory.getLogger(ShellyManagerServlet.class);
private static final String SERVLET_URI = SHELLY_MANAGER_URI; public static final String SERVLET_URI = SHELLY_MANAGER_URI;
private final ShellyManager manager; private final ShellyManager manager;
private final String className; private final String className;
private final HttpService httpService;
@Activate @Activate
public ShellyManagerServlet(@Reference ConfigurationAdmin configurationAdmin, public ShellyManagerServlet(@Reference ConfigurationAdmin configurationAdmin,
@Reference NetworkAddressService networkAddressService, @Reference HttpService httpService, @Reference NetworkAddressService networkAddressService, @Reference HttpClientFactory httpClientFactory,
@Reference HttpClientFactory httpClientFactory, @Reference ShellyHandlerFactory handlerFactory, @Reference ShellyHandlerFactory handlerFactory, @Reference ShellyTranslationProvider translationProvider,
@Reference ShellyTranslationProvider translationProvider, ComponentContext componentContext, ComponentContext componentContext, Map<String, Object> config) {
Map<String, Object> config) {
className = substringAfterLast(getClass().toString(), "."); className = substringAfterLast(getClass().toString(), ".");
this.httpService = httpService;
String localIp = getString(networkAddressService.getPrimaryIpv4HostAddress()); String localIp = getString(networkAddressService.getPrimaryIpv4HostAddress());
Integer localPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext()); Integer localPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
this.manager = new ShellyManager(configurationAdmin, translationProvider, this.manager = new ShellyManager(configurationAdmin, translationProvider,
httpClientFactory.getCommonHttpClient(), localIp, localPort, handlerFactory); httpClientFactory.getCommonHttpClient(), localIp, localPort, handlerFactory);
try { // Promote Shelly Manager usage
httpService.registerServlet(SERVLET_URI, this, null, httpService.createDefaultHttpContext()); logger.info("{}", translationProvider.get("status.managerstarted", localIp, localPort.toString()));
// Promote Shelly Manager usage
logger.info("{}", translationProvider.get("status.managerstarted", localIp, localPort.toString()));
} catch (NamespaceException | ServletException | IllegalArgumentException e) {
logger.warn("{}: Unable to initialize bindingConfig", className, e);
}
} }
@Deactivate @Deactivate
protected void deactivate() { protected void deactivate() {
httpService.unregister(SERVLET_URI);
logger.debug("{} stopped", className); logger.debug("{} stopped", className);
} }
@ -118,7 +111,7 @@ public class ShellyManagerServlet extends HttpServlet {
output = manager.generateContent(path, parameters); output = manager.generateContent(path, parameters);
response.setContentType(output.mimeType); response.setContentType(output.mimeType);
if (output.mimeType.equals("text/html")) { if ("text/html".equals(output.mimeType)) {
// Make sure it's UTF-8 encoded // Make sure it's UTF-8 encoded
response.setCharacterEncoding(UTF_8); response.setCharacterEncoding(UTF_8);
print = response.getWriter(); print = response.getWriter();
@ -137,7 +130,7 @@ public class ShellyManagerServlet extends HttpServlet {
e); e);
response.setContentType("text/html"); response.setContentType("text/html");
print = response.getWriter(); print = response.getWriter();
print.write("Exception:" + e.toString() + "<br/>Check openHAB.log for details." print.write("Exception:" + e.toString() + "<br/>Check openhab.log for details."
+ "<p/><a href=\"/shelly/manager\">Return to Overview</a>"); + "<p/><a href=\"/shelly/manager\">Return to Overview</a>");
logger.debug("{}: {}", className, output); logger.debug("{}: {}", className, output);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);