]> git.basschouten.com Git - openhab-addons.git/commitdiff
[ipcamera] Make sure created Servlet supports async (#14552)
authorWouter Born <github@maindrain.net>
Wed, 8 Mar 2023 16:20:08 +0000 (17:20 +0100)
committerGitHub <noreply@github.com>
Wed, 8 Mar 2023 16:20:08 +0000 (17:20 +0100)
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 <github@maindrain.net>
bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/CameraServlet.java
bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/servlet/IpCameraServlet.java

index 1b5c14b883ebe9ad2610ea645b18e35fe0403e24..4868ed050b71c34bdbb4b4859780802b464fd3d5 100644 (file)
@@ -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<Object, Object> 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 {
index 49a4ccba5e6a36d32d20f1d12691762dfdc211df..f8f817387f3198925e71b47c8574632df925e526 100644 (file)
@@ -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<Object, Object> initParameters;
 
     public IpCameraServlet(ThingHandler handler, HttpService httpService) {
+        this(handler, httpService, new Properties());
+    }
+
+    public IpCameraServlet(ThingHandler handler, HttpService httpService, Dictionary<Object, Object> 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());