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