Reseted to non attachment version

This commit is contained in:
Sven Kubiak 2018-04-11 14:49:00 +02:00
parent 8f91cae8e4
commit f12d251d76

View File

@ -1,6 +1,5 @@
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;
@ -12,12 +11,6 @@ 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;
@ -44,7 +37,6 @@ 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;
@ -98,17 +90,6 @@ public class JPushover {
this.pushoverRetry = retry; this.pushoverRetry = retry;
return this; return this;
} }
/**
* Add a file attachment to be added to the request
*
* @param attachment 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).
@ -299,83 +280,42 @@ public class JPushover {
Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency"); Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency");
} }
HttpPost httpPost = new HttpPost(Constants.MESSAGES_URL.toString()); final List<NameValuePair> params = Form.form()
httpPost.addHeader("Content-Type", "text/html; charset=UTF-8"); .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);
}
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());
}
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");
httpPost.setEntity(builder.build());
JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false); JPushoverResponse jPushoverResponse = new JPushoverResponse().isSuccessful(false);
try { try {
CloseableHttpClient client = HttpClients.createDefault(); final HttpResponse httpResponse = Request.Post(Constants.MESSAGES_URL.toString())
CloseableHttpResponse closeableHttpResponse = client.execute(httpPost); .bodyForm(params, Consts.UTF_8)
.execute()
.returnResponse();
if (closeableHttpResponse != null) { if (httpResponse != null) {
final int status = closeableHttpResponse.getStatusLine().getStatusCode(); final int status = httpResponse.getStatusLine().getStatusCode();
jPushoverResponse = jPushoverResponse.httpStatus(status) jPushoverResponse
.response(IOUtils.toString(closeableHttpResponse.getEntity().getContent(), Consts.UTF_8)) .httpStatus(status)
.isSuccessful((status == HTTP_OK) ? true : false); .response(IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8))
.isSuccessful((status == HTTP_OK) ? true : false);
} }
} catch (final IOException e) {
client.close();
} catch (IOException e) {
LOG.error("Failed to send message to pushover", e); LOG.error("Failed to send message to pushover", e);
} }
return jPushoverResponse; return jPushoverResponse;
} }
@ -414,10 +354,6 @@ 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;