]> git.basschouten.com Git - openhab-addons.git/blob
8c4aa97c24afb14fa6e914bd3fb153d4c16a072c
[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.nio.charset.StandardCharsets;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Objects;
20
21 import javax.ws.rs.core.Response;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.api.ContentResponse;
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 refreshResponse the non-null response
51      * @throws IOException Signals that an I/O exception has occurred.
52      */
53     HttpResponse(ContentResponse refreshResponse) throws IOException {
54         Objects.requireNonNull(refreshResponse, "response cannot be null");
55
56         httpStatus = refreshResponse.getStatus();
57         httpReason = refreshResponse.getReason();
58         contents = refreshResponse.getContent();
59
60         for (String key : refreshResponse.getHeaders().getFieldNamesCollection()) {
61             headers.put(key, refreshResponse.getHeaders().getField(key).toString());
62         }
63     }
64
65     /**
66      * Instantiates a new http response.
67      *
68      * @param httpCode the http code
69      * @param msg the msg
70      */
71     HttpResponse(int httpCode, @Nullable String msg) {
72         httpStatus = httpCode;
73         httpReason = msg != null ? msg : "";
74         contents = null;
75     }
76
77     /**
78      * Gets the HTTP status code.
79      *
80      * @return the HTTP status code
81      */
82     public int getHttpCode() {
83         return httpStatus;
84     }
85
86     /**
87      * Gets the content.
88      *
89      * @return the content
90      */
91     public String getContent() {
92         final byte[] localContents = contents;
93         if (localContents == null || localContents.length == 0) {
94             return "";
95         }
96
97         return new String(localContents, StandardCharsets.UTF_8);
98     }
99
100     /**
101      * Creates an {@link IOException} from the {@link #httpReason}
102      *
103      * @return the IO exception
104      */
105     public IOException createException() {
106         return new IOException(httpReason);
107     }
108
109     @Override
110     public String toString() {
111         return getHttpCode() + " (" + (contents == null ? ("http reason: " + httpReason) : getContent()) + ")";
112     }
113 }