diff --git a/src/main/java/de/svenkubiak/jpushover/apis/Glance.java b/src/main/java/de/svenkubiak/jpushover/apis/Glance.java index 4b62968..5c33411 100644 --- a/src/main/java/de/svenkubiak/jpushover/apis/Glance.java +++ b/src/main/java/de/svenkubiak/jpushover/apis/Glance.java @@ -17,35 +17,51 @@ import de.svenkubiak.jpushover.utils.Validate; * */ public class Glance { - private String token; - private String user; - private String device; - private String title; - private String text; - private String subtext; + private NavigableMap body = new TreeMap<>(); private String proxyHost; - private int count; - private int percent; private int proxyPort; + /** + * Your application's API token + * (required) + * + * @param token The pushover API token + * @return Glance instance + */ public Glance withToken(String token) { Objects.requireNonNull(token, "token can not be null"); - this.token = token; + body.put(Param.TOKEN.toString(), token); return this; } + /** + * The user/group key (not e-mail address) of your user (or you), + * viewable when logged into the @see pushover dashboard + * (required) + * + * @param user The username + * @return Glance instance + */ public Glance withUser(String user) { Objects.requireNonNull(user, "user can not be null"); - this.user = user; + body.put(Param.USER.toString(), user); return this; } + /** + * Your user's device name to send the message directly to that device, + * rather than all of the user's devices + * (optional) + * + * @param device The device name + * @return Glance instance + */ public Glance withDevice(String device) { Objects.requireNonNull(device, "device can not be null"); - this.device = device; + body.put(Param.DEVICE.toString(), device); return this; } @@ -59,7 +75,7 @@ public class Glance { Objects.requireNonNull(title, "title can not be null"); Validate.checkArgument(title.length() <= 100, "Title must not exceed a length of 100 characters"); - this.title = title; + body.put(Param.TITLE.toString(), title); return this; } @@ -73,7 +89,7 @@ public class Glance { Objects.requireNonNull(text, "text can not be null"); Validate.checkArgument(text.length() <= 100, "Text must not exceed a length of 100 characters"); - this.text = text; + body.put(Param.TEXT.toString(), text); return this; } @@ -87,7 +103,7 @@ public class Glance { Objects.requireNonNull(subtext, "subtext can not be null"); Validate.checkArgument(subtext.length() <= 100, "Subtext must not exceed a length of 100 characters"); - this.subtext = subtext; + body.put(Param.SUBTEXT.toString(), subtext); return this; } @@ -98,7 +114,7 @@ public class Glance { * @return Glance instance */ public Glance withCount(int count) { - this.count = count; + body.put(Param.COUNT.toString(), String.valueOf(count)); return this; } @@ -109,7 +125,7 @@ public class Glance { * @return GLance instance */ public Glance withPercent(int percent) { - this.percent = percent; + body.put(Param.PERCENT.toString(), String.valueOf(percent)); return this; } @@ -122,19 +138,8 @@ public class Glance { * @throws InterruptedException if sending the message fails */ public PushoverResponse push() throws IOException, InterruptedException { - Objects.requireNonNull(this.token, "Token is required for a glance"); - Objects.requireNonNull(this.user, "User is required for a glance"); - - NavigableMap body = new TreeMap<>(); - body.put(Param.TOKEN.toString(), this.token); - body.put(Param.USER.toString(), this.user); - body.put(Param.DEVICE.toString(), this.device); - body.put(Param.TITLE.toString(), this.title); - body.put(Param.TEXT.toString(), this.text); - body.put(Param.SUBTEXT.toString(), this.subtext); - body.put(Param.COUNT.toString(), String.valueOf(this.count)); - body.put(Param.PERCENT.toString(), String.valueOf(this.percent)); - + Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for a glance"); + Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for a glance"); return new PushoverRequest().push(Url.GLANCES.toString(), body, this.proxyHost, this.proxyPort); } diff --git a/src/main/java/de/svenkubiak/jpushover/apis/Message.java b/src/main/java/de/svenkubiak/jpushover/apis/Message.java index a9be5c0..eb22f7a 100644 --- a/src/main/java/de/svenkubiak/jpushover/apis/Message.java +++ b/src/main/java/de/svenkubiak/jpushover/apis/Message.java @@ -19,25 +19,13 @@ import de.svenkubiak.jpushover.utils.Validate; * */ public class Message { - private Priority priority = Priority.NORMAL; - private Sound sound = Sound.PUSHOVER; - private String token; - private String user; - private String message; - private String device; - private String title; - private String url = ""; - private String urlTitle = ""; - private String callback; + private NavigableMap body = new TreeMap<>(); private String proxyHost; private int proxyPort; - private int retry; - private int expire; - private int timestamp; - private boolean html; - private boolean monospace; public Message() { + this.withPriority(Priority.NORMAL); + this.withSound(Sound.PUSHOVER); } /** @@ -47,8 +35,8 @@ public class Message { * @param token The pushover API token * @return Message instance */ - public final Message withToken(final String token) { - this.token = token; + public final Message withToken(String token) { + body.put(Param.TOKEN.toString(), token); return this; } @@ -60,8 +48,8 @@ public class Message { * @param user The username * @return Message instance */ - public final Message withUser(final String user) { - this.user = user; + public final Message withUser(String user) { + body.put(Param.USER.toString(), user); return this; } @@ -73,8 +61,8 @@ public class Message { * @param retry Number of seconds * @return Message instance */ - public final Message withRetry(final int retry) { - this.retry = retry; + public final Message withRetry(int retry) { + body.put(Param.RETRY.toString(), String.valueOf(retry)); return this; } @@ -86,8 +74,8 @@ public class Message { * @param expire Number of seconds * @return Message instance */ - public final Message withExpire(final int expire) { - this.expire = expire; + public final Message withExpire(int expire) { + body.put(Param.EXPIRE.toString(), String.valueOf(expire)); return this; } @@ -98,8 +86,8 @@ public class Message { * @param message The message to sent * @return Message instance */ - public final Message withMessage(final String message) { - this.message = message; + public final Message withMessage(String message) { + body.put(Param.MESSAGE.toString(), message); return this; } @@ -111,8 +99,8 @@ public class Message { * @param device The device name * @return Message instance */ - public final Message withDevice(final String device) { - this.device = device; + public final Message withDevice(String device) { + body.put(Param.DEVICE.toString(), device); return this; } @@ -123,8 +111,8 @@ public class Message { * @param title The title * @return Message instance */ - public final Message withTitle(final String title) { - this.title = title; + public final Message withTitle(String title) { + body.put(Param.TITLE.toString(), title); return this; } @@ -135,9 +123,9 @@ public class Message { * @param url The url * @return Message instance */ - public final Message withUrl(final String url) { - this.url = url; - this.urlTitle = url; + public final Message withUrl(String url) { + body.put(Param.URL.toString(), url); + body.put(Param.URL_TITLE.toString(), url); return this; } @@ -149,8 +137,8 @@ public class Message { * @return Message instance */ public final Message enableMonospace() { - this.monospace = true; - this.html = false; + body.put(Param.MONOSPACE.toString(), "1"); + body.put(Param.HTML.toString(), "0"); return this; } @@ -162,8 +150,8 @@ public class Message { * @return Message instance */ public final Message enableHtml() { - this.monospace = false; - this.html = true; + body.put(Param.MONOSPACE.toString(), "0"); + body.put(Param.HTML.toString(), "1"); return this; } @@ -174,8 +162,8 @@ public class Message { * @param urlTitle The url title * @return Message instance */ - public final Message withUrlTitle(final String urlTitle) { - this.urlTitle = urlTitle; + public final Message withUrlTitle(String urlTitle) { + body.put(Param.URL_TITLE.toString(), urlTitle); return this; } @@ -187,8 +175,8 @@ public class Message { * @param timestamp The Unix timestamp * @return Message instance */ - public final Message withTimestamp(final int timestamp) { - this.timestamp = timestamp; + public final Message withTimestamp(int timestamp) { + body.put(Param.TIMESTAMP.toString(), String.valueOf(timestamp)); return this; } @@ -199,8 +187,8 @@ public class Message { * @param priority The priority enum * @return Message instance */ - public final Message withPriority(final Priority priority) { - this.priority = priority; + public final Message withPriority(Priority priority) { + body.put(Param.PRIORITY.toString(), priority.toString()); return this; } @@ -212,8 +200,8 @@ public class Message { * @param sound THe sound enum * @return Message instance */ - public final Message withSound(final Sound sound) { - this.sound = sound; + public final Message withSound(Sound sound) { + body.put(Param.SOUND.toString(), sound.toString()); return this; } @@ -227,8 +215,8 @@ public class Message { * @param callback The callback URL * @return Message instance */ - public final Message withCallback(final String callback) { - this.callback = callback; + public final Message withCallback(String callback) { + body.put(Param.CALLBACK.toString(), callback); return this; } @@ -239,7 +227,7 @@ public class Message { * @param proxyPort The port that should be used for the Proxy * @return Message instance */ - public final Message withProxy(final String proxyHost, final int proxyPort) { + public final Message withProxy(String proxyHost, int proxyPort) { this.proxyHost = proxyHost; this.proxyPort = proxyPort; return this; @@ -259,12 +247,8 @@ public class Message { * @throws InterruptedException if validation fails */ public boolean validate() throws IOException, InterruptedException { - Objects.requireNonNull(this.token, "Token is required for validation"); - Objects.requireNonNull(this.user, "User is required for validation"); - - NavigableMap body = new TreeMap<>(); - body.put(Param.TOKEN.toString(), this.token); - body.put(Param.USER.toString(), this.user); + Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for validation"); + Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for validation"); var pushoverResponse = new PushoverRequest().push(Url.VALIDATE.toString(), body, this.proxyHost, this.proxyPort); @@ -288,136 +272,33 @@ public class Message { * @throws InterruptedException if sending the message fails */ public final PushoverResponse push() throws IOException, InterruptedException { - Objects.requireNonNull(this.token, "Token is required for a message"); - Objects.requireNonNull(this.user, "User is required for a message"); - Objects.requireNonNull(this.message, "Message is required for a message"); - Validate.checkArgument(this.message.length() <= 1024, "Message can not exceed more than 1024 characters"); + Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for validation"); + Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for validation"); + Objects.requireNonNull(body.get(Param.MESSAGE.toString()), "Message is required for a message"); + Validate.checkArgument(body.get(Param.MESSAGE.toString()).length() <= 1024, "Message can not exceed more than 1024 characters"); - if (Priority.EMERGENCY.equals(this.priority)) { - if (this.retry == 0) { - this.retry = 60; + if (Priority.EMERGENCY.toString().equals(body.get(Param.PRIORITY.toString()))) { + if (body.get(Param.RETRY.toString()) == null) { + body.put(Param.RETRY.toString(), "60"); } - if (this.expire == 0) { - this.expire = 3600; + if (body.get(Param.EXPIRE.toString()) == null) { + body.put(Param.EXPIRE.toString(), "3600"); } } - if (this.title != null) { - Validate.checkArgument(this.title.length() <= 250, "Title can not exceed more than 250 characters"); + if (body.get(Param.TITLE.toString()) != null) { + Validate.checkArgument(body.get(Param.TITLE.toString()).length() <= 250, "Title can not exceed more than 250 characters"); } - if (this.url != null) { - Validate.checkArgument(this.url.length() <= 512, "URL can not exceed more than 512 characters"); + if (body.get(Param.URL.toString()) != null) { + Validate.checkArgument(body.get(Param.URL.toString()).length() <= 512, "URL can not exceed more than 512 characters"); } - if (this.urlTitle != null) { - Validate.checkArgument(this.urlTitle.length() <= 100, "URL Title can not exceed more than 100 characters"); - } - - NavigableMap body = new TreeMap<>(); - body.put(Param.TOKEN.toString(), this.token); - body.put(Param.USER.toString(), this.user); - body.put(Param.MESSAGE.toString(), this.message); - body.put(Param.URL.toString(), this.url); - body.put(Param.URL_TITLE.toString(), this.urlTitle); - body.put(Param.PRIORITY.toString(), this.priority.toString()); - body.put(Param.SOUND.toString(), this.sound.toString()); - body.put(Param.HTML.toString(), this.html ? "1" : "0"); - body.put(Param.MONOSPACE.toString(), this.monospace ? "1" : "0"); - - if (this.device != null) { - body.put(Param.DEVICE.toString(), this.device); - } - - if (this.title != null) { - body.put(Param.TITLE.toString(), this.title); - } - - if (this.callback != null) { - body.put(Param.CALLBACK.toString(), this.callback); - } - - if (this.timestamp > 0) { - body.put(Param.TIMESTAMP.toString(), String.valueOf(this.timestamp)); - } - - if (this.retry > 0) { - body.put(Param.RETRY.toString(), String.valueOf(this.retry)); - } - - if (this.expire > 0) { - body.put(Param.EXPIRE.toString(), String.valueOf(this.expire)); + if (body.get(Param.URL_TITLE.toString()) != null) { + Validate.checkArgument(body.get(Param.URL_TITLE.toString()).length() <= 100, "URL Title can not exceed more than 100 characters"); } return new PushoverRequest().push(Url.MESSAGES.toString(), body, this.proxyHost, this.proxyPort); } - - public Priority getPriority() { - return priority; - } - - public Sound getSound() { - return sound; - } - - public String getToken() { - return token; - } - - public String getUser() { - return user; - } - - public String getMessage() { - return message; - } - - public String getDevice() { - return device; - } - - public String getTitle() { - return title; - } - - public String getUrl() { - return url; - } - - public String getUrlTitle() { - return urlTitle; - } - - public int getTimestamp() { - return timestamp; - } - - public int getRetry() { - return retry; - } - - public int getExpire() { - return expire; - } - - public String getCallback() { - return callback; - } - - public String getProxyHost() { - return proxyHost; - } - - public int getProxyPort() { - return proxyPort; - } - - public boolean isHtml() { - return html; - } - - public boolean isMonospace() { - return monospace; - } } \ No newline at end of file diff --git a/src/main/java/de/svenkubiak/jpushover/http/PushoverRequest.java b/src/main/java/de/svenkubiak/jpushover/http/PushoverRequest.java index 41b7889..3bd2aaf 100644 --- a/src/main/java/de/svenkubiak/jpushover/http/PushoverRequest.java +++ b/src/main/java/de/svenkubiak/jpushover/http/PushoverRequest.java @@ -38,10 +38,6 @@ public class PushoverRequest { return jPushoverResponse; } - private OptionalLong getHeaderValue(HttpResponse httpResponse, String header) { - return httpResponse.headers().firstValueAsLong(header); - } - private HttpResponse getResponse(String body, String url, String proxyHost, int proxyPort) throws IOException, InterruptedException { var httpRequest = HttpRequest.newBuilder() .uri(URI.create(url)) @@ -62,7 +58,7 @@ public class PushoverRequest { } private String toJson(NavigableMap body) { - StringBuilder buffer = new StringBuilder(); + var buffer = new StringBuilder(); buffer.append("{"); for (Map.Entry entry : body.entrySet()) { buffer.append("\"").append(entry.getKey()).append("\""); @@ -74,4 +70,8 @@ public class PushoverRequest { return buffer.toString().replace(",}", "}"); } + + private OptionalLong getHeaderValue(HttpResponse httpResponse, String header) { + return httpResponse.headers().firstValueAsLong(header); + } } \ No newline at end of file diff --git a/src/main/java/de/svenkubiak/jpushover/http/PushoverResponse.java b/src/main/java/de/svenkubiak/jpushover/http/PushoverResponse.java index c1a81d8..3382382 100644 --- a/src/main/java/de/svenkubiak/jpushover/http/PushoverResponse.java +++ b/src/main/java/de/svenkubiak/jpushover/http/PushoverResponse.java @@ -7,11 +7,11 @@ package de.svenkubiak.jpushover.http; */ public class PushoverResponse { private String pushoverResponse; - private boolean pushoverSuccessful; - private int pushoverHttpStatus; private long pushoverLimit; private long pushoverRemaining; private long pushoverReset; + private int pushoverHttpStatus; + private boolean pushoverSuccessful; public PushoverResponse response(String response) { this.pushoverResponse = response; diff --git a/src/test/java/jpushover/apis/MessageTest.java b/src/test/java/jpushover/apis/MessageTest.java deleted file mode 100644 index ad6f80a..0000000 --- a/src/test/java/jpushover/apis/MessageTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package jpushover.apis; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -import java.io.IOException; - -import org.junit.jupiter.api.Test; - -import de.svenkubiak.jpushover.JPushover; -import de.svenkubiak.jpushover.apis.Message; -import de.svenkubiak.jpushover.enums.Priority; -import de.svenkubiak.jpushover.enums.Sound; - -/** - * - * @author svenkubiak - * - */ -public class MessageTest { - - @Test - public void testConstruct() throws IOException, InterruptedException { - //given - int proxyPort = 8080; - int timestamp = 0; - int expire = 0; - int retry = 0; - String callback = "callback"; - String device = "device"; - String message = "message"; - String user = "user"; - String urlTitle = "urlTitle"; - String proxyHost = "proxyhost"; - String title = "tile"; - String token = "token"; - String url = "https://www.url.url"; - Priority priority = Priority.HIGH; - Sound sound = Sound.BUGLE; - - //when - Message m = JPushover.newMessage() - .enableHtml() - .withCallback(callback) - .withDevice(device) - .withExpire(expire) - .withMessage(message) - .withPriority(priority) - .withProxy(proxyHost, proxyPort) - .withRetry(retry) - .withSound(sound) - .withTimestamp(timestamp) - .withTitle(title) - .withToken(token) - .withUrl(url) - .withUser(user); - - //then - assertThat(m.isHtml(), equalTo(true)); - assertThat(m.isMonospace(), equalTo(false)); - assertThat(m.getCallback(), equalTo(callback)); - assertThat(m.getDevice(), equalTo(device)); - assertThat(m.getExpire(), equalTo(expire)); - assertThat(m.getMessage(), equalTo(message)); - assertThat(m.getPriority(), equalTo(Priority.HIGH)); - assertThat(m.getProxyHost(), equalTo(proxyHost)); - assertThat(m.getProxyPort(), equalTo(proxyPort)); - assertThat(m.getRetry(), equalTo(retry)); - assertThat(m.getSound(), equalTo(Sound.BUGLE)); - assertThat(m.getTimestamp(), equalTo(timestamp)); - assertThat(m.getTitle(), equalTo(title)); - assertThat(m.getToken(), equalTo(token)); - assertThat(m.getUrl(), equalTo(url)); - assertThat(m.getUrlTitle(), equalTo(url)); - assertThat(m.getUser(), equalTo(user)); - - //when - m.withUrlTitle(urlTitle); - m.enableMonospace(); - - //then - assertThat(m.getUrlTitle(), equalTo(urlTitle)); - assertThat(m.isHtml(), equalTo(false)); - assertThat(m.isMonospace(), equalTo(true)); - } -}