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