From 50cdd0244799e9c6faea3e1600ea62841af83e60 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 8 Mar 2023 17:20:08 +0100 Subject: [PATCH] [ipcamera] Make sure created Servlet supports async (#14552) Fixes the folowing errors: ``` HTTP ERROR 500 java.lang.IllegalStateException: !asyncSupported: NotAsync:org.ops4j.pax.web.service.spi.servlet.OsgiInitializedServlet@536b0858 URI: /ipcamera/192168493/ipcamera.jpg STATUS: 500 MESSAGE: java.lang.IllegalStateException: !asyncSupported: NotAsync:org.ops4j.pax.web.service.spi.servlet.OsgiInitializedServlet@536b0858 SERVLET: org.openhab.binding.ipcamera.internal.servlet.CameraServlet CAUSED BY: java.lang.IllegalStateException: !asyncSupported: NotAsync:org.ops4j.pax.web.service.spi.servlet.OsgiInitializedServlet@536b0858 Powered by Jetty:// 9.4.50.v20221201 ``` See: * https://community.openhab.org/t/openhab-4-0-snapshot-discussion/142322/226 * https://groups.google.com/g/ops4j/c/E9p7tPydPmo * https://github.com/ops4j/org.ops4j.pax.web/issues/1767 Signed-off-by: Wouter Born --- .../internal/servlet/CameraServlet.java | 24 +++++++++++-------- .../internal/servlet/IpCameraServlet.java | 10 +++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/CameraServlet.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/CameraServlet.java index 1b5c14b883e..4868ed050b7 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/CameraServlet.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/CameraServlet.java @@ -17,6 +17,9 @@ import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.HLS import java.io.IOException; import java.time.Duration; import java.time.Instant; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Map; import javax.servlet.AsyncContext; import javax.servlet.ServletInputStream; @@ -41,13 +44,16 @@ import org.osgi.service.http.HttpService; @NonNullByDefault public class CameraServlet extends IpCameraServlet { private static final long serialVersionUID = -134658667574L; + private static final Dictionary INIT_PARAMETERS = new Hashtable<>( + Map.of("async-supported", "true")); + private final IpCameraHandler handler; public OpenStreams openStreams = new OpenStreams(); private OpenStreams openSnapshotStreams = new OpenStreams(); private OpenStreams openAutoFpsStreams = new OpenStreams(); public CameraServlet(IpCameraHandler handler, HttpService httpService) { - super(handler, httpService); + super(handler, httpService, INIT_PARAMETERS); this.handler = handler; } @@ -183,17 +189,15 @@ public class CameraServlet extends IpCameraServlet { } else { output = new StreamOutput(resp, handler.mjpegContentType); } + } else if (handler.mjpegUri.isEmpty() || "ffmpeg".equals(handler.mjpegUri)) { + output = new StreamOutput(resp); } else { - if (handler.mjpegUri.isEmpty() || "ffmpeg".equals(handler.mjpegUri)) { - output = new StreamOutput(resp); - } else { - ChannelTracking tracker = handler.channelTrackingMap.get(handler.getTinyUrl(handler.mjpegUri)); - if (tracker == null || !tracker.getChannel().isOpen()) { - logger.debug("Not the first stream requested but the stream from camera was closed"); - handler.openCamerasStream(); - } - output = new StreamOutput(resp, handler.mjpegContentType); + ChannelTracking tracker = handler.channelTrackingMap.get(handler.getTinyUrl(handler.mjpegUri)); + if (tracker == null || !tracker.getChannel().isOpen()) { + logger.debug("Not the first stream requested but the stream from camera was closed"); + handler.openCamerasStream(); } + output = new StreamOutput(resp, handler.mjpegContentType); } openStreams.addStream(output); do { diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/IpCameraServlet.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/IpCameraServlet.java index 49a4ccba5e6..f8f817387f3 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/IpCameraServlet.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/IpCameraServlet.java @@ -17,6 +17,8 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Dictionary; +import java.util.Properties; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; @@ -42,16 +44,22 @@ public abstract class IpCameraServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected final ThingHandler handler; protected final HttpService httpService; + protected final Dictionary initParameters; public IpCameraServlet(ThingHandler handler, HttpService httpService) { + this(handler, httpService, new Properties()); + } + + public IpCameraServlet(ThingHandler handler, HttpService httpService, Dictionary initParameters) { this.handler = handler; this.httpService = httpService; + this.initParameters = initParameters; startListening(); } public void startListening() { try { - httpService.registerServlet("/ipcamera/" + handler.getThing().getUID().getId(), this, null, + httpService.registerServlet("/ipcamera/" + handler.getThing().getUID().getId(), this, initParameters, httpService.createDefaultHttpContext()); } catch (Exception e) { logger.warn("Registering servlet failed:{}", e.getMessage());