Skip to content

Instantly share code, notes, and snippets.

@dlinsin
Created October 12, 2009 10:48
Show Gist options
  • Save dlinsin/208326 to your computer and use it in GitHub Desktop.
Save dlinsin/208326 to your computer and use it in GitHub Desktop.
public class AppNotificationService implements NotificationService {
private final String credentials;
public static final String USER_CREDENTIALS = "user_credentials";
public static final String NOTIFICATION_LONG_MESSAGE = "notification[long_message]";
public static final String NOTIFICATION_TITLE = "notification[title]";
public static final String MESSAGE_LEVEL = "message_level";
public static final String URL = "https://www.appnotifications.com/account/notifications.xml";
public static final String HTTP_USERAGENT = "http.useragent";
public AppNotificationService(String argCredentials) {
credentials = argCredentials;
}
public boolean notify(String argTitle, String argMessage) {
HttpClient client;
PostMethod method = null;
try {
client = setUp();
method = setUp(argTitle, argMessage);
int returnCode = client.executeMethod(method);
if (returnCode > HttpStatus.SC_OK) {
return false;
} else {
logger.info("Sent notification!");
return true;
}
} catch (Exception e) {
throw new AppNotificationException();
} finally {
if (method != null) {
method.releaseConnection();
}
}
}
HttpClient setUp() {
HttpClient client = new HttpClient();
client.getParams().setParameter(HTTP_USERAGENT, "IRC Bot");
return client;
}
PostMethod setUp(String argTitle, String argMessage) {
PostMethod method = new PostMethod(URL);
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
method.addParameter(USER_CREDENTIALS, credentials);
method.addParameter(NOTIFICATION_LONG_MESSAGE, argMessage);
method.addParameter(NOTIFICATION_TITLE, argTitle);
method.addParameter(MESSAGE_LEVEL, "2");
return method;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment