Created
October 12, 2009 10:48
-
-
Save dlinsin/208326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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