#1 added support for html flag
This commit is contained in:
parent
e6dc23e0ae
commit
c2432fc017
@ -1,6 +1,5 @@
|
||||
[](https://maven-badges.herokuapp.com/maven-central/de.svenkubiak/jpushover)
|
||||
[](http://travis-ci.org/svenkubiak/JPushover)
|
||||
[](https://www.versioneye.com/user/projects/54dcba12c1bbbd9bd700051a)
|
||||
|
||||
If this software is useful to you, you can support further development by using Flattr. Thank you!
|
||||
|
||||
@ -25,7 +24,7 @@ Usage
|
||||
|
||||
2) Use the JPushover object with the required informations were you want
|
||||
|
||||
new JPushover()
|
||||
JPushover.build()
|
||||
.token("MyToken")
|
||||
.user("MyUser")
|
||||
.message("MyMessage")
|
||||
@ -42,7 +41,7 @@ You can also validate a user and token using the following method
|
||||
|
||||
If you want more information and/or the response from the Pushover API, use the JPushoverResponse object.
|
||||
|
||||
JPushoverResponse jPushoverResponse = new JPushover()
|
||||
JPushoverResponse jPushoverResponse = JPushover()
|
||||
.token("MyToken")
|
||||
.user("MyUser")
|
||||
.message("MyMessage")
|
||||
|
@ -2,6 +2,7 @@ package de.svenkubiak.jpushover;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -13,8 +14,6 @@ import org.apache.http.client.fluent.Request;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import de.svenkubiak.jpushover.enums.Constants;
|
||||
import de.svenkubiak.jpushover.enums.Priority;
|
||||
import de.svenkubiak.jpushover.enums.Sound;
|
||||
@ -27,7 +26,6 @@ import de.svenkubiak.jpushover.enums.Sound;
|
||||
public class JPushover {
|
||||
private static final Logger LOG = LogManager.getLogger(JPushover.class);
|
||||
private static final int HTTP_OK = 200;
|
||||
|
||||
private String pushoverToken;
|
||||
private String pushoverUser;
|
||||
private String pushoverMessage;
|
||||
@ -39,12 +37,17 @@ public class JPushover {
|
||||
private String pushoverRetry;
|
||||
private String pushoverExpire;
|
||||
private String pushoverCallback;
|
||||
private boolean pushoverHtml;
|
||||
private Priority pushoverPriority;
|
||||
private Sound pushoverSound;
|
||||
|
||||
public JPushover(){
|
||||
this.pushoverSound = Sound.PUSHOVER;
|
||||
this.pushoverPriority = Priority.NORMAL;
|
||||
public JPushover() {
|
||||
this.sound(Sound.PUSHOVER);
|
||||
this.priority(Priority.NORMAL);
|
||||
}
|
||||
|
||||
public static JPushover build() {
|
||||
return new JPushover();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -145,6 +148,17 @@ public class JPushover {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables HTML in the pushover message
|
||||
* (optional)
|
||||
*
|
||||
* @return JPushover instance
|
||||
*/
|
||||
public JPushover html() {
|
||||
this.pushoverHtml = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* A title for your supplementary URL, otherwise just the URL is shown
|
||||
*
|
||||
@ -207,95 +221,6 @@ public class JPushover {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a validation request to pushover ensuring that the token and user
|
||||
* is correct, that there is at least one active device on the account.
|
||||
*
|
||||
* Requires token parameter
|
||||
* Requires user parameter
|
||||
* Optional device parameter to check specific device
|
||||
*
|
||||
* @return true if token and user are valid and at least on device is on the account, false otherwise
|
||||
*/
|
||||
public boolean validate() {
|
||||
Preconditions.checkNotNull(this.pushoverToken, "Token is required for validation");
|
||||
Preconditions.checkNotNull(this.pushoverUser, "User is required for validation");
|
||||
|
||||
final List<NameValuePair> params = Form.form()
|
||||
.add(Constants.TOKEN.get(), this.pushoverToken)
|
||||
.add(Constants.USER.get(), this.pushoverUser)
|
||||
.add(Constants.DEVICE.get(), this.pushoverDevice)
|
||||
.build();
|
||||
|
||||
HttpResponse httpResponse = null;
|
||||
boolean valid = false;
|
||||
try {
|
||||
httpResponse = Request.Post(Constants.VALIDATION_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse();
|
||||
|
||||
if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HTTP_OK) {
|
||||
final String response = IOUtils.toString(httpResponse.getEntity().getContent());
|
||||
if (StringUtils.isNotBlank(response) && response.contains("\"status\":1")) {
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error("Failed to send validation requeste to pushover", e);
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to pushover
|
||||
*
|
||||
* @return JPushoverResponse instance
|
||||
*/
|
||||
public JPushoverResponse push() {
|
||||
Preconditions.checkNotNull(this.pushoverToken, "Token is required for a message");
|
||||
Preconditions.checkNotNull(this.pushoverUser, "User is required for a message");
|
||||
Preconditions.checkNotNull(this.pushoverMessage, "Message is required for a message");
|
||||
|
||||
if (Priority.EMERGENCY.equals(this.pushoverPriority)) {
|
||||
Preconditions.checkNotNull(this.pushoverRetry, "Retry is required on priority emergency");
|
||||
Preconditions.checkNotNull(this.pushoverExpire, "Expire is required on priority emergency");
|
||||
}
|
||||
|
||||
final List<NameValuePair> params = Form.form()
|
||||
.add(Constants.TOKEN.get(), this.pushoverToken)
|
||||
.add(Constants.USER.get(), this.pushoverUser)
|
||||
.add(Constants.MESSAGE.get(), this.pushoverMessage)
|
||||
.add(Constants.DEVICE.get(), this.pushoverDevice)
|
||||
.add(Constants.TITLE.get(), this.pushoverTitle)
|
||||
.add(Constants.URL.get(), this.pushoverUrl)
|
||||
.add(Constants.RETRY.get(), this.pushoverRetry)
|
||||
.add(Constants.EXPIRE.get(), this.pushoverExpire)
|
||||
.add(Constants.CALLBACK.get(), this.pushoverCallback)
|
||||
.add(Constants.URLTITLE.get(), this.pushoverUrlTitle)
|
||||
.add(Constants.PRIORITY.get(), this.pushoverPriority.get())
|
||||
.add(Constants.TIMESTAMP.get(), this.pushoverTimestamp)
|
||||
.add(Constants.SOUND.get(), this.pushoverSound.get())
|
||||
.build();
|
||||
|
||||
HttpResponse httpResponse = null;
|
||||
JPushoverResponse jPushoverResponse = null;
|
||||
try {
|
||||
httpResponse = Request.Post(Constants.MESSAGES_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse();
|
||||
|
||||
if (httpResponse != null) {
|
||||
final int status = httpResponse.getStatusLine().getStatusCode();
|
||||
|
||||
jPushoverResponse = new 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);
|
||||
}
|
||||
|
||||
return (jPushoverResponse == null) ? new JPushoverResponse().isSuccessful(false) : jPushoverResponse;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return pushoverToken;
|
||||
}
|
||||
@ -347,4 +272,96 @@ public class JPushover {
|
||||
public Sound getSound() {
|
||||
return pushoverSound;
|
||||
}
|
||||
|
||||
public boolean getHtml() {
|
||||
return pushoverHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a validation request to pushover ensuring that the token and user
|
||||
* is correct, that there is at least one active device on the account.
|
||||
*
|
||||
* Requires token parameter
|
||||
* Requires user parameter
|
||||
* Optional device parameter to check specific device
|
||||
*
|
||||
* @return true if token and user are valid and at least on device is on the account, false otherwise
|
||||
*/
|
||||
public boolean validate() {
|
||||
Objects.requireNonNull(this.pushoverToken, "Token is required for validation");
|
||||
Objects.requireNonNull(this.pushoverUser, "User is required for validation");
|
||||
|
||||
final List<NameValuePair> params = Form.form()
|
||||
.add(Constants.TOKEN.toString(), this.pushoverToken)
|
||||
.add(Constants.USER.toString(), this.pushoverUser)
|
||||
.add(Constants.DEVICE.toString(), this.pushoverDevice)
|
||||
.build();
|
||||
|
||||
boolean valid = false;
|
||||
try {
|
||||
final HttpResponse httpResponse = Request.Post(Constants.VALIDATION_URL.toString()).bodyForm(params, Consts.UTF_8).execute().returnResponse();
|
||||
|
||||
if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HTTP_OK) {
|
||||
final String response = IOUtils.toString(httpResponse.getEntity().getContent());
|
||||
if (StringUtils.isNotBlank(response) && response.contains("\"status\":1")) {
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error("Failed to send validation requeste to pushover", e);
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to pushover
|
||||
*
|
||||
* @return JPushoverResponse instance
|
||||
*/
|
||||
public JPushoverResponse push() {
|
||||
Objects.requireNonNull(this.pushoverToken, "Token is required for a message");
|
||||
Objects.requireNonNull(this.pushoverUser, "User is required for a message");
|
||||
Objects.requireNonNull(this.pushoverMessage, "Message is required for a message");
|
||||
|
||||
if (Priority.EMERGENCY.equals(this.pushoverPriority)) {
|
||||
Objects.requireNonNull(this.pushoverRetry, "Retry is required on priority emergency");
|
||||
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.get())
|
||||
.add(Constants.TIMESTAMP.toString(), this.pushoverTimestamp)
|
||||
.add(Constants.SOUND.toString(), this.pushoverSound.get())
|
||||
.add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0")
|
||||
.build();
|
||||
|
||||
JPushoverResponse jPushoverResponse = null;
|
||||
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 = new 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);
|
||||
}
|
||||
|
||||
return (jPushoverResponse == null) ? new JPushoverResponse().isSuccessful(false) : jPushoverResponse;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package de.svenkubiak.jpushover;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author svenkubiak
|
||||
*
|
||||
*/
|
||||
@ -9,7 +9,7 @@ public class JPushoverResponse {
|
||||
private String pushoverResponse;
|
||||
private int pushoverHttpStatus;
|
||||
private boolean pushoverSuccessful;
|
||||
|
||||
|
||||
public JPushoverResponse response(String response) {
|
||||
this.pushoverResponse = response;
|
||||
return this;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package de.svenkubiak.jpushover.enums;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author svenkubiak
|
||||
*
|
||||
*/
|
||||
@ -20,15 +20,16 @@ public enum Constants {
|
||||
URLTITLE("urltitle"),
|
||||
CALLBACK("callback"),
|
||||
EXPIRE("expire"),
|
||||
RETRY("retry");
|
||||
|
||||
RETRY("retry"),
|
||||
HTML("html");
|
||||
|
||||
private final String value;
|
||||
|
||||
Constants (String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
public String toString() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
@ -19,48 +19,51 @@ public class TestJPushover {
|
||||
private static final String EXPIRE = "expire";
|
||||
private static final String DEVICE = "device";
|
||||
private static final String CALLBACK = "callback";
|
||||
|
||||
|
||||
@Test
|
||||
public void TestValues(){
|
||||
JPushover push = new JPushover();
|
||||
|
||||
final JPushover push = new JPushover();
|
||||
|
||||
push.callback(CALLBACK);
|
||||
assertEquals(push.getCallback(), CALLBACK);
|
||||
|
||||
|
||||
push.device(DEVICE);
|
||||
assertEquals(push.getDevice(), DEVICE);
|
||||
|
||||
|
||||
push.expire(EXPIRE);
|
||||
assertEquals(push.getExpire(), EXPIRE);
|
||||
|
||||
|
||||
push.message(MESSAGE);
|
||||
assertEquals(push.getMessage(), MESSAGE);
|
||||
|
||||
|
||||
push.priority(Priority.HIGH);
|
||||
assertEquals(push.getPriority(), Priority.HIGH);
|
||||
|
||||
|
||||
push.retry(RETRY);
|
||||
assertEquals(push.getRetry(), RETRY);
|
||||
|
||||
|
||||
push.sound(Sound.ALIEN);
|
||||
assertEquals(push.getSound(), Sound.ALIEN);
|
||||
|
||||
|
||||
push.timestamp(TIMESTAMP);
|
||||
assertEquals(push.getTimestamp(), TIMESTAMP);
|
||||
|
||||
|
||||
push.title(TITLE);
|
||||
assertEquals(push.getTitle(), TITLE);
|
||||
|
||||
|
||||
push.token(TOKEN);
|
||||
assertEquals(push.getToken(), TOKEN);
|
||||
|
||||
|
||||
push.url(URL);
|
||||
assertEquals(push.getUrl(), URL);
|
||||
|
||||
|
||||
push.urlTitle(URL_TITLE);
|
||||
assertEquals(push.getUrlTitle(), URL_TITLE);
|
||||
|
||||
|
||||
push.user(USER);
|
||||
assertEquals(push.getUser(), USER);
|
||||
|
||||
push.html();
|
||||
assertEquals(push.getHtml(), true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user