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 java.io.UnsupportedEncodingException;
22 import net.pterodactylus.rhynodge.State;
23 import net.pterodactylus.rhynodge.queries.HttpQuery;
25 import org.apache.http.HeaderElement;
26 import org.apache.http.NameValuePair;
27 import org.apache.http.message.BasicHeaderValueParser;
30 * {@link State} that contains the results of an {@link HttpQuery}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class HttpState extends AbstractState {
36 /** The URI that was requested. */
37 private final String uri;
39 /** The protocol code. */
40 private final int protocolCode;
42 /** The content type. */
43 private final String contentType;
46 private final byte[] rawResult;
49 * Creates a new HTTP state.
52 * The URI that was requested
54 * The code of the reply
56 * The content type of the reply
60 public HttpState(String uri, int protocolCode, String contentType, byte[] rawResult) {
62 this.protocolCode = protocolCode;
63 this.contentType = contentType;
64 this.rawResult = rawResult;
72 * Returns the URI that was requested.
74 * @return The URI that was request
81 * Returns the protocol code of the reply.
83 * @return The protocol code of the reply
85 public int protocolCode() {
90 * Returns the content type of the reply.
92 * @return The content type of the reply
94 public String contentType() {
99 * Returns the raw result of the reply.
101 * @return The raw result of the reply
103 public byte[] rawResult() {
108 * Returns the decoded content of the reply. This method uses the charset
109 * information from the {@link #contentType()}, if present, or UTF-8 if no
110 * content type is present.
112 * @return The decoded content
114 public String content() {
116 return new String(rawResult(), extractCharset(contentType()));
117 } catch (UnsupportedEncodingException uee1) {
118 throw new RuntimeException(String.format("Could not decode content as %s.", extractCharset(contentType())), uee1);
127 * Extracts charset information from the given content type.
130 * The content type response header
131 * @return The extracted charset, or UTF-8 if no charset could be extracted
133 private static String extractCharset(String contentType) {
134 if (contentType == null) {
137 HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
138 NameValuePair charset = headerElement.getParameterByName("charset");
139 return (charset != null) ? charset.getValue() : "ISO-8859-1";
150 public String toString() {
151 return String.format("%s[uri=%s,protocolCode=%d,contentType=%s,rawResult=(%s bytes)]", getClass().getSimpleName(), uri(), protocolCode(), contentType(), rawResult().length);