Added new attachment functionality
This commit is contained in:
parent
4a0c45a9fc
commit
63f7da86d5
11
pom.xml
11
pom.xml
@ -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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.svenkubiak</groupId>
|
<groupId>de.svenkubiak</groupId>
|
||||||
<artifactId>jpushover</artifactId>
|
<artifactId>jpushover</artifactId>
|
||||||
@ -20,6 +21,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<log4j.version>2.10.0</log4j.version>
|
<log4j.version>2.10.0</log4j.version>
|
||||||
|
<httpcomponents.version>4.5.4</httpcomponents.version>
|
||||||
</properties>
|
</properties>
|
||||||
<scm>
|
<scm>
|
||||||
<connection>scm:git:git@github.com:svenkubiak/JPushover.git</connection>
|
<connection>scm:git:git@github.com:svenkubiak/JPushover.git</connection>
|
||||||
@ -129,6 +131,11 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpmime</artifactId>
|
||||||
|
<version>${httpcomponents.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
@ -137,7 +144,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>fluent-hc</artifactId>
|
<artifactId>fluent-hc</artifactId>
|
||||||
<version>4.5.4</version>
|
<version>${httpcomponents.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.svenkubiak.jpushover;
|
package de.svenkubiak.jpushover;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -7,10 +8,17 @@ import java.util.Objects;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.Consts;
|
import org.apache.http.Consts;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.fluent.Form;
|
import org.apache.http.client.fluent.Form;
|
||||||
import org.apache.http.client.fluent.Request;
|
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.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -37,6 +45,7 @@ public class JPushover {
|
|||||||
private String pushoverRetry;
|
private String pushoverRetry;
|
||||||
private String pushoverExpire;
|
private String pushoverExpire;
|
||||||
private String pushoverCallback;
|
private String pushoverCallback;
|
||||||
|
private File pushoverAttachment;
|
||||||
private boolean pushoverHtml;
|
private boolean pushoverHtml;
|
||||||
private Priority pushoverPriority;
|
private Priority pushoverPriority;
|
||||||
private Sound pushoverSound;
|
private Sound pushoverSound;
|
||||||
@ -90,6 +99,17 @@ public class JPushover {
|
|||||||
this.pushoverRetry = retry;
|
this.pushoverRetry = retry;
|
||||||
return this;
|
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).
|
* 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");
|
Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency");
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<NameValuePair> params = Form.form()
|
HttpPost httpPost = new HttpPost(Constants.MESSAGES_URL.toString());
|
||||||
.add(Constants.TOKEN.toString(), this.pushoverToken)
|
|
||||||
.add(Constants.USER.toString(), this.pushoverUser)
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||||
.add(Constants.MESSAGE.toString(), this.pushoverMessage)
|
builder.addTextBody(Constants.TOKEN.toString(), this.pushoverToken);
|
||||||
.add(Constants.DEVICE.toString(), this.pushoverDevice)
|
builder.addTextBody(Constants.USER.toString(), this.pushoverUser);
|
||||||
.add(Constants.TITLE.toString(), this.pushoverTitle)
|
builder.addTextBody(Constants.MESSAGE.toString(), this.pushoverMessage);
|
||||||
.add(Constants.URL.toString(), this.pushoverUrl)
|
|
||||||
.add(Constants.RETRY.toString(), this.pushoverRetry)
|
if (StringUtils.isNotBlank(this.pushoverDevice)) {
|
||||||
.add(Constants.EXPIRE.toString(), this.pushoverExpire)
|
builder.addTextBody(Constants.DEVICE.toString(), this.pushoverDevice);
|
||||||
.add(Constants.CALLBACK.toString(), this.pushoverCallback)
|
}
|
||||||
.add(Constants.URLTITLE.toString(), this.pushoverUrlTitle)
|
|
||||||
.add(Constants.PRIORITY.toString(), this.pushoverPriority.toString())
|
if (StringUtils.isNotBlank(this.pushoverTitle)) {
|
||||||
.add(Constants.TIMESTAMP.toString(), this.pushoverTimestamp)
|
builder.addTextBody(Constants.DEVICE.toString(), this.pushoverTitle);
|
||||||
.add(Constants.SOUND.toString(), this.pushoverSound.toString())
|
}
|
||||||
.add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0")
|
|
||||||
.build();
|
if (StringUtils.isNotBlank(this.pushoverTitle)) {
|
||||||
|
builder.addTextBody(Constants.TITLE.toString(), this.pushoverTitle);
|
||||||
JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false);
|
}
|
||||||
try {
|
|
||||||
final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString())
|
if (StringUtils.isNotBlank(this.pushoverUrl)) {
|
||||||
.bodyForm(params, Consts.UTF_8)
|
builder.addTextBody(Constants.URL.toString(), this.pushoverUrl);
|
||||||
.execute()
|
}
|
||||||
.returnResponse();
|
|
||||||
|
if (StringUtils.isNotBlank(this.pushoverRetry)) {
|
||||||
if (httpResponse != null) {
|
builder.addTextBody(Constants.RETRY.toString(), this.pushoverRetry);
|
||||||
final int status = httpResponse.getStatusLine().getStatusCode();
|
}
|
||||||
|
|
||||||
jPushoverResponse
|
if (StringUtils.isNotBlank(this.pushoverExpire)) {
|
||||||
.httpStatus(status)
|
builder.addTextBody(Constants.EXPIRE.toString(), this.pushoverExpire);
|
||||||
.response(IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8))
|
}
|
||||||
.isSuccessful((status == HTTP_OK) ? true : false);
|
|
||||||
}
|
if (StringUtils.isNotBlank(this.pushoverCallback)) {
|
||||||
} catch (final IOException e) {
|
builder.addTextBody(Constants.CALLBACK.toString(), this.pushoverCallback);
|
||||||
LOG.error("Failed to send message to pushover", e);
|
}
|
||||||
|
|
||||||
|
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() {
|
public String getToken() {
|
||||||
@ -354,6 +416,10 @@ public class JPushover {
|
|||||||
public String getRetry() {
|
public String getRetry() {
|
||||||
return pushoverRetry;
|
return pushoverRetry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getAttachment() {
|
||||||
|
return pushoverAttachment;
|
||||||
|
}
|
||||||
|
|
||||||
public String getExpire() {
|
public String getExpire() {
|
||||||
return pushoverExpire;
|
return pushoverExpire;
|
||||||
|
@ -6,22 +6,23 @@ package de.svenkubiak.jpushover.enums;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public enum Constants {
|
public enum Constants {
|
||||||
MESSAGES_URL("https://api.pushover.net/1/messages.json"),
|
ATTACHMENT("attachment"),
|
||||||
VALIDATION_URL("https://api.pushover.net/1/users/validate.json"),
|
CALLBACK("callback"),
|
||||||
MESSAGE("message"),
|
|
||||||
TITLE("title"),
|
|
||||||
DEVICE("device"),
|
DEVICE("device"),
|
||||||
USER("user"),
|
EXPIRE("expire"),
|
||||||
TOKEN("token"),
|
HTML("html"),
|
||||||
SOUND("sound"),
|
MESSAGE("message"),
|
||||||
|
MESSAGES_URL("https://api.pushover.net/1/messages.json"),
|
||||||
PRIORITY("priority"),
|
PRIORITY("priority"),
|
||||||
|
RETRY("retry"),
|
||||||
|
SOUND("sound"),
|
||||||
TIMESTAMP("timestamp"),
|
TIMESTAMP("timestamp"),
|
||||||
|
TITLE("title"),
|
||||||
|
TOKEN("token"),
|
||||||
URL("url"),
|
URL("url"),
|
||||||
URLTITLE("urltitle"),
|
URLTITLE("urltitle"),
|
||||||
CALLBACK("callback"),
|
USER("user"),
|
||||||
EXPIRE("expire"),
|
VALIDATION_URL("https://api.pushover.net/1/users/validate.json");
|
||||||
RETRY("retry"),
|
|
||||||
HTML("html");
|
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
|
@ -6,28 +6,28 @@ package de.svenkubiak.jpushover.enums;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public enum Sound {
|
public enum Sound {
|
||||||
PUSHOVER("pushover"),
|
ALIEN("alien"),
|
||||||
BIKE("bike"),
|
BIKE("bike"),
|
||||||
BUGLE("bugle"),
|
BUGLE("bugle"),
|
||||||
CASHREGISTET("cashregister"),
|
CASHREGISTET("cashregister"),
|
||||||
CLASSICAL("classical"),
|
CLASSICAL("classical"),
|
||||||
|
CLIMB("climb"),
|
||||||
COSMIC("cosmic"),
|
COSMIC("cosmic"),
|
||||||
|
ECHO("echo"),
|
||||||
FALLING("falling"),
|
FALLING("falling"),
|
||||||
GAMELAN("gamelan"),
|
GAMELAN("gamelan"),
|
||||||
INCOMING("incoming"),
|
INCOMING("incoming"),
|
||||||
INTERMISSION("intermission"),
|
INTERMISSION("intermission"),
|
||||||
MAGIC("magic"),
|
MAGIC("magic"),
|
||||||
MECHANICAL("mechanical"),
|
MECHANICAL("mechanical"),
|
||||||
|
NONE("none"),
|
||||||
|
PERSISTENT("persistent"),
|
||||||
PIANOBAR("pianobar"),
|
PIANOBAR("pianobar"),
|
||||||
|
PUSHOVER("pushover"),
|
||||||
SIREN("siren"),
|
SIREN("siren"),
|
||||||
SPACEALARM("spacealarm"),
|
SPACEALARM("spacealarm"),
|
||||||
TUGBOAT("tugboat"),
|
TUGBOAT("tugboat"),
|
||||||
ALIEN("alien"),
|
UPDOWN("updown");
|
||||||
CLIMB("climb"),
|
|
||||||
PERSISTENT("persistent"),
|
|
||||||
ECHO("echo"),
|
|
||||||
UPDOWN("updown"),
|
|
||||||
NONE("none");
|
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
|
@ -3,6 +3,9 @@ package de.svenkubiak.jpushover;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import de.svenkubiak.jpushover.enums.Priority;
|
import de.svenkubiak.jpushover.enums.Priority;
|
||||||
@ -66,5 +69,9 @@ public class TestJPushover {
|
|||||||
|
|
||||||
push.enableHtml();
|
push.enableHtml();
|
||||||
assertTrue(push.isHtml());
|
assertTrue(push.isHtml());
|
||||||
|
|
||||||
|
File file = new File(UUID.randomUUID().toString());
|
||||||
|
push.withAttachment(file);
|
||||||
|
assertTrue(push.getAttachment() != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user