ea733f743bf89899973f1b602033a3f7a4831f43
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / HttpState.java
1 /*
2  * Rhynodge - HttpState.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.states;
19
20 import static java.util.Arrays.copyOf;
21
22 import java.io.UnsupportedEncodingException;
23
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.queries.HttpQuery;
26
27 import org.apache.http.HeaderElement;
28 import org.apache.http.NameValuePair;
29 import org.apache.http.message.BasicHeaderValueParser;
30
31 /**
32  * {@link State} that contains the results of an {@link HttpQuery}.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class HttpState extends AbstractState {
37
38         /** The URI that was requested. */
39         private final String uri;
40
41         /** The protocol code. */
42         private final int protocolCode;
43
44         /** The content type. */
45         private final String contentType;
46
47         /** The result. */
48         private final byte[] rawResult;
49
50         /**
51          * Creates a new HTTP state.
52          *
53          * @param uri
54          *            The URI that was requested
55          * @param protocolCode
56          *            The code of the reply
57          * @param contentType
58          *            The content type of the reply
59          * @param rawResult
60          *            The raw result
61          */
62         public HttpState(String uri, int protocolCode, String contentType, byte[] rawResult) {
63                 this.uri = uri;
64                 this.protocolCode = protocolCode;
65                 this.contentType = contentType;
66                 this.rawResult = copyOf(rawResult, rawResult.length);
67         }
68
69         //
70         // ACCESSORS
71         //
72
73         /**
74          * Returns the URI that was requested.
75          *
76          * @return The URI that was request
77          */
78         public String uri() {
79                 return uri;
80         }
81
82         /**
83          * Returns the protocol code of the reply.
84          *
85          * @return The protocol code of the reply
86          */
87         public int protocolCode() {
88                 return protocolCode;
89         }
90
91         /**
92          * Returns the content type of the reply.
93          *
94          * @return The content type of the reply
95          */
96         public String contentType() {
97                 return contentType;
98         }
99
100         /**
101          * Returns the raw result of the reply.
102          *
103          * @return The raw result of the reply
104          */
105         public byte[] rawResult() {
106                 return copyOf(rawResult, rawResult.length);
107         }
108
109         @Override
110         public boolean isEmpty() {
111                 return rawResult.length == 0;
112         }
113
114         /**
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.
118          *
119          * @return The decoded content
120          */
121         public String content() {
122                 try {
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);
126                 }
127         }
128
129         //
130         // STATIC METHODS
131         //
132
133         /**
134          * Extracts charset information from the given content type.
135          *
136          * @param contentType
137          *            The content type response header
138          * @return The extracted charset, or UTF-8 if no charset could be extracted
139          */
140         private static String extractCharset(String contentType) {
141                 if (contentType == null) {
142                         return "ISO-8859-1";
143                 }
144                 HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
145                 NameValuePair charset = headerElement.getParameterByName("charset");
146                 return (charset != null) ? charset.getValue() : "ISO-8859-1";
147         }
148
149         //
150         // OBJECT METHODS
151         //
152
153         /**
154          * {@inheritDoc}
155          */
156         @Override
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);
159         }
160
161 }