2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.neeo.internal.net;
15 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.Objects;
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;
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;
36 * This class represents an HTTP session with a client
38 * @author Tim Roberts - Initial contribution
41 public class HttpRequest implements AutoCloseable {
44 private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);
46 /** The client to use */
47 private final Client client;
50 * Instantiates a new request
52 public HttpRequest(ClientBuilder clientBuilder) {
53 client = clientBuilder.build();
55 if (logger.isDebugEnabled()) {
56 client.register(new LoggingFilter(new Slf4LoggingAdapter(logger), true));
61 * Send a get command to the specified uri
63 * @param uri the non-null uri
64 * @return the {@link HttpResponse}
66 public HttpResponse sendGetCommand(String uri) {
67 NeeoUtil.requireNotEmpty(uri, "uri cannot be empty");
69 final Builder request = client.target(uri).request();
71 final Response content = request.get();
74 return new HttpResponse(content);
78 } catch (IOException | IllegalStateException e) {
79 return new HttpResponse(HttpStatus.SERVICE_UNAVAILABLE_503, e.getMessage());
84 * Send post JSON command using the body
86 * @param uriString the non empty uri
87 * @param body the non-null, possibly empty body
88 * @return the {@link HttpResponse}
90 public HttpResponse sendPostJsonCommand(String uriString, String body) {
91 NeeoUtil.requireNotEmpty(uriString, "uri cannot be empty");
92 Objects.requireNonNull(body, "body cannot be null");
94 logger.trace("sendPostJsonCommand: target={}, body={}", uriString, body);
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");
102 final Builder request = client.target(targetUri).request(MediaType.APPLICATION_JSON);
104 final Response content = request.post(Entity.entity(body, MediaType.APPLICATION_JSON));
107 return new HttpResponse(content);
111 // IllegalArgumentException/ProcessingException catches issues with the URI being invalid
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());
121 public void close() {