]> git.basschouten.com Git - openhab-addons.git/blob
064cd273f198b6cdab116150b33b997554eb937c
[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.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.commons.io.IOUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jetty.http.HttpStatus;
29 import org.openhab.binding.neeo.internal.net.HttpRequest;
30 import org.openhab.binding.neeo.internal.net.HttpResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This servlet handles the forward actions events from the NEEO Brain. The forward actions will be posted to the
36  * callback and then will be forwarded on to any URLs lised in {@link #forwardChain}
37  *
38  * @author Tim Roberts - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 @SuppressWarnings("serial")
43 public class NeeoForwardActionsServlet extends HttpServlet {
44
45     /** The logger */
46     private final Logger logger = LoggerFactory.getLogger(NeeoForwardActionsServlet.class);
47
48     /** The event publisher */
49     private final Callback callback;
50
51     /** The forwarding chain */
52     @Nullable
53     private final String forwardChain;
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, @Nullable String forwardChain) {
66         super();
67
68         Objects.requireNonNull(scheduler, "scheduler cannot be null");
69         Objects.requireNonNull(callback, "callback cannot be null");
70
71         this.scheduler = scheduler;
72         this.callback = callback;
73         this.forwardChain = forwardChain;
74     }
75
76     /**
77      * Processes the post action from the NEEO brain. Simply get's 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)
85             throws ServletException, IOException {
86         Objects.requireNonNull(req, "req cannot be null");
87         Objects.requireNonNull(resp, "resp cannot be null");
88
89         final String json = IOUtils.toString(req.getReader());
90         logger.debug("handleForwardActions {}", json);
91
92         callback.post(json);
93
94         final String fc = forwardChain;
95         if (fc != null && StringUtils.isNotEmpty(fc)) {
96             scheduler.execute(() -> {
97                 try (final HttpRequest request = new HttpRequest()) {
98                     for (final String forwardUrl : fc.split(",")) {
99                         if (StringUtils.isNotEmpty(forwardUrl)) {
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
112     interface Callback {
113         void post(String json);
114     }
115 }