From cd6c94114788a7137c689a109d1b3665977b87a4 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Thu, 9 Aug 2012 15:31:55 -0400 Subject: [PATCH] Adding feedback servlets to add and dump out the data --- src/org/wescheme/data/DAO.java | 20 ++++++ src/org/wescheme/data/Feedback.java | 51 ++++++++++++++ src/org/wescheme/servlet/AddFeedback.java | 29 ++++++++ src/org/wescheme/servlet/DumpFeedback.java | 82 ++++++++++++++++++++++ war/WEB-INF/web.xml | 20 ++++++ 5 files changed, 202 insertions(+) create mode 100644 src/org/wescheme/data/DAO.java create mode 100644 src/org/wescheme/data/Feedback.java create mode 100644 src/org/wescheme/servlet/AddFeedback.java create mode 100644 src/org/wescheme/servlet/DumpFeedback.java diff --git a/src/org/wescheme/data/DAO.java b/src/org/wescheme/data/DAO.java new file mode 100644 index 00000000..8f5fe337 --- /dev/null +++ b/src/org/wescheme/data/DAO.java @@ -0,0 +1,20 @@ +package org.wescheme.data; + +import com.googlecode.objectify.ObjectifyService; +import com.googlecode.objectify.util.DAOBase; + +/** Data Access Object for all the data we're managing with Objectify. + * + * @author dyoo + * + */ + +public class DAO extends DAOBase { + static { + ObjectifyService.register(Feedback.class); + } + + public void saveFeedback(Feedback feedback) { + ofy().put(feedback); + } +} diff --git a/src/org/wescheme/data/Feedback.java b/src/org/wescheme/data/Feedback.java new file mode 100644 index 00000000..bebb2f18 --- /dev/null +++ b/src/org/wescheme/data/Feedback.java @@ -0,0 +1,51 @@ +package org.wescheme.data; + +import java.util.Date; + +import javax.persistence.Id; + +import org.json.simple.JSONObject; + +import com.googlecode.objectify.annotation.Unindexed; + + +/** Represents feedback we get back from WeScheme users. + * The content is unstructured for the most part; we may want + * to enforce some structure later one to help with data mining. + * @author dyoo + * + */ + +public class Feedback { + @Id Long id; + @Unindexed String author; + String type; + @Unindexed String feedbackText; + Date date; + + // NoArg constructor for Objectify. + Feedback() {} + + public Feedback(String author, String type, String feedbackText) { + this.author = author; + this.type = type; + this.feedbackText = feedbackText; + this.date = new Date(); + } + + + + public String getAuthor() { return this.author; } + public String getType() { return this.type; } + public String getFeedbackText() { return this.feedbackText; } + + + public JSONObject toJSONObject() { + JSONObject obj = new JSONObject(); + obj.put("id", this.id); + obj.put("author", this.author); + obj.put("type", this.type); + obj.put("date", this.date); + return obj; + } +} diff --git a/src/org/wescheme/servlet/AddFeedback.java b/src/org/wescheme/servlet/AddFeedback.java new file mode 100644 index 00000000..603a7514 --- /dev/null +++ b/src/org/wescheme/servlet/AddFeedback.java @@ -0,0 +1,29 @@ +package org.wescheme.servlet; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.wescheme.data.DAO; +import org.wescheme.data.Feedback; + +public class AddFeedback extends HttpServlet { + /** + * + */ + private static final long serialVersionUID = -7686196925524722519L; + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + String author = request.getParameter("author"); + String type = request.getParameter("type"); + String feedbackText = request.getParameter("feedbackText"); + Feedback feedback = new Feedback(author, type, feedbackText); + new DAO().saveFeedback(feedback); + + response.setContentType("text/plain"); + response.getWriter().write("ok"); + } +} diff --git a/src/org/wescheme/servlet/DumpFeedback.java b/src/org/wescheme/servlet/DumpFeedback.java new file mode 100644 index 00000000..7a6c0e32 --- /dev/null +++ b/src/org/wescheme/servlet/DumpFeedback.java @@ -0,0 +1,82 @@ +package org.wescheme.servlet; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.wescheme.data.Feedback; +import org.wescheme.user.Session; +import org.wescheme.user.SessionManager; + +import com.google.appengine.api.datastore.Cursor; +import com.google.appengine.api.datastore.QueryResultIterator; +import com.googlecode.objectify.Objectify; +import com.googlecode.objectify.ObjectifyService; +import com.googlecode.objectify.Query; + +/** + * Retrieve all the feedback we get back from users. + * Output is represented as an JSON string: + * { feedbacks: [{ author: string, type: string: feedbackText: string, date: string} ...], + * cursor: string } + * + * The optional "cursor" argument here will allow us to stream + * the table, just in case it gets large enough to hit against + * the compute ceiling enforced by AppEngine. + * + * Only admins should be allowed to get at this information. + * + * This code is adapted from http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify + * @author dyoo + * + */ +public class DumpFeedback { + + public static final long LIMIT_MILLIS = 1000 * 25; // provide a little leeway + + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + // First, check that the person is admin. + SessionManager sm = new SessionManager(); + Session userSession = sm.authenticate(request, response); + if (!userSession.isAdmin()) { + response.sendError(401); + return; + } + + + // Next, start dumping content till we hit CPU limit + long startTime = System.currentTimeMillis(); + Objectify ofy = ObjectifyService.begin(); + Query query = ofy.query(Feedback.class); + String cursorStr = request.getParameter("cursor"); + if (cursorStr != null) { + query.startCursor(Cursor.fromWebSafeString(cursorStr)); + } + + JSONArray listOfFeedbacks = new JSONArray(); + String nextCursorString = null; + + QueryResultIterator iterator = query.iterator(); + while(iterator.hasNext()) { + Feedback feedback = iterator.next(); + listOfFeedbacks.add(feedback.toJSONObject()); + if (System.currentTimeMillis() - startTime > LIMIT_MILLIS) { + nextCursorString = iterator.getCursor().toWebSafeString(); + break; + } + } + + // Finally, dump the content back to the user. + JSONObject result = new JSONObject(); + result.put("feedbacks", listOfFeedbacks); + result.put("cursor", nextCursorString); + response.setContentType("text/plain"); + response.getWriter().write(result.toString()); + } + +} diff --git a/war/WEB-INF/web.xml b/war/WEB-INF/web.xml index d58ac090..4b14b98b 100644 --- a/war/WEB-INF/web.xml +++ b/war/WEB-INF/web.xml @@ -140,6 +140,26 @@ + + AddFeedback + org.wescheme.servlet.AddFeedback + + + AddFeedback + /addFeedback + + + + + DumpFeedback + org.wescheme.servlet.DumpFeedback + + + DumpFeedback + /dumpFeedback + + +