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