From 1b1e1bb3e990268cd9335dba0b48232e4a201f61 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Sat, 23 Jul 2016 18:46:02 -0400 Subject: [PATCH] Perform better validation on Mandrill responses --- .../frozenorb/apiv3/util/MandrillUtils.java | 25 +++++++++++++++---- .../frozenorb/apiv3/util/MaxMindUtils.java | 4 +-- .../net/frozenorb/apiv3/util/ZangUtils.java | 5 +--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java b/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java index e63b01b..7c02886 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MandrillUtils.java @@ -4,10 +4,13 @@ import com.google.common.net.MediaType; import com.mongodb.async.SingleResultCallback; import io.vertx.core.http.HttpClient; import io.vertx.core.http.HttpHeaders; +import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.APIv3; +import java.io.IOException; + @UtilityClass public class MandrillUtils { @@ -15,16 +18,28 @@ public class MandrillUtils { private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient(); public static void sendEmail(JsonObject message, SingleResultCallback callback) { - JsonObject body = new JsonObject() + JsonObject requestBody = new JsonObject() .put("key", mandrillApiKey) .put("message", message); httpClient.post("mandrillapp.com", "/api/1.0/messages/send.json", (response) -> { - response.bodyHandler((ignored) -> callback.onResult(null, null)); + response.bodyHandler((responseBody) -> { + try { + JsonArray bodyJson = new JsonArray(responseBody.toString()); + JsonObject emailJson = bodyJson.getJsonObject(0); + String emailStatus = emailJson.getString("status"); + + if (emailStatus.equals("rejected") || emailStatus.equals("invalid")) { + callback.onResult(null, new IOException("Illegal email status while reading Mandrill response: " + emailStatus + " (" + emailJson.encode() + ")")); + } else { + callback.onResult(null, null); + } + } catch (Exception ex) { + callback.onResult(null, new IOException("Failed to process Mandrill response: " + responseBody, ex)); + } + }); response.exceptionHandler((error) -> callback.onResult(null, error)); - }) - .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) - .end(body.encode()); + }).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).end(requestBody.encode()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java index 3866fe4..98db270 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MaxMindUtils.java @@ -62,9 +62,7 @@ public class MaxMindUtils { future.fail(error); } }); - }) - .putHeader("Authorization", authHeader) - .end(); + }).putHeader("Authorization", authHeader).end(); }).setHandler((result) -> { if (result.failed()) { callback.onResult(null, result.cause()); diff --git a/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java b/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java index f9638aa..aae9e40 100644 --- a/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/ZangUtils.java @@ -36,10 +36,7 @@ public class ZangUtils { }); response.exceptionHandler((error) -> callback.onResult(null, error)); - }) - .putHeader("Authorization", authHeader) - .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) - .end("To=" + to + "&From=339-337-5300&Body=" + messageBody); + }).putHeader("Authorization", authHeader).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).end("To=" + to + "&From=339-337-5300&Body=" + messageBody); } public static void getCarrierInfo(String phoneNumber, SingleResultCallback callback) {