]> git.basschouten.com Git - openhab-addons.git/blob
f00795831ecac97625a2f12b47fd2894245f64a7
[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.neeo.internal.handler;
14
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.function.Consumer;
18 import java.util.stream.Collectors;
19
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.ws.rs.client.ClientBuilder;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.openhab.binding.neeo.internal.net.HttpRequest;
29 import org.openhab.binding.neeo.internal.net.HttpResponse;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This servlet handles the forward actions events from the NEEO Brain. The forward actions will be posted to the
35  * callback and then will be forwarded on to any URLs lised in {@link #forwardChain}
36  *
37  * @author Tim Roberts - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 @SuppressWarnings("serial")
42 public class NeeoForwardActionsServlet extends HttpServlet {
43
44     /** The logger */
45     private final Logger logger = LoggerFactory.getLogger(NeeoForwardActionsServlet.class);
46
47     /** The event publisher */
48     private final Consumer<String> callback;
49
50     /** The forwarding chain */
51     private final @Nullable String forwardChain;
52
53     /** The {@link ClientBuilder} to use */
54     private final ClientBuilder clientBuilder;
55
56     /** The scheduler to use to schedule recipe execution */
57     private final ScheduledExecutorService scheduler;
58
59     /**
60      * Creates the servlet the will process forward action events from the NEEO brain.
61      *
62      * @param scheduler a non-null {@link ScheduledExecutorService} to schedule forward actions
63      * @param callback a non-null String consumer
64      * @param forwardChain a possibly null, possibly empty forwarding chain
65      */
66     NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Consumer<String> callback,
67             @Nullable String forwardChain, ClientBuilder clientBuilder) {
68         super();
69
70         this.scheduler = scheduler;
71         this.callback = callback;
72         this.forwardChain = forwardChain;
73         this.clientBuilder = clientBuilder;
74     }
75
76     /**
77      * Processes the post action from the NEEO brain. Simply gets the specified json and then forwards it on (if
78      * needed)
79      *
80      * @param req the non-null request
81      * @param resp the non-null response
82      */
83     @Override
84     protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) throws IOException {
85         if (req == null || resp == null) {
86             logger.warn("doPost called with req={}, resp={}, non-null required.", req, resp);
87             return;
88         }
89
90         final String json = req.getReader().lines().collect(Collectors.joining("\n"));
91         logger.debug("handleForwardActions {}", json);
92
93         callback.accept(json);
94
95         if (forwardChain != null && !forwardChain.isEmpty()) {
96             scheduler.execute(() -> {
97                 try (final HttpRequest request = new HttpRequest(clientBuilder)) {
98                     for (final String forwardUrl : forwardChain.split(",")) {
99                         if (!forwardUrl.isEmpty()) {
100                             final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
101                             if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
102                                 logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
103                                         httpResponse.getHttpCode());
104                             }
105                         }
106                     }
107                 }
108             });
109         }
110     }
111 }