Add HTTP query and state.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 2 Jan 2013 14:54:41 +0000 (15:54 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 2 Jan 2013 20:14:37 +0000 (21:14 +0100)
pom.xml
src/main/java/net/pterodactylus/reactor/queries/HttpQuery.java [new file with mode: 0644]
src/main/java/net/pterodactylus/reactor/states/HttpState.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 58e6a00..3d6265c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                </dependency>
+               <dependency>
+                       <groupId>org.apache.httpcomponents</groupId>
+                       <artifactId>httpclient</artifactId>
+                       <version>4.2.2</version>
+               </dependency>
        </dependencies>
 </project>
diff --git a/src/main/java/net/pterodactylus/reactor/queries/HttpQuery.java b/src/main/java/net/pterodactylus/reactor/queries/HttpQuery.java
new file mode 100644 (file)
index 0000000..c514084
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Reactor - HttpQuery.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.reactor.queries;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import net.pterodactylus.reactor.Query;
+import net.pterodactylus.reactor.State;
+import net.pterodactylus.reactor.states.FailedState;
+import net.pterodactylus.reactor.states.HttpState;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+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.util.EntityUtils;
+
+import com.google.common.io.Closeables;
+
+/**
+ * {@link Query} that performs an HTTP GET request to a fixed uri.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class HttpQuery implements Query {
+
+       /** The uri to request. */
+       private final String uri;
+
+       /**
+        * Creates a new HTTP query.
+        *
+        * @param uri
+        *            The uri to request
+        */
+       public HttpQuery(String uri) {
+               this.uri = uri;
+       }
+
+       //
+       // QUERY METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("deprecation")
+       public State state() {
+               DefaultHttpClient httpClient = new DefaultHttpClient();
+               httpClient.addResponseInterceptor(new ResponseContentEncoding());
+               HttpGet get = new HttpGet(uri);
+
+               InputStreamReader inputStreamReader = null;
+               try {
+                       /* make request. */
+                       get.addHeader("Accept-Encoding", "identity");
+                       HttpResponse response = httpClient.execute(get);
+                       if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+                               return new FailedState();
+                       }
+                       HttpEntity entity = response.getEntity();
+
+                       /* yay, done! */
+                       return new HttpState(uri, response.getStatusLine().getStatusCode(), entity.getContentType().getValue(), EntityUtils.toByteArray(entity));
+
+               } catch (IOException ioe1) {
+                       return new FailedState(ioe1);
+               } finally {
+                       Closeables.closeQuietly(inputStreamReader);
+               }
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/reactor/states/HttpState.java b/src/main/java/net/pterodactylus/reactor/states/HttpState.java
new file mode 100644 (file)
index 0000000..38ca80c
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Reactor - HttpState.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.reactor.states;
+
+import java.io.UnsupportedEncodingException;
+
+import net.pterodactylus.reactor.State;
+import net.pterodactylus.reactor.queries.HttpQuery;
+
+import org.apache.http.HeaderElement;
+import org.apache.http.NameValuePair;
+import org.apache.http.message.BasicHeaderValueParser;
+
+/**
+ * {@link State} that contains the results of an {@link HttpQuery}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class HttpState extends AbstractState {
+
+       /** The URI that was requested. */
+       private final String uri;
+
+       /** The protocol code. */
+       private final int protocolCode;
+
+       /** The content type. */
+       private final String contentType;
+
+       /** The result. */
+       private final byte[] rawResult;
+
+       /**
+        * Creates a new HTTP state.
+        *
+        * @param uri
+        *            The URI that was requested
+        * @param protocolCode
+        *            The code of the reply
+        * @param contentType
+        *            The content type of the reply
+        * @param rawResult
+        *            The raw result
+        */
+       public HttpState(String uri, int protocolCode, String contentType, byte[] rawResult) {
+               this.uri = uri;
+               this.protocolCode = protocolCode;
+               this.contentType = contentType;
+               this.rawResult = rawResult;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the URI that was requested.
+        *
+        * @return The URI that was request
+        */
+       public String uri() {
+               return uri;
+       }
+
+       /**
+        * Returns the protocol code of the reply.
+        *
+        * @return The protocol code of the reply
+        */
+       public int protocolCode() {
+               return protocolCode;
+       }
+
+       /**
+        * Returns the content type of the reply.
+        *
+        * @return The content type of the reply
+        */
+       public String contentType() {
+               return contentType;
+       }
+
+       /**
+        * Returns the raw result of the reply.
+        *
+        * @return The raw result of the reply
+        */
+       public byte[] rawResult() {
+               return rawResult;
+       }
+
+       /**
+        * Returns the decoded content of the reply. This method uses the charset
+        * information from the {@link #contentType()}, if present, or UTF-8 if no
+        * content type is present.
+        *
+        * @return The decoded content
+        */
+       public String content() {
+               try {
+                       return new String(rawResult(), extractCharset(contentType()));
+               } catch (UnsupportedEncodingException uee1) {
+                       throw new RuntimeException(String.format("Could not decode content as %s.", extractCharset(contentType())), uee1);
+               }
+       }
+
+       //
+       // STATIC METHODS
+       //
+
+       /**
+        * Extracts charset information from the given content type.
+        *
+        * @param contentType
+        *            The content type response header
+        * @return The extracted charset, or UTF-8 if no charset could be extracted
+        */
+       private static String extractCharset(String contentType) {
+               if (contentType == null) {
+                       return "UTF-8";
+               }
+               HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser());
+               NameValuePair charset = headerElement.getParameterByName("charset");
+               return (charset != null) ? charset.getValue() : "UTF-8";
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String toString() {
+               return String.format("%s[uri=%s,protocolCode=%d,contentType=%s,rawResult=(%s bytes)]", getClass().getSimpleName(), uri(), protocolCode(), contentType(), rawResult().length);
+       }
+
+}