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 javax.annotation.Nonnull;
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.queries.HttpQuery;
29 import org.apache.http.HeaderElement;
30 import org.apache.http.NameValuePair;
31 import org.apache.http.message.BasicHeaderValueParser;
34 * {@link State} that contains the results of an {@link HttpQuery}.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class HttpState extends AbstractState {
40 /** The URI that was requested. */
41 private final String uri;
43 /** The protocol code. */
44 private final int protocolCode;
46 /** The content type. */
47 private final String contentType;
50 private final byte[] rawResult;
53 * Creates a new HTTP state.
56 * The URI that was requested
58 * The code of the reply
60 * The content type of the reply
64 public HttpState(String uri, int protocolCode, String contentType, byte[] rawResult) {
66 this.protocolCode = protocolCode;
67 this.contentType = contentType;
68 this.rawResult = copyOf(rawResult, rawResult.length);
76 * Returns the URI that was requested.
78 * @return The URI that was request
85 * Returns the protocol code of the reply.
87 * @return The protocol code of the reply
89 public int protocolCode() {
94 * Returns the content type of the reply.
96 * @return The content type of the reply
98 public String contentType() {
103 * Returns the raw result of the reply.
105 * @return The raw result of the reply
107 public byte[] rawResult() {
108 return copyOf(rawResult, rawResult.length);
112 public boolean isEmpty() {
113 return rawResult.length == 0;
117 * Returns the decoded content of the reply. This method uses the charset
118 * information from the {@link #contentType()}, if present, or UTF-8 if no
119 * content type is present.
121 * @return The decoded content
123 public String content() {
125 return new String(rawResult(), extractCharset(contentType()));
126 } catch (UnsupportedEncodingException uee1) {
127 throw new RuntimeException(String.format("Could not decode content as %s.", extractCharset(contentType())), uee1);
133 protected String plainText() {
142 * Extracts charset information from the given content type.
145 * The content type response header
146 * @return The extracted charset, or UTF-8 if no charset could be extracted
148 private static String extractCharset(String contentType) {
149 if (contentType == null) {
152 HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
153 NameValuePair charset = headerElement.getParameterByName("charset");
154 return (charset != null) ? charset.getValue() : "ISO-8859-1";
165 public String toString() {
166 return String.format("%s[uri=%s,protocolCode=%d,contentType=%s,rawResult=(%s bytes)]", getClass().getSimpleName(), uri(), protocolCode(), contentType(), rawResult().length);