Skip to content

Commit

Permalink
completes simple version of notification api
Browse files Browse the repository at this point in the history
* notifications can be PUT, and if an matching email is provided a notification is stored for the user. refs #202
  • Loading branch information
kraxner committed Aug 28, 2014
1 parent d1948d9 commit 98cb2c3
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 64 deletions.
Expand Up @@ -2,37 +2,90 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;

import eu.scape_project.planning.model.Notification;
import eu.scape_project.planning.model.User;

@Path("/")
@Consumes({"application/json"})
@Produces({"application/json"})
public class NotificationResource {
static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(NotificationResource.class);


@POST
@Path("/notification")
public WatchNotification addNotification(WatchNotification notification){
LOGGER.info("recieved a notification: {}", notification.getMessage());
return null;
}

@GET
@Path("/notifications")
public Collection<WatchNotification> getNotifications(){
ArrayList<WatchNotification> list = new ArrayList<WatchNotification>();
WatchNotification n = new WatchNotification();
n.setMessage("test message");
list.add(n);
return list;
}
static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(NotificationResource.class);

@PersistenceContext
private EntityManager em;

@Resource
UserTransaction ut;


/**
* adds the notification in the system.
* If there is a user with email equal to plannerEmail registered, a notification is stored.
*
* @param notification
* @return
*/
@PUT
@Path("/notification")
public boolean addNotification(WatchNotification notification) {
LOGGER.info("recieved a notification: {}", notification.getMessage());

List<User> recipients = new ArrayList<User>();

if (notification.getPlannerEmail() != null) {
User recipient = em.createQuery("select u from User u where u.email = :email", User.class)
.setParameter("email", notification.getPlannerEmail()).getSingleResult();
if (recipient != null) {
recipients.add(recipient);
}
}
// TODO add users related to plan with repository id planId
// ..
if (recipients.size() > 0) {
String notificationID = UUID.randomUUID().toString();
Date notificationTime = new Date();

String message = notification.getMessage();

try {
ut.begin();
for (User recipient : recipients) {
Notification n = new Notification(notificationID, notificationTime, "SCOUT", message, recipient);
em.persist(n);
}
ut.commit();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}

@GET
@Path("/notifications")
public Collection<WatchNotification> getNotifications() {
ArrayList<WatchNotification> list = new ArrayList<WatchNotification>();
WatchNotification n = new WatchNotification(null, "test message", null, null, "test@test.com");
list.add(n);
return list;
}
}
Expand Up @@ -2,45 +2,47 @@


public class WatchNotification {
private String planId;

private String message;

private String measureUri;

private String value;

public String getPlanId() {
return planId;
}

public void setPlanId(String planId) {
this.planId = planId;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getMeasureUri() {
return measureUri;
}

public void setMeasureUri(String measureUri) {
this.measureUri = measureUri;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}



private String planId;

private String message;

private String measureUri;

private String value;

private String plannerEmail;

public WatchNotification(String planId, String message, String measureUri, String value, String plannerEmail) {
super();
this.planId = planId;
this.message = message;
this.measureUri = measureUri;
this.value = value;
this.plannerEmail = plannerEmail;
}

public WatchNotification() {
}


public String getPlanId() {
return planId;
}

public String getMessage() {
return message;
}

public String getMeasureUri() {
return measureUri;
}

public String getValue() {
return value;
}

public String getPlannerEmail() {
return plannerEmail;
}
}
4 changes: 0 additions & 4 deletions plato/src/main/webapp/WEB-INF/faces-config.xml
Expand Up @@ -7,10 +7,6 @@
<converter-id>NumberConverter</converter-id>
<converter-class>eu.scape_project.planning.converters.NumberConverter</converter-class>
</converter>
<converter>
<converter-id>fastTrackTemplateConverter</converter-id>
<converter-class>eu.scape_project.planning.converters.FastTrackTemplateConverter</converter-class>
</converter>
<validator>
<validator-id>TargetValueValidator</validator-id>
<validator-class>eu.scape_project.planning.validators.TargetValueValidator</validator-class>
Expand Down
15 changes: 15 additions & 0 deletions plato/src/main/webapp/WEB-INF/web.xml
Expand Up @@ -8,6 +8,11 @@
<session-timeout>60</session-timeout>
</session-config>

<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<listener>
<display-name>SessionTimoutListener</display-name>
<listener-class>eu.scape_project.planning.application.SessionTimeoutListener</listener-class>
Expand Down Expand Up @@ -41,7 +46,9 @@
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>



<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
Expand All @@ -66,6 +73,14 @@
<role-name>authenticated</role-name>
</auth-constraint>
</security-constraint>

<security-constraint>
<web-resource-collection>
<web-resource-name>Plato-REST</web-resource-name>
<url-pattern>/rest/*</url-pattern>
</web-resource-collection>
</security-constraint>

<security-role>
<description>
The role that is required to log in to Plato
Expand Down
@@ -0,0 +1,56 @@
package eu.scape_project.planning.services.notification;

import java.io.StringWriter;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import eu.scape_project.planning.annotation.IntegrationTest;

@Category(IntegrationTest.class)
public class NotificationResourceIT {

/**
* adds a test notification.
* - to user scape.pw@gmail.com
* - the server must be running on localhost:8080
*
* @throws Exception
*/
@Test
public void addNotificationTest() throws Exception{
DefaultHttpClient httpClient = new DefaultHttpClient();

WatchNotification n = new WatchNotification(null, "test message", null, null, "scape.pw@gmail.com");

StringWriter writer = new StringWriter();

ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, n);

HttpPut putRequest = new HttpPut("http://localhost:8080/plato/rest/notification");
putRequest.addHeader("content-type", "application/json");

putRequest.setEntity(new StringEntity(writer.getBuffer().toString()));

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

int statusCode = response.getStatusLine().getStatusCode();

Assert.assertEquals(200, statusCode);

} finally {
httpClient.getConnectionManager().shutdown();
}

}

}

0 comments on commit 98cb2c3

Please sign in to comment.