]> git.basschouten.com Git - openhab-addons.git/blob
681a6169b400a4adbf6af211884b061e6b598caf
[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.io.InputStream;
17 import java.nio.charset.StandardCharsets;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Objects;
21
22 import javax.ws.rs.core.Response;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26
27 /**
28  * This class represents an {@link HttpRequest} response
29  *
30  * @author Tim Roberts - Initial contribution
31  */
32 @NonNullByDefault
33 public class HttpResponse {
34
35     /** The http status */
36     private final int httpStatus;
37
38     /** The http reason */
39     private final String httpReason;
40
41     /** The http headers */
42     private final Map<String, String> headers = new HashMap<>();
43
44     /** The contents as a raw byte array */
45     private final byte @Nullable [] contents;
46
47     /**
48      * Instantiates a new http response from the {@link Response}.
49      *
50      * @param response the non-null response
51      * @throws IOException Signals that an I/O exception has occurred.
52      */
53     HttpResponse(Response response) throws IOException {
54         Objects.requireNonNull(response, "response cannot be null");
55
56         httpStatus = response.getStatus();
57         httpReason = response.getStatusInfo().getReasonPhrase();
58
59         if (response.hasEntity()) {
60             InputStream is = response.readEntity(InputStream.class);
61             contents = is.readAllBytes();
62         } else {
63             contents = null;
64         }
65
66         for (String key : response.getHeaders().keySet()) {
67             headers.put(key, response.getHeaderString(key));
68         }
69     }
70
71     /**
72      * Instantiates a new http response.
73      *
74      * @param httpCode the http code
75      * @param msg the msg
76      */
77     HttpResponse(int httpCode, @Nullable String msg) {
78         httpStatus = httpCode;
79         httpReason = msg != null ? msg : "";
80         contents = null;
81     }
82
83     /**
84      * Gets the HTTP status code.
85      *
86      * @return the HTTP status code
87      */
88     public int getHttpCode() {
89         return httpStatus;
90     }
91
92     /**
93      * Gets the content.
94      *
95      * @return the content
96      */
97     public String getContent() {
98         final byte[] localContents = contents;
99         if (localContents == null || localContents.length == 0) {
100             return "";
101         }
102
103         return new String(localContents, StandardCharsets.UTF_8);
104     }
105
106     /**
107      * Creates an {@link IOException} from the {@link #httpReason}
108      *
109      * @return the IO exception
110      */
111     public IOException createException() {
112         return new IOException(httpReason);
113     }
114
115     @Override
116     public String toString() {
117         return getHttpCode() + " (" + (contents == null ? ("http reason: " + httpReason) : getContent()) + ")";
118     }
119 }