362a04bf682a7b48053c36edc11bf439fb1d38fa
[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.io.InputStreamReader;
22 import java.util.concurrent.TimeUnit;
23
24 import net.pterodactylus.rhynodge.Query;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.states.FailedState;
27 import net.pterodactylus.rhynodge.states.HttpState;
28
29 import org.apache.http.HttpEntity;
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.DefaultHttpClient;
36 import org.apache.http.impl.client.HttpClientBuilder;
37 import org.apache.http.params.HttpConnectionParams;
38 import org.apache.http.util.EntityUtils;
39
40 import com.google.common.io.Closeables;
41
42 /**
43  * {@link Query} that performs an HTTP GET request to a fixed uri.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class HttpQuery implements Query {
48
49         /** The uri to request. */
50         private final String uri;
51
52         /**
53          * Creates a new HTTP query.
54          *
55          * @param uri
56          *            The uri to request
57          */
58         public HttpQuery(String uri) {
59                 this.uri = uri;
60         }
61
62         //
63         // QUERY METHODS
64         //
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         @SuppressWarnings("deprecation")
71         public State state() {
72                 HttpClient httpClient = HttpClientBuilder.create()
73                                 .setSSLHostnameVerifier((hostname, session) -> true)
74                                 .addInterceptorFirst(new ResponseContentEncoding()).build();
75                 HttpGet get = new HttpGet(uri);
76
77                 InputStreamReader inputStreamReader = null;
78                 try {
79                         /* make request. */
80                         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");
81                         HttpConnectionParams.setConnectionTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300));
82                         HttpConnectionParams.setSoTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300));
83                         HttpResponse response = httpClient.execute(get);
84                         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
85                                 return new FailedState();
86                         }
87                         HttpEntity entity = response.getEntity();
88
89                         /* yay, done! */
90                         return new HttpState(uri, response.getStatusLine().getStatusCode(), entity.getContentType().getValue(), EntityUtils.toByteArray(entity));
91
92                 } catch (IOException ioe1) {
93                         return new FailedState(ioe1);
94                 } finally {
95                         Closeables.closeQuietly(inputStreamReader);
96                 }
97         }
98
99 }