Skip to content

Commit

Permalink
intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sps committed Jul 27, 2010
0 parents commit 08730a5
Show file tree
Hide file tree
Showing 12 changed files with 612 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cobertura.ser
.classpath
.project
.settings
target/
*.swp
44 changes: 44 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.notifo4j</groupId>
<artifactId>notifo-client</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>notifo4j</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
39 changes: 39 additions & 0 deletions src/main/java/com/notifo/client/NotifoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2010 Sean P. Scanlon.
* All rights reserved. Unauthorized disclosure or distribution is prohibited.
*/
package com.notifo.client;

/**
* @author sscanlon
*
*/
public interface NotifoClient {

/**
*
* @param messageBody
* @return
* @throws NotifoException
*/
NotifoResponse sendMessage(NotifoMessage message) throws NotifoException;

/**
* Send a message to a particular user
*/
NotifoResponse sendMessage(String to, String messageBody) throws NotifoException;

/**
* Send a message to the default user (you)
*/
NotifoResponse sendMessage(String messageBody) throws NotifoException;

/**
*
* @param userName
* @return
* @throws NotifoException
*/
NotifoResponse subscribeUser(String userName) throws NotifoException;

}
18 changes: 18 additions & 0 deletions src/main/java/com/notifo/client/NotifoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (C) 2010 Sean P. Scanlon.
* All rights reserved. Unauthorized disclosure or distribution is prohibited.
*/
package com.notifo.client;

/**
* @author sscanlon
*
*/
public class NotifoException extends Exception {

private static final long serialVersionUID = 1L;

public NotifoException(String message, Throwable cause) {
super(message, cause);
}
}
140 changes: 140 additions & 0 deletions src/main/java/com/notifo/client/NotifoHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Copyright (C) 2010 Sean P. Scanlon
* All rights reserved. Unauthorized disclosure or distribution is prohibited.
*/
package com.notifo.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

/**
* @author sscanlon
*
*/
public class NotifoHttpClient implements NotifoClient {

private static final String SEND_NOTIFICATION_URL = "https://api.notifo.com/v1/send_notification";

private static final String SUBSCRIBE_USER_URL = "https://api.notifo.com/v1/subscribe_user";

private static final AuthScheme AUTH_SCHEME = new BasicScheme();

private HttpClient httpClient = new DefaultHttpClient();

private final UsernamePasswordCredentials credentials;

private final String userName;

/**
* Construct a client with a username and apikey. "username" will be the default recipient of
* messages
*
* @param userName
* @param apiKey
*/
public NotifoHttpClient(String userName, String apiKey) {
this.userName = userName;
this.credentials = new UsernamePasswordCredentials(userName, apiKey);
((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
request.addHeader(AUTH_SCHEME.authenticate(credentials, request));
}
});
}

@Override
public NotifoResponse sendMessage(String messageBody) throws NotifoException {
return sendMessage(new NotifoMessage(this.userName, messageBody));
}

@Override
public NotifoResponse sendMessage(String to, String messageBody) throws NotifoException {
return sendMessage(new NotifoMessage(to, messageBody));
}

/**
* Send a message with the parameters in {@link NotifoMessage}
*/
@Override
public NotifoResponse sendMessage(NotifoMessage message) throws NotifoException {

HttpPost post = new HttpPost(SEND_NOTIFICATION_URL);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("to", message.getTo()));
nvps.add(new BasicNameValuePair("msg", message.getMessage()));

addPairIfNotNull(nvps, "title", message.getSubject());
addPairIfNotNull(nvps, "uri", message.getUrl());
addPairIfNotNull(nvps, "label", message.getLabel());

try {
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new NotifoException(e.getMessage(), e.getCause());
}

try {
HttpResponse response = httpClient.execute(post);

return NotifoResponseFactory.parseResponse(response);

} catch (Exception e) {
throw new NotifoException(e.getMessage(), e.getCause());
}
}

@Override
public NotifoResponse subscribeUser(String userName) throws NotifoException {
HttpPost post = new HttpPost(SUBSCRIBE_USER_URL);
try {
post.setEntity(
new UrlEncodedFormEntity(
Arrays.asList(
new BasicNameValuePair("username", userName)), HTTP.UTF_8)
);
return NotifoResponseFactory.parseResponse(httpClient.execute(post));
} catch (Exception e) {
throw new NotifoException(e.getMessage(), e.getCause());
}

}

private void addPairIfNotNull(List<NameValuePair> nvps, String key, Object value) {
if (value != null) {
nvps.add(new BasicNameValuePair(key, value.toString()));
}

}

/**
* Override the default http client
*
* @param httpClient
*/
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}

}
83 changes: 83 additions & 0 deletions src/main/java/com/notifo/client/NotifoMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (C) 2010 Sean P. Scanlon.
* All rights reserved. Unauthorized disclosure or distribution is prohibited.
*/
package com.notifo.client;

/**
* @author sscanlon
*
*/
public class NotifoMessage {

private final String to;
private final String message;
private String subject;
private String label;
private String url;

public NotifoMessage(String to, String message) {
this.to = to;
this.message = message;
}

/**
* @return the subject
*/
public String getSubject() {
return subject;
}

/**
* @param subject
* the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}

/**
* @return the label
*/
public String getLabel() {
return label;
}

/**
* @param label
* the label to set
*/
public void setLabel(String label) {
this.label = label;
}

/**
* @return the to
*/
public String getTo() {
return to;
}

/**
* @return the message
*/
public String getMessage() {
return message;
}

/**
* @return the url
*/
public String getUrl() {
return url;
}

/**
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
}

}
62 changes: 62 additions & 0 deletions src/main/java/com/notifo/client/NotifoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (C) 2010 Sean P Scanlon.
* All rights reserved. Unauthorized disclosure or distribution is prohibited.
*/
package com.notifo.client;

import com.google.gson.annotations.SerializedName;

/**
* @author sscanlon
*
*/
public class NotifoResponse {

private static final String SUCCESS = "success";

private String status;

@SerializedName("response_code")
private int responseCode;

@SerializedName("response_message")
private String responseMessage;

public boolean isOk() {
return SUCCESS.equalsIgnoreCase(status);
}

@Override
public String toString() {
return new StringBuilder()
.append("status: ").append(status)
.append(", responseCode: ").append(responseCode)
.append(", responseMessage: ").append(responseMessage)
.toString();
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public int getResponseCode() {
return responseCode;
}

public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}

public String getResponseMessage() {
return responseMessage;
}

public void setResponseMessage(String responseMessage) {
this.responseMessage = responseMessage;
}

}
Loading

0 comments on commit 08730a5

Please sign in to comment.