2 * Rhynodge - HttpState.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.states;
20 import static java.util.Arrays.copyOf;
22 import java.io.UnsupportedEncodingException;
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.queries.HttpQuery;
27 import org.apache.http.HeaderElement;
28 import org.apache.http.NameValuePair;
29 import org.apache.http.message.BasicHeaderValueParser;
32 * {@link State} that contains the results of an {@link HttpQuery}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class HttpState extends AbstractState {
38 /** The URI that was requested. */
39 private final String uri;
41 /** The protocol code. */
42 private final int protocolCode;
44 /** The content type. */
45 private final String contentType;
48 private final byte[] rawResult;
51 * Creates a new HTTP state.
54 * The URI that was requested
56 * The code of the reply
58 * The content type of the reply
62 public HttpState(String uri, int protocolCode, String contentType, byte[] rawResult) {
64 this.protocolCode = protocolCode;
65 this.contentType = contentType;
66 this.rawResult = copyOf(rawResult, rawResult.length);
74 * Returns the URI that was requested.
76 * @return The URI that was request
83 * Returns the protocol code of the reply.
85 * @return The protocol code of the reply
87 public int protocolCode() {
92 * Returns the content type of the reply.
94 * @return The content type of the reply
96 public String contentType() {
101 * Returns the raw result of the reply.
103 * @return The raw result of the reply
105 public byte[] rawResult() {
106 return copyOf(rawResult, rawResult.length);
110 public boolean isEmpty() {
111 return rawResult.length == 0;
115 * Returns the decoded content of the reply. This method uses the charset
116 * information from the {@link #contentType()}, if present, or UTF-8 if no
117 * content type is present.
119 * @return The decoded content
121 public String content() {
123 return new String(rawResult(), extractCharset(contentType()));
124 } catch (UnsupportedEncodingException uee1) {
125 throw new RuntimeException(String.format("Could not decode content as %s.", extractCharset(contentType())), uee1);
134 * Extracts charset information from the given content type.
137 * The content type response header
138 * @return The extracted charset, or UTF-8 if no charset could be extracted
140 private static String extractCharset(String contentType) {
141 if (contentType == null) {
144 HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
145 NameValuePair charset = headerElement.getParameterByName("charset");
146 return (charset != null) ? charset.getValue() : "ISO-8859-1";
157 public String toString() {
158 return String.format("%s[uri=%s,protocolCode=%d,contentType=%s,rawResult=(%s bytes)]", getClass().getSimpleName(), uri(), protocolCode(), contentType(), rawResult().length);