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