🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / queries / HttpQuery.java
1 /*
2  * Rhynodge - 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.rhynodge.queries;
19
20 import java.io.IOException;
21 import java.util.concurrent.TimeUnit;
22
23 import net.pterodactylus.rhynodge.Query;
24 import net.pterodactylus.rhynodge.State;
25 import net.pterodactylus.rhynodge.states.FailedState;
26 import net.pterodactylus.rhynodge.states.HttpState;
27
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpHost;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.protocol.ResponseContentEncoding;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.params.HttpConnectionParams;
37 import org.apache.http.util.EntityUtils;
38
39 /**
40  * {@link Query} that performs an HTTP GET request to a fixed uri.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
43  */
44 public class HttpQuery implements Query {
45
46         private final String uri;
47         private final String proxyHost;
48         private final int proxyPort;
49
50         public HttpQuery(String uri) {
51                 this(uri, null, -1);
52         }
53
54         public HttpQuery(String uri, String proxyHost, int proxyPort) {
55                 this.uri = uri;
56                 this.proxyHost = proxyHost;
57                 this.proxyPort = proxyPort;
58         }
59
60         //
61         // QUERY METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         @SuppressWarnings("deprecation")
69         public State state() {
70                 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
71                                 .setSSLHostnameVerifier((hostname, session) -> true)
72                                 .addInterceptorFirst(new ResponseContentEncoding());
73                 if ((proxyHost != null) && (proxyPort != -1)) {
74                         httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
75                 }
76                 HttpClient httpClient = httpClientBuilder.build();
77                 HttpGet get = new HttpGet(uri);
78
79                 try {
80                         /* make request. */
81                         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");
82                         HttpConnectionParams.setConnectionTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300));
83                         HttpConnectionParams.setSoTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300));
84                         HttpResponse response = httpClient.execute(get);
85                         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
86                                 return new FailedState(new IllegalStateException(String.format("Invalid HTTP Status: %d", response.getStatusLine().getStatusCode())));
87                         }
88                         HttpEntity entity = response.getEntity();
89
90                         /* yay, done! */
91                         return new HttpState(uri, response.getStatusLine().getStatusCode(), entity.getContentType().getValue(), EntityUtils.toByteArray(entity));
92
93                 } catch (IOException ioe1) {
94                         return new FailedState(ioe1);
95                 }
96         }
97
98 }