Added new attachment functionality

This commit is contained in:
Sven Kubiak 2018-01-24 17:00:42 +01:00
parent 4a0c45a9fc
commit 63f7da86d5
5 changed files with 136 additions and 55 deletions

11
pom.xml
View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.svenkubiak</groupId>
<artifactId>jpushover</artifactId>
@ -20,6 +21,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>2.10.0</log4j.version>
<httpcomponents.version>4.5.4</httpcomponents.version>
</properties>
<scm>
<connection>scm:git:git@github.com:svenkubiak/JPushover.git</connection>
@ -129,6 +131,11 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpcomponents.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@ -137,7 +144,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.4</version>
<version>${httpcomponents.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>

View File

@ -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<NameValuePair> 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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}