Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 74fb02b3a6d0a95951e8c6c9140e11ce72d5c88c Andrew Kirilenko committed Jan 30, 2012
@@ -0,0 +1,83 @@
+package io.iron.ironmq;
+
+import groovy.json.JsonSlurper;
+
+public class Client {
+ static final private String proto = "https";
+ static final private String host = "mq-aws-us-east-1.iron.io";
+ static final private int port = 443;
+ static final private String apiVersion = "1";
+
+ private String projectId;
+ private String token;
+
+ /**
+ * Constructs a new Client using the specified project ID and token.
+ * The network is not accessed during construction and this call will
+ * succeed even if the credentials are invalid.
+ *
+ * @param projectId A 24-character project ID.
+ * @param token An OAuth token.
+ */
+ public Client(String projectId, String token) {
+ this.projectId = projectId;
+ this.token = token;
+ }
+
+ /**
+ * Returns a Queue using the given name.
+ * The network is not accessed during this call.
+ *
+ * @param name The name of the Queue to create.
+ */
+ public Queue queue(String name) {
+ return new Queue(this, name);
+ }
+
+ Map doDelete(String endpoint) throws IOException {
+ return doRequest("DELETE", endpoint, null);
+ }
+
+ Map doGet(String endpoint) throws IOException {
+ return doRequest("GET", endpoint, null);
+ }
+
+ Map doPost(String endpoint, String body) throws IOException {
+ return doRequest("POST", endpoint, body);
+ }
+
+ private Map doRequest(String method, String endpoint, String body) throws IOException {
+ String path = "/" + apiVersion + "/projects/" + projectId + "/" + endpoint;
+ URL url = new URL(proto, host, port, path);
+
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod(method);
+ conn.setRequestProperty("Content-Type", "application/json");
+ conn.setRequestProperty("Authorization", "OAuth " + token);
+ conn.setRequestProperty("User-Agent", "IronMQ Java Client");
+
+ if (body != null) {
+ conn.setDoOutput(true);
+ }
+
+ conn.connect();
+
+ if (body != null) {
+ OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
+ out.write(body);
+ out.flush();
+ }
+
+ int status = conn.getResponseCode();
+ if (status != 200) {
+ String jsonString = conn.getInputStream().getText();
+ Map jsonObj = (Map)((new JsonSlurper()).parseText(jsonString));
+ String msg = (String)jsonObj.get("msg");
+ throw new HTTPException(status, msg);
+ }
+
+ String jsonString = conn.getInputStream().getText();
+ Map jsonObj = (Map)((new JsonSlurper()).parseText(jsonString));
+ return jsonObj;
+ }
+}
@@ -0,0 +1,24 @@
+package io.iron.ironmq;
+
+/**
+ * The HTTPException class represents an HTTP response status that is not 200 OK.
+ */
+public class HTTPException extends IOException {
+ private int status;
+
+ /**
+ * Creates a new HTTPException.
+ *
+ * @param status The HTTP status code.
+ * @param message The text of the HTTP status code.
+ */
+ public HTTPException(int status, String message) {
+ super(message);
+ this.status = status;
+ }
+
+ /**
+ * Returns the HTTP response's status code.
+ */
+ public int getStatusCode() { return this.status; }
+}
@@ -0,0 +1,53 @@
+package io.iron.ironmq;
+
+/**
+ * The Message class represents a message retrieved from an IronMQ queue.
+ */
+public class Message {
+ private String id;
+ private String body;
+ private long timeout;
+
+ public Message() {}
+
+ /**
+ * Returns the Message's body contents.
+ */
+ public String getBody() { return body; }
+
+ /**
+ * Sets the Message's body contents.
+ *
+ * @param body The new body contents.
+ */
+ public void setBody(String body) { this.body = body; }
+
+ /**
+ * Returns the Message's ID.
+ */
+ public String getId() { return id; }
+
+ /**
+ * Sets the Message's ID.
+ *
+ * @param id The new ID.
+ */
+ public void setId(String id) { this.id = id; }
+
+ /**
+ * Returns the Message's timeout.
+ */
+ public long getTimeout() { return timeout; }
+
+ /**
+ * Sets the Message's timeout.
+ *
+ * @param timeout The new timeout.
+ */
+ public void setTimeout(long timeout) { this.timeout = timeout; }
+
+ /**
+ * Returns a string representation of the Message.
+ */
+ public String toString() { return body; }
+}
@@ -0,0 +1,86 @@
+package io.iron.ironmq;
+
+import groovy.json.JsonBuilder;
+
+/**
+ * The Queue class represents a specific IronMQ queue bound to a client.
+ */
+public class Queue {
+ final private Client client;
+ final private String name;
+
+ Queue(Client client, String name) {
+ this.client = client;
+ this.name = name;
+ }
+
+ /**
+ * Retrieves a Message from the queue. If there are no items on the queue, an
+ * HTTPException is thrown.
+ *
+ * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
+ * @throws IOException If there is an error accessing the IronMQ server.
+ */
+ public Message get() throws IOException {
+ Map jsonObj = client.doGet("queues/" + name + "/messages");
+ Message msg = new Message();
+ msg.setId((String)jsonObj.get("id"));
+ msg.setBody((String)jsonObj.get("body"));
+ if (jsonObj.get("timeout") != null) {
+ msg.setTimeout((Long)jsonObj.get("timeout"));
+ }
+ return msg;
+ }
+
+ /**
+ * Deletes a Message from the queue.
+ *
+ * @param id The ID of the message to delete.
+ *
+ * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
+ * @throws IOException If there is an error accessing the IronMQ server.
+ */
+ public void deleteMessage(String id) throws IOException {
+ client.doDelete("queues/" + name + "/messages/" + id);
+ }
+
+ /**
+ * Deletes a Message from the queue.
+ *
+ * @param msg The message to delete.
+ *
+ * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
+ * @throws IOException If there is an error accessing the IronMQ server.
+ */
+ public void deleteMessage(Message msg) throws IOException {
+ deleteMessage(msg.getId());
+ }
+
+ /**
+ * Pushes a message onto the queue.
+ *
+ * @param msg The body of the message to push.
+ *
+ * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
+ * @throws IOException If there is an error accessing the IronMQ server.
+ */
+ public void push(String msg) throws IOException {
+ push(msg, 0);
+ }
+
+ /**
+ * Pushes a message onto the queue.
+ *
+ * @param msg The body of the message to push.
+ * @param timeout The timeout of the message to push.
+ *
+ * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
+ * @throws IOException If there is an error accessing the IronMQ server.
+ */
+ public void push(String msg, long timeout) throws IOException {
+ JsonBuilder builder = new JsonBuilder();
+ builder body: msg, timeout: timeout;
+ String jsonStr = builder.toString();
+ client.doPost("queues/" + name + "/messages", jsonStr);
+ }
+}

0 comments on commit 74fb02b

Please sign in to comment.