254 lines
7.8 KiB
Java
Raw Normal View History

2015-01-07 13:16:55 +01:00
package de.svenkubiak.jpushover;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
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
/**
*
* @author svenkubiak
*
*/
2015-01-07 13:16:55 +01:00
public class JPushover {
private static final Logger LOG = LoggerFactory.getLogger(JPushover.class);
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;
2015-01-07 20:11:23 +01:00
private Priority pushoverPriority;
private Sound pushoverSound;
2015-01-07 13:16:55 +01:00
public JPushover(){
}
/**
* Your application's API token
* (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
*/
public JPushover token(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>
* (required)
*
2015-01-08 08:12:55 +01:00
* @param user The username
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover user(String user) {
2015-01-07 20:11:23 +01:00
this.pushoverUser = user;
2015-01-07 13:16:55 +01:00
return this;
}
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.
*
* @param retry Number of seconds
* @return JPushover instance
*/
public JPushover retry(String retry) {
this.pushoverRetry = retry;
return this;
}
/**
* Specifies how many seconds your notification will continue to be retried for (every retry seconds).
* Only required if priority is set to emergency.
*
* @param expire Number of seconds
* @return JPushover instance
*/
public JPushover expire(String expire) {
this.pushoverExpire = expire;
return this;
}
2015-01-07 13:16:55 +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
*/
public JPushover message(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-01-08 08:12:55 +01:00
* @param device The device name
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover device(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-01-08 08:12:55 +01:00
* @param title The title
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover title(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-01-08 08:12:55 +01:00
* @param url The url
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover url(String url) {
2015-01-07 20:11:23 +01:00
this.pushoverUrl = url;
2015-01-07 13:16:55 +01:00
return this;
}
/**
* A title for your supplementary URL, otherwise just the URL is shown
*
2015-01-08 08:12:55 +01:00
* @param urlTitle The url title
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover urlTitle(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-01-08 08:12:55 +01:00
* @param timestamp The Unix timestamp
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover timestamp(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-01-08 08:12:55 +01:00
* @param priority The priority enum
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover priority(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-01-08 08:12:55 +01:00
* @param sound THe sound enum
2015-01-07 13:16:55 +01:00
* @return JPushover instance
*/
public JPushover sound(Sound sound) {
2015-01-07 20:11:23 +01:00
this.pushoverSound = sound;
2015-01-07 13:16:55 +01:00
return this;
}
2015-01-09 07:56:01 +01:00
/**
* Callback parameter may be supplied with a publicly-accessible URL that the
* Pushover servers will send a request to when the user has acknowledged your
* notification.
* Only required if priority is set to emergency.
*
* @param callback
* @return
*/
public JPushover callback(String callback) {
this.pushoverCallback = callback;
return this;
}
2015-01-07 13:16:55 +01:00
/**
* Send the message to pushover
*/
public JPushoverResponse push() {
2015-01-07 20:11:23 +01:00
Preconditions.checkNotNull(this.pushoverToken, "Token is required");
Preconditions.checkNotNull(this.pushoverUser, "User is required");
Preconditions.checkNotNull(this.pushoverMessage, "Message is required");
2015-01-07 13:16:55 +01:00
2015-01-09 07:56:01 +01:00
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");
}
2015-01-07 13:16:55 +01:00
List<NameValuePair> params = Form.form()
2015-01-07 20:11:23 +01:00
.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)
2015-01-09 07:56:01 +01:00
.add(Constants.RETRY.get(), this.pushoverRetry)
.add(Constants.EXPIRE.get(), this.pushoverExpire)
.add(Constants.CALLBACK.get(), this.pushoverCallback)
2015-01-07 20:11:23 +01:00
.add(Constants.URLTITLE.get(), this.pushoverUrlTitle)
2015-01-09 07:56:01 +01:00
.add(Constants.PRIORITY.get(), (this.pushoverPriority == null) ? null : this.pushoverPriority.get())
2015-01-07 20:11:23 +01:00
.add(Constants.TIMESTAMP.get(), this.pushoverTimestamp)
2015-01-09 07:56:01 +01:00
.add(Constants.SOUND.get(), (this.pushoverSound == null) ? null : this.pushoverSound.get())
2015-01-07 13:16:55 +01:00
.build();
2015-01-09 07:56:01 +01:00
2015-01-07 13:16:55 +01:00
HttpResponse httpResponse = null;
JPushoverResponse jPushoverResponse = null;
try {
2015-01-07 20:11:23 +01:00
httpResponse = Request.Post(Constants.PUSHOVER_URL.get()).bodyForm(params, Consts.UTF_8).execute().returnResponse();
2015-01-07 13:16:55 +01:00
if (httpResponse != null) {
int status = httpResponse.getStatusLine().getStatusCode();
jPushoverResponse = new JPushoverResponse()
.httpStatus(status)
.response(IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8))
.isSuccessful((status == 200) ? true : false);
}
} catch (IOException e) {
LOG.error("Failed to send message to pushover", e);
}
return (jPushoverResponse == null) ? new JPushoverResponse().isSuccessful(false) : jPushoverResponse;
}
}