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