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 * Returns the decoded content of the reply. This method uses the charset
111 * information from the {@link #contentType()}, if present, or UTF-8 if no
112 * content type is present.
114 * @return The decoded content
116 public String content() {
118 return new String(rawResult(), extractCharset(contentType()));
119 } catch (UnsupportedEncodingException uee1) {
120 throw new RuntimeException(String.format("Could not decode content as %s.", extractCharset(contentType())), uee1);
129 * Extracts charset information from the given content type.
132 * The content type response header
133 * @return The extracted charset, or UTF-8 if no charset could be extracted
135 private static String extractCharset(String contentType) {
136 if (contentType == null) {
139 HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
140 NameValuePair charset = headerElement.getParameterByName("charset");
141 return (charset != null) ? charset.getValue() : "ISO-8859-1";
152 public String toString() {
153 return String.format("%s[uri=%s,protocolCode=%d,contentType=%s,rawResult=(%s bytes)]", getClass().getSimpleName(), uri(), protocolCode(), contentType(), rawResult().length);