2015-01-07 13:16:55 +01:00
|
|
|
package de.svenkubiak.jpushover;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-10-06 07:49:05 +02:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.ProxySelector;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
import java.net.http.HttpResponse;
|
|
|
|
import java.time.Duration;
|
2015-12-08 22:03:44 +01:00
|
|
|
import java.util.Objects;
|
2015-01-07 13:16:55 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
import com.eclipsesource.json.Json;
|
2015-01-07 13:16:55 +01:00
|
|
|
|
|
|
|
import de.svenkubiak.jpushover.enums.Constants;
|
|
|
|
import de.svenkubiak.jpushover.enums.Priority;
|
|
|
|
import de.svenkubiak.jpushover.enums.Sound;
|
|
|
|
|
2015-01-08 08:12:55 +01:00
|
|
|
/**
|
2018-10-06 19:08:37 +02:00
|
|
|
*
|
|
|
|
* Minimalist convenient class for sending messages to Pushover
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @author svenkubiak
|
|
|
|
*
|
|
|
|
*/
|
2015-01-07 13:16:55 +01:00
|
|
|
public class JPushover {
|
2015-01-26 08:15:18 +01:00
|
|
|
private static final int HTTP_OK = 200;
|
2018-10-06 07:49:05 +02:00
|
|
|
private Priority pushoverPriority;
|
|
|
|
private Sound pushoverSound;
|
2015-01-07 20:11:23 +01:00
|
|
|
private String pushoverToken;
|
|
|
|
private String pushoverUser;
|
|
|
|
private String pushoverMessage;
|
|
|
|
private String pushoverDevice;
|
|
|
|
private String pushoverTitle;
|
|
|
|
private String pushoverUrl;
|
|
|
|
private String pushoverUrlTitle;
|
|
|
|
private String pushoverTimestamp;
|
2015-01-09 07:56:01 +01:00
|
|
|
private String pushoverRetry;
|
|
|
|
private String pushoverExpire;
|
|
|
|
private String pushoverCallback;
|
2018-10-06 07:49:05 +02:00
|
|
|
private String proxyHost;
|
|
|
|
private int proxyPort;
|
2018-08-16 11:26:28 +02:00
|
|
|
private boolean pushoverHtml;
|
|
|
|
|
2015-12-08 22:03:44 +01:00
|
|
|
public JPushover() {
|
2016-10-20 08:35:08 +02:00
|
|
|
this.withSound(Sound.PUSHOVER);
|
|
|
|
this.withPriority(Priority.NORMAL);
|
2015-12-08 22:03:44 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 11:17:36 +01:00
|
|
|
/**
|
|
|
|
* Creates a new JPushover instance
|
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-10-06 07:49:05 +02:00
|
|
|
public static JPushover create() {
|
2015-12-08 22:03:44 +01:00
|
|
|
return new JPushover();
|
2015-01-07 13:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Your application's API token
|
2015-11-29 10:35:32 +01:00
|
|
|
* (required)
|
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param token The pushover API token
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withToken(final String token) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverToken = token;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user/group key (not e-mail address) of your user (or you),
|
|
|
|
* viewable when logged into the @see <a href="https://pushover.net/login">pushover dashboard</a>
|
2015-11-29 10:35:32 +01:00
|
|
|
* (required)
|
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param user The username
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withUser(final String user) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverUser = user;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2015-01-09 07:56:01 +01:00
|
|
|
/**
|
|
|
|
* Specifies how often (in seconds) the Pushover servers will send the same notification to the user.
|
|
|
|
* Only required if priority is set to emergency.
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-09 07:56:01 +01:00
|
|
|
* @param retry Number of seconds
|
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withRetry(final String retry) {
|
2015-01-09 07:56:01 +01:00
|
|
|
this.pushoverRetry = retry;
|
|
|
|
return this;
|
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2015-01-09 07:56:01 +01:00
|
|
|
/**
|
|
|
|
* Specifies how many seconds your notification will continue to be retried for (every retry seconds).
|
|
|
|
* Only required if priority is set to emergency.
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-09 07:56:01 +01:00
|
|
|
* @param expire Number of seconds
|
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withExpire(final String expire) {
|
2015-01-09 07:56:01 +01:00
|
|
|
this.pushoverExpire = expire;
|
|
|
|
return this;
|
|
|
|
}
|
2015-01-07 13:16:55 +01:00
|
|
|
|
|
|
|
/**
|
2015-11-29 10:35:32 +01:00
|
|
|
* Your message
|
|
|
|
* (required)
|
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param message The message to sent
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withMessage(final String message) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverMessage = message;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Your user's device name to send the message directly to that device,
|
|
|
|
* rather than all of the user's devices
|
|
|
|
* (optional)
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param device The device name
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withDevice(final String device) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverDevice = device;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Your message's title, otherwise your app's name is used
|
|
|
|
* (optional)
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param title The title
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withTitle(final String title) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverTitle = title;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A supplementary URL to show with your message
|
|
|
|
* (optional)
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param url The url
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withUrl(final String url) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverUrl = url;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-12-08 22:03:44 +01:00
|
|
|
/**
|
|
|
|
* Enables HTML in the pushover message
|
|
|
|
* (optional)
|
|
|
|
*
|
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2016-10-20 08:35:08 +02:00
|
|
|
public final JPushover enableHtml() {
|
2015-12-08 22:03:44 +01:00
|
|
|
this.pushoverHtml = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-01-07 13:16:55 +01:00
|
|
|
/**
|
|
|
|
* A title for your supplementary URL, otherwise just the URL is shown
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param urlTitle The url title
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withUrlTitle(final String urlTitle) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverUrlTitle = urlTitle;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Unix timestamp of your message's date and time to display to the user,
|
|
|
|
* rather than the time your message is received by our API
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param timestamp The Unix timestamp
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withTimestamp(final String timestamp) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverTimestamp = timestamp;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-07 20:11:23 +01:00
|
|
|
* Priority of the message based on the @see <a href="https://pushover.net/api#priority">documentation</a>
|
2015-01-07 13:16:55 +01:00
|
|
|
* (optional)
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param priority The priority enum
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withPriority(final Priority priority) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverPriority = priority;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of one of the sounds supported by device clients to override
|
|
|
|
* the user's default sound choice
|
|
|
|
* (optional)
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-08 08:12:55 +01:00
|
|
|
* @param sound THe sound enum
|
2015-01-07 13:16:55 +01:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withSound(final Sound sound) {
|
2015-01-07 20:11:23 +01:00
|
|
|
this.pushoverSound = sound;
|
2015-01-07 13:16:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2015-01-09 07:56:01 +01:00
|
|
|
/**
|
|
|
|
* Callback parameter may be supplied with a publicly-accessible URL that the
|
2015-12-10 11:17:36 +01:00
|
|
|
* pushover servers will send a request to when the user has acknowledged your
|
2015-01-09 07:56:01 +01:00
|
|
|
* notification.
|
|
|
|
* Only required if priority is set to emergency.
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-12-08 22:06:13 +01:00
|
|
|
* @param callback The callback URL
|
|
|
|
* @return JPushover instance
|
2015-01-09 07:56:01 +01:00
|
|
|
*/
|
2018-08-16 15:28:02 +08:00
|
|
|
public final JPushover withCallback(final String callback) {
|
2015-01-09 07:56:01 +01:00
|
|
|
this.pushoverCallback = callback;
|
|
|
|
return this;
|
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-08-16 15:28:02 +08:00
|
|
|
/**
|
2018-08-16 11:26:28 +02:00
|
|
|
* Uses the given proxy for HTTP requests
|
2018-08-16 15:28:02 +08:00
|
|
|
*
|
2018-10-06 07:49:05 +02:00
|
|
|
* @param proxyHost The host that should be used for the Proxy
|
|
|
|
* @param proxyPort The port that should be used for the Proxy
|
2018-08-16 15:28:02 +08:00
|
|
|
* @return JPushover instance
|
|
|
|
*/
|
2018-10-06 07:49:05 +02:00
|
|
|
public final JPushover withProxy(final String proxyHost, final int proxyPort) {
|
|
|
|
this.proxyHost = proxyHost;
|
|
|
|
this.proxyPort = proxyPort;
|
2018-08-16 15:28:02 +08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:22:35 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-09 08:22:35 +01:00
|
|
|
* Requires token parameter
|
|
|
|
* Requires user parameter
|
|
|
|
* Optional device parameter to check specific device
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-09 08:22:35 +01:00
|
|
|
* @return true if token and user are valid and at least on device is on the account, false otherwise
|
2018-10-06 13:44:18 +02:00
|
|
|
*
|
|
|
|
* @throws IOException if validation fails
|
|
|
|
* @throws InterruptedException if validation fails
|
2015-01-09 08:22:35 +01:00
|
|
|
*/
|
2018-10-06 07:49:05 +02:00
|
|
|
public boolean validate() throws IOException, InterruptedException {
|
2015-12-08 22:03:44 +01:00
|
|
|
Objects.requireNonNull(this.pushoverToken, "Token is required for validation");
|
|
|
|
Objects.requireNonNull(this.pushoverUser, "User is required for validation");
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
var body = Json.object()
|
2015-12-08 22:03:44 +01:00
|
|
|
.add(Constants.TOKEN.toString(), this.pushoverToken)
|
|
|
|
.add(Constants.USER.toString(), this.pushoverUser)
|
2018-10-06 07:49:05 +02:00
|
|
|
.add(Constants.DEVICE.toString(), this.pushoverDevice);
|
|
|
|
|
|
|
|
var httpResponse = getResponse(body.toString(), Constants.VALIDATION_URL.toString());
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
var valid = false;
|
|
|
|
if (httpResponse.statusCode() == HTTP_OK) {
|
|
|
|
var response = httpResponse.body();
|
2018-10-06 19:08:37 +02:00
|
|
|
if (response != null && response.contains("\"status\":1")) {
|
2018-10-06 07:49:05 +02:00
|
|
|
valid = true;
|
2015-01-09 08:22:35 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2015-01-09 08:22:35 +01:00
|
|
|
return valid;
|
|
|
|
}
|
2015-01-07 13:16:55 +01:00
|
|
|
|
|
|
|
/**
|
2015-01-09 08:22:35 +01:00
|
|
|
* Sends a message to pushover
|
2015-11-29 10:35:32 +01:00
|
|
|
*
|
2015-01-09 08:22:35 +01:00
|
|
|
* @return JPushoverResponse instance
|
2018-10-06 13:44:18 +02:00
|
|
|
*
|
2018-10-06 19:08:37 +02:00
|
|
|
* @throws IOException if sending the message fails
|
|
|
|
* @throws InterruptedException if sending the message fails
|
2015-01-07 13:16:55 +01:00
|
|
|
*/
|
2018-10-06 07:49:05 +02:00
|
|
|
public final JPushoverResponse push() throws IOException, InterruptedException {
|
2015-12-08 22:03:44 +01:00
|
|
|
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");
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2015-01-09 07:56:01 +01:00
|
|
|
if (Priority.EMERGENCY.equals(this.pushoverPriority)) {
|
2015-12-08 22:03:44 +01:00
|
|
|
Objects.requireNonNull(this.pushoverRetry, "Retry is required on priority emergency");
|
|
|
|
Objects.requireNonNull(this.pushoverExpire, "Expire is required on priority emergency");
|
2015-01-09 07:56:01 +01:00
|
|
|
}
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
var body = Json.object()
|
2018-04-11 14:49:00 +02:00
|
|
|
.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())
|
2018-10-06 07:49:05 +02:00
|
|
|
.add(Constants.HTML.toString(), this.pushoverHtml ? "1" : "0");
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
var httpResponse = getResponse(body.toString(), Constants.MESSAGES_URL.toString());
|
2015-11-29 10:35:32 +01:00
|
|
|
|
2018-10-06 19:08:37 +02:00
|
|
|
var jPushoverResponse = new JPushoverResponse().isSuccessful(false);
|
2018-10-06 07:49:05 +02:00
|
|
|
jPushoverResponse
|
|
|
|
.httpStatus(httpResponse.statusCode())
|
|
|
|
.response(httpResponse.body())
|
|
|
|
.isSuccessful((httpResponse.statusCode() == HTTP_OK) ? true : false);
|
2018-04-11 14:49:00 +02:00
|
|
|
|
2018-01-31 15:55:01 +01:00
|
|
|
return jPushoverResponse;
|
2015-01-07 13:16:55 +01:00
|
|
|
}
|
2015-12-10 11:30:21 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
private HttpResponse<String> getResponse(String body, String url) throws IOException, InterruptedException {
|
2018-10-06 19:08:37 +02:00
|
|
|
var httpRequest = HttpRequest.newBuilder()
|
2018-10-06 07:49:05 +02:00
|
|
|
.uri(URI.create(url))
|
|
|
|
.timeout(Duration.ofSeconds(5))
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body))
|
|
|
|
.build();
|
2015-12-10 11:30:21 +01:00
|
|
|
|
2018-10-06 19:08:37 +02:00
|
|
|
var httpClientBuilder = HttpClient.newBuilder();
|
2015-12-10 11:30:21 +01:00
|
|
|
|
2018-10-06 19:08:37 +02:00
|
|
|
if (this.proxyHost != null && this.proxyPort > 0) {
|
2018-10-06 07:49:05 +02:00
|
|
|
httpClientBuilder.proxy(ProxySelector.of(new InetSocketAddress(this.proxyHost, this.proxyPort)));
|
|
|
|
}
|
2015-12-10 11:30:21 +01:00
|
|
|
|
2018-10-06 07:49:05 +02:00
|
|
|
return httpClientBuilder.build().send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
2018-08-16 15:28:02 +08:00
|
|
|
}
|
2015-01-07 13:16:55 +01:00
|
|
|
}
|