]> git.basschouten.com Git - openhab-addons.git/blob
0e2a5743e8b69bccb1a60c0cd26559816b176f4e
[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.net;
14
15 import java.io.IOException;
16 import java.util.Objects;
17
18 import javax.ws.rs.ProcessingException;
19 import javax.ws.rs.client.Client;
20 import javax.ws.rs.client.ClientBuilder;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.client.Invocation.Builder;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.glassfish.jersey.filter.LoggingFilter;
29 import org.openhab.binding.neeo.internal.NeeoUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class represents an HTTP session with a client
35  *
36  * @author Tim Roberts - Initial contribution
37  */
38 @NonNullByDefault
39 public class HttpRequest implements AutoCloseable {
40
41     /** the logger */
42     private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);
43
44     /** The client to use */
45     private final Client client;
46
47     /**
48      * Instantiates a new request
49      */
50     public HttpRequest() {
51         client = ClientBuilder.newClient();
52
53         if (logger.isDebugEnabled()) {
54             client.register(new LoggingFilter(new Slf4LoggingAdapter(logger), true));
55         }
56     }
57
58     /**
59      * Send a get command to the specified uri
60      *
61      * @param uri the non-null uri
62      * @return the {@link HttpResponse}
63      */
64     public HttpResponse sendGetCommand(String uri) {
65         NeeoUtil.requireNotEmpty(uri, "uri cannot be empty");
66         try {
67             final Builder request = client.target(uri).request();
68
69             final Response content = request.get();
70
71             try {
72                 return new HttpResponse(content);
73             } finally {
74                 content.close();
75             }
76         } catch (IOException | IllegalStateException e) {
77             return new HttpResponse(HttpStatus.SERVICE_UNAVAILABLE_503, e.getMessage());
78         }
79     }
80
81     /**
82      * Send post JSON command using the body
83      *
84      * @param uri the non empty uri
85      * @param body the non-null, possibly empty body
86      * @return the {@link HttpResponse}
87      */
88     public HttpResponse sendPostJsonCommand(String uri, String body) {
89         NeeoUtil.requireNotEmpty(uri, "uri cannot be empty");
90         Objects.requireNonNull(body, "body cannot be null");
91
92         try {
93             final Builder request = client.target(uri).request(MediaType.APPLICATION_JSON);
94
95             final Response content = request.post(Entity.entity(body, MediaType.APPLICATION_JSON));
96
97             try {
98                 return new HttpResponse(content);
99             } finally {
100                 content.close();
101             }
102             // IllegalArgumentException/ProcessingException catches issues with the URI being invalid
103             // as well
104         } catch (IOException | IllegalStateException | IllegalArgumentException | ProcessingException e) {
105             return new HttpResponse(HttpStatus.SERVICE_UNAVAILABLE_503, e.getMessage());
106         }
107     }
108
109     @Override
110     public void close() {
111         client.close();
112     }
113 }