From 5f81c4af581f877df92f94708f977da8fe474d93 Mon Sep 17 00:00:00 2001 From: skubiak Date: Fri, 9 Jan 2015 08:22:35 +0100 Subject: [PATCH] added validation method --- pom.xml | 8 ++- .../de/svenkubiak/jpushover/JPushover.java | 51 +++++++++++++++++-- .../svenkubiak/jpushover/enums/Constants.java | 3 +- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 8794830..9186333 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 de.svenkubiak jpushover @@ -104,6 +105,11 @@ fluent-hc 4.3.6 + + org.apache.commons + commons-lang3 + 3.3.2 + ch.qos.logback logback-classic diff --git a/src/main/java/de/svenkubiak/jpushover/JPushover.java b/src/main/java/de/svenkubiak/jpushover/JPushover.java index 3d8e65c..1e73a6c 100644 --- a/src/main/java/de/svenkubiak/jpushover/JPushover.java +++ b/src/main/java/de/svenkubiak/jpushover/JPushover.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.List; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.http.Consts; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -204,14 +205,54 @@ public class JPushover { this.pushoverCallback = callback; return this; } + + /** + * Sends a validation request to pushover ensuring that the token and user + * is correct, that there is at least one active device on the account. + * + * Requires token parameter + * Requires user parameter + * Optional device parameter to check specific device + * + * @return true if token and user are valid and at least on device is on the account, false otherwise + */ + public boolean validate() { + Preconditions.checkNotNull(this.pushoverToken, "Token is required for validation"); + Preconditions.checkNotNull(this.pushoverUser, "User is required for validation"); + + List params = Form.form() + .add(Constants.TOKEN.get(), this.pushoverToken) + .add(Constants.USER.get(), this.pushoverUser) + .add(Constants.DEVICE.get(), this.pushoverDevice) + .build(); + + HttpResponse httpResponse = null; + boolean valid = false; + try { + httpResponse = Request.Post(Constants.VALIDATION_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse(); + + if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) { + String response = IOUtils.toString(httpResponse.getEntity().getContent()); + if (StringUtils.isNotBlank(response) && response.contains("\"status\":1")) { + valid = true; + } + } + } catch (IOException e) { + LOG.error("Failed to send validation requeste to pushover", e); + } + + return valid; + } /** - * Send the message to pushover + * Sends a message to pushover + * + * @return JPushoverResponse instance */ public JPushoverResponse push() { - Preconditions.checkNotNull(this.pushoverToken, "Token is required"); - Preconditions.checkNotNull(this.pushoverUser, "User is required"); - Preconditions.checkNotNull(this.pushoverMessage, "Message is required"); + Preconditions.checkNotNull(this.pushoverToken, "Token is required for a message"); + Preconditions.checkNotNull(this.pushoverUser, "User is required for a message"); + Preconditions.checkNotNull(this.pushoverMessage, "Message is required for a message"); if (Priority.EMERGENCY.equals(this.pushoverPriority)) { Preconditions.checkNotNull(this.pushoverRetry, "Retry is required on priority emergency"); @@ -237,7 +278,7 @@ public class JPushover { HttpResponse httpResponse = null; JPushoverResponse jPushoverResponse = null; try { - httpResponse = Request.Post(Constants.PUSHOVER_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse(); + httpResponse = Request.Post(Constants.MESSAGES_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse(); if (httpResponse != null) { int status = httpResponse.getStatusLine().getStatusCode(); diff --git a/src/main/java/de/svenkubiak/jpushover/enums/Constants.java b/src/main/java/de/svenkubiak/jpushover/enums/Constants.java index 874391d..f896a33 100644 --- a/src/main/java/de/svenkubiak/jpushover/enums/Constants.java +++ b/src/main/java/de/svenkubiak/jpushover/enums/Constants.java @@ -6,7 +6,8 @@ package de.svenkubiak.jpushover.enums; * */ public enum Constants { - PUSHOVER_URL("https://api.pushover.net/1/messages.json"), + MESSAGES_URL("https://api.pushover.net/1/messages.json"), + VALIDATION_URL("https://api.pushover.net/1/users/validate.json"), MESSAGE("message"), TITLE("title"), DEVICE("device"),