]> git.basschouten.com Git - openhab-addons.git/blob
5c2565b5ec1c285a1de646c261bfad949acc4a46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Objects;
17 import java.util.concurrent.ScheduledExecutorService;
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.apache.commons.io.IOUtils;
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 Callback callback;
49
50     /** The forwarding chain */
51     @Nullable
52     private final String forwardChain;
53
54     /** The {@link ClientBuilder} to use */
55     private final ClientBuilder clientBuilder;
56
57     /** The scheduler to use to schedule recipe execution */
58     private final ScheduledExecutorService scheduler;
59
60     /**
61      * Creates the servlet the will process foward action events from the NEEO brain.
62      *
63      * @param scheduler a non-null {@link ScheduledExecutorService} to schedule forward actions
64      * @param callback a non-null {@link Callback}
65      * @param forwardChain a possibly null, possibly empty forwarding chain
66      */
67     NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, @Nullable String forwardChain,
68             ClientBuilder clientBuilder) {
69         super();
70
71         Objects.requireNonNull(scheduler, "scheduler cannot be null");
72         Objects.requireNonNull(callback, "callback cannot be null");
73
74         this.scheduler = scheduler;
75         this.callback = callback;
76         this.forwardChain = forwardChain;
77         this.clientBuilder = clientBuilder;
78     }
79
80     /**
81      * Processes the post action from the NEEO brain. Simply get's the specified json and then forwards it on (if
82      * needed)
83      *
84      * @param req the non-null request
85      * @param resp the non-null response
86      */
87     @Override
88     protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) throws IOException {
89         if (req == null || resp == null) {
90             logger.warn("doPost called with req={}, resp={}, non-null required.", req, resp);
91             return;
92         }
93
94         final String json = IOUtils.toString(req.getReader());
95         logger.debug("handleForwardActions {}", json);
96
97         callback.post(json);
98
99         final String fc = forwardChain;
100         if (fc != null && !fc.isEmpty()) {
101             scheduler.execute(() -> {
102                 try (final HttpRequest request = new HttpRequest(clientBuilder)) {
103                     for (final String forwardUrl : fc.split(",")) {
104                         if (forwardUrl != null && !forwardUrl.isEmpty()) {
105                             final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
106                             if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
107                                 logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
108                                         httpResponse.getHttpCode());
109                             }
110                         }
111                     }
112                 }
113             });
114         }
115     }
116
117     interface Callback {
118         void post(String json);
119     }
120 }