Don’t send Accept-Encoding header, send User-Agent instead.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / queries / HttpQuery.java
1 /*
2  * Reactor - HttpQuery.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.queries;
19
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22
23 import net.pterodactylus.reactor.Query;
24 import net.pterodactylus.reactor.State;
25 import net.pterodactylus.reactor.states.FailedState;
26 import net.pterodactylus.reactor.states.HttpState;
27
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.protocol.ResponseContentEncoding;
33 import org.apache.http.impl.client.DefaultHttpClient;
34 import org.apache.http.util.EntityUtils;
35
36 import com.google.common.io.Closeables;
37
38 /**
39  * {@link Query} that performs an HTTP GET request to a fixed uri.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class HttpQuery implements Query {
44
45         /** The uri to request. */
46         private final String uri;
47
48         /**
49          * Creates a new HTTP query.
50          *
51          * @param uri
52          *            The uri to request
53          */
54         public HttpQuery(String uri) {
55                 this.uri = uri;
56         }
57
58         //
59         // QUERY METHODS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         @SuppressWarnings("deprecation")
67         public State state() {
68                 DefaultHttpClient httpClient = new DefaultHttpClient();
69                 httpClient.addResponseInterceptor(new ResponseContentEncoding());
70                 HttpGet get = new HttpGet(uri);
71
72                 InputStreamReader inputStreamReader = null;
73                 try {
74                         /* make request. */
75                         get.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11");
76                         HttpResponse response = httpClient.execute(get);
77                         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
78                                 return new FailedState();
79                         }
80                         HttpEntity entity = response.getEntity();
81
82                         /* yay, done! */
83                         return new HttpState(uri, response.getStatusLine().getStatusCode(), entity.getContentType().getValue(), EntityUtils.toByteArray(entity));
84
85                 } catch (IOException ioe1) {
86                         return new FailedState(ioe1);
87                 } finally {
88                         Closeables.closeQuietly(inputStreamReader);
89                 }
90         }
91
92 }