Use proxy to access The Pirate Bay
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / queries / HttpQuery.java
index 362a04b..c1c315f 100644 (file)
@@ -18,7 +18,6 @@
 package net.pterodactylus.rhynodge.queries;
 
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.concurrent.TimeUnit;
 
 import net.pterodactylus.rhynodge.Query;
@@ -27,18 +26,16 @@ import net.pterodactylus.rhynodge.states.FailedState;
 import net.pterodactylus.rhynodge.states.HttpState;
 
 import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.protocol.ResponseContentEncoding;
-import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.util.EntityUtils;
 
-import com.google.common.io.Closeables;
-
 /**
  * {@link Query} that performs an HTTP GET request to a fixed uri.
  *
@@ -46,17 +43,18 @@ import com.google.common.io.Closeables;
  */
 public class HttpQuery implements Query {
 
-       /** The uri to request. */
        private final String uri;
+       private final String proxyHost;
+       private final int proxyPort;
 
-       /**
-        * Creates a new HTTP query.
-        *
-        * @param uri
-        *            The uri to request
-        */
        public HttpQuery(String uri) {
+               this(uri, null, -1);
+       }
+
+       public HttpQuery(String uri, String proxyHost, int proxyPort) {
                this.uri = uri;
+               this.proxyHost = proxyHost;
+               this.proxyPort = proxyPort;
        }
 
        //
@@ -69,12 +67,15 @@ public class HttpQuery implements Query {
        @Override
        @SuppressWarnings("deprecation")
        public State state() {
-               HttpClient httpClient = HttpClientBuilder.create()
+               HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
                                .setSSLHostnameVerifier((hostname, session) -> true)
-                               .addInterceptorFirst(new ResponseContentEncoding()).build();
+                               .addInterceptorFirst(new ResponseContentEncoding());
+               if ((proxyHost != null) && (proxyPort != -1)) {
+                       httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
+               }
+               HttpClient httpClient = httpClientBuilder.build();
                HttpGet get = new HttpGet(uri);
 
-               InputStreamReader inputStreamReader = null;
                try {
                        /* make request. */
                        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,7 +83,7 @@ public class HttpQuery implements Query {
                        HttpConnectionParams.setSoTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300));
                        HttpResponse response = httpClient.execute(get);
                        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
-                               return new FailedState();
+                               return new FailedState(new IllegalStateException(String.format("Invalid HTTP Status: %d", response.getStatusLine().getStatusCode())));
                        }
                        HttpEntity entity = response.getEntity();
 
@@ -91,8 +92,6 @@ public class HttpQuery implements Query {
 
                } catch (IOException ioe1) {
                        return new FailedState(ioe1);
-               } finally {
-                       Closeables.closeQuietly(inputStreamReader);
                }
        }