diff --git a/pom.xml b/pom.xml index a3c1e9a..a148a9f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 de.svenkubiak jpushover @@ -20,6 +21,7 @@ UTF-8 2.10.0 + 4.5.4 scm:git:git@github.com:svenkubiak/JPushover.git @@ -129,6 +131,11 @@ + + org.apache.httpcomponents + httpmime + ${httpcomponents.version} + commons-io commons-io @@ -137,7 +144,7 @@ org.apache.httpcomponents fluent-hc - 4.5.4 + ${httpcomponents.version} org.apache.commons diff --git a/src/main/java/de/svenkubiak/jpushover/JPushover.java b/src/main/java/de/svenkubiak/jpushover/JPushover.java index 7e1dbe5..21563b9 100644 --- a/src/main/java/de/svenkubiak/jpushover/JPushover.java +++ b/src/main/java/de/svenkubiak/jpushover/JPushover.java @@ -1,5 +1,6 @@ package de.svenkubiak.jpushover; +import java.io.File; import java.io.IOException; import java.util.List; import java.util.Objects; @@ -7,10 +8,17 @@ import java.util.Objects; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.Consts; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.fluent.Form; import org.apache.http.client.fluent.Request; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -37,6 +45,7 @@ public class JPushover { private String pushoverRetry; private String pushoverExpire; private String pushoverCallback; + private File pushoverAttachment; private boolean pushoverHtml; private Priority pushoverPriority; private Sound pushoverSound; @@ -90,6 +99,17 @@ public class JPushover { this.pushoverRetry = retry; return this; } + + /** + * Add a file attachment to be added to the request + * + * @param file The attachment to add + * @return JPushover instance + */ + public final JPushover withAttachment(File attachment) { + this.pushoverAttachment = attachment; + return this; + } /** * Specifies how many seconds your notification will continue to be retried for (every retry seconds). @@ -280,43 +300,85 @@ public class JPushover { Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency"); } - final List params = Form.form() - .add(Constants.TOKEN.toString(), this.pushoverToken) - .add(Constants.USER.toString(), this.pushoverUser) - .add(Constants.MESSAGE.toString(), this.pushoverMessage) - .add(Constants.DEVICE.toString(), this.pushoverDevice) - .add(Constants.TITLE.toString(), this.pushoverTitle) - .add(Constants.URL.toString(), this.pushoverUrl) - .add(Constants.RETRY.toString(), this.pushoverRetry) - .add(Constants.EXPIRE.toString(), this.pushoverExpire) - .add(Constants.CALLBACK.toString(), this.pushoverCallback) - .add(Constants.URLTITLE.toString(), this.pushoverUrlTitle) - .add(Constants.PRIORITY.toString(), this.pushoverPriority.toString()) - .add(Constants.TIMESTAMP.toString(), this.pushoverTimestamp) - .add(Constants.SOUND.toString(), this.pushoverSound.toString()) - .add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0") - .build(); - - JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); - try { - final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString()) - .bodyForm(params, Consts.UTF_8) - .execute() - .returnResponse(); - - if (httpResponse != null) { - final int status = httpResponse.getStatusLine().getStatusCode(); - - jPushoverResponse - .httpStatus(status) - .response(IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8)) - .isSuccessful((status == HTTP_OK) ? true : false); - } - } catch (final IOException e) { - LOG.error("Failed to send message to pushover", e); + HttpPost httpPost = new HttpPost(Constants.MESSAGES_URL.toString()); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(Constants.TOKEN.toString(), this.pushoverToken); + builder.addTextBody(Constants.USER.toString(), this.pushoverUser); + builder.addTextBody(Constants.MESSAGE.toString(), this.pushoverMessage); + + if (StringUtils.isNotBlank(this.pushoverDevice)) { + builder.addTextBody(Constants.DEVICE.toString(), this.pushoverDevice); + } + + if (StringUtils.isNotBlank(this.pushoverTitle)) { + builder.addTextBody(Constants.DEVICE.toString(), this.pushoverTitle); + } + + if (StringUtils.isNotBlank(this.pushoverTitle)) { + builder.addTextBody(Constants.TITLE.toString(), this.pushoverTitle); + } + + if (StringUtils.isNotBlank(this.pushoverUrl)) { + builder.addTextBody(Constants.URL.toString(), this.pushoverUrl); + } + + if (StringUtils.isNotBlank(this.pushoverRetry)) { + builder.addTextBody(Constants.RETRY.toString(), this.pushoverRetry); + } + + if (StringUtils.isNotBlank(this.pushoverExpire)) { + builder.addTextBody(Constants.EXPIRE.toString(), this.pushoverExpire); + } + + if (StringUtils.isNotBlank(this.pushoverCallback)) { + builder.addTextBody(Constants.CALLBACK.toString(), this.pushoverCallback); + } + + if (StringUtils.isNotBlank(this.pushoverUrlTitle)) { + builder.addTextBody(Constants.URLTITLE.toString(), this.pushoverUrlTitle); + } + + if (StringUtils.isNotBlank(this.pushoverPriority.toString())) { + builder.addTextBody(Constants.PRIORITY.toString(), this.pushoverPriority.toString()); + } + + if (StringUtils.isNotBlank(this.pushoverTimestamp)) { + builder.addTextBody(Constants.TIMESTAMP.toString(), this.pushoverTimestamp); + } + + if (StringUtils.isNotBlank(this.pushoverSound.toString())) { + builder.addTextBody(Constants.SOUND.toString(), this.pushoverSound.toString()); } - return jPushoverResponse; + if (this.pushoverAttachment != null) { + builder.addBinaryBody(Constants.ATTACHMENT.toString(), this.pushoverAttachment, ContentType.APPLICATION_OCTET_STREAM, "file.ext"); + } + + builder.addTextBody(Constants.SOUND.toString(), this.pushoverHtml ? "1" : "0"); + + HttpEntity multipart = builder.build(); + httpPost.setEntity(multipart); + + JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); + try { + CloseableHttpClient client = HttpClients.createDefault(); + CloseableHttpResponse closeableHttpResponse = client.execute(httpPost); + + if (closeableHttpResponse != null) { + final int status = closeableHttpResponse.getStatusLine().getStatusCode(); + + jPushoverResponse = jPushoverResponse.httpStatus(status) + .response(IOUtils.toString(closeableHttpResponse.getEntity().getContent(), Consts.UTF_8)) + .isSuccessful((status == HTTP_OK) ? true : false); + } + + client.close(); + } catch (IOException e) { + LOG.error("Failed to send message to pushover", e); + } + + return null; } public String getToken() { @@ -354,6 +416,10 @@ public class JPushover { public String getRetry() { return pushoverRetry; } + + public File getAttachment() { + return pushoverAttachment; + } public String getExpire() { return pushoverExpire; diff --git a/src/main/java/de/svenkubiak/jpushover/enums/Constants.java b/src/main/java/de/svenkubiak/jpushover/enums/Constants.java index 844035b..1fa18af 100644 --- a/src/main/java/de/svenkubiak/jpushover/enums/Constants.java +++ b/src/main/java/de/svenkubiak/jpushover/enums/Constants.java @@ -6,22 +6,23 @@ package de.svenkubiak.jpushover.enums; * */ public enum Constants { - MESSAGES_URL("https://api.pushover.net/1/messages.json"), - VALIDATION_URL("https://api.pushover.net/1/users/validate.json"), - MESSAGE("message"), - TITLE("title"), + ATTACHMENT("attachment"), + CALLBACK("callback"), DEVICE("device"), - USER("user"), - TOKEN("token"), - SOUND("sound"), + EXPIRE("expire"), + HTML("html"), + MESSAGE("message"), + MESSAGES_URL("https://api.pushover.net/1/messages.json"), PRIORITY("priority"), + RETRY("retry"), + SOUND("sound"), TIMESTAMP("timestamp"), + TITLE("title"), + TOKEN("token"), URL("url"), URLTITLE("urltitle"), - CALLBACK("callback"), - EXPIRE("expire"), - RETRY("retry"), - HTML("html"); + USER("user"), + VALIDATION_URL("https://api.pushover.net/1/users/validate.json"); private final String value; diff --git a/src/main/java/de/svenkubiak/jpushover/enums/Sound.java b/src/main/java/de/svenkubiak/jpushover/enums/Sound.java index 2165841..0e8ed6d 100644 --- a/src/main/java/de/svenkubiak/jpushover/enums/Sound.java +++ b/src/main/java/de/svenkubiak/jpushover/enums/Sound.java @@ -6,28 +6,28 @@ package de.svenkubiak.jpushover.enums; * */ public enum Sound { - PUSHOVER("pushover"), + ALIEN("alien"), BIKE("bike"), BUGLE("bugle"), CASHREGISTET("cashregister"), CLASSICAL("classical"), + CLIMB("climb"), COSMIC("cosmic"), + ECHO("echo"), FALLING("falling"), GAMELAN("gamelan"), INCOMING("incoming"), INTERMISSION("intermission"), MAGIC("magic"), MECHANICAL("mechanical"), + NONE("none"), + PERSISTENT("persistent"), PIANOBAR("pianobar"), + PUSHOVER("pushover"), SIREN("siren"), SPACEALARM("spacealarm"), TUGBOAT("tugboat"), - ALIEN("alien"), - CLIMB("climb"), - PERSISTENT("persistent"), - ECHO("echo"), - UPDOWN("updown"), - NONE("none"); + UPDOWN("updown"); private final String value; diff --git a/src/test/java/de/svenkubiak/jpushover/TestJPushover.java b/src/test/java/de/svenkubiak/jpushover/TestJPushover.java index 9706786..6c2c33c 100644 --- a/src/test/java/de/svenkubiak/jpushover/TestJPushover.java +++ b/src/test/java/de/svenkubiak/jpushover/TestJPushover.java @@ -3,6 +3,9 @@ package de.svenkubiak.jpushover; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.UUID; + import org.junit.Test; import de.svenkubiak.jpushover.enums.Priority; @@ -66,5 +69,9 @@ public class TestJPushover { push.enableHtml(); assertTrue(push.isHtml()); + + File file = new File(UUID.randomUUID().toString()); + push.withAttachment(file); + assertTrue(push.getAttachment() != null); } } \ No newline at end of file