-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding feedback servlets to add and dump out the data
- Loading branch information
Danny Yoo
committed
Aug 9, 2012
1 parent
34930f1
commit cd6c941
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Feedback> 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<Feedback> 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()); | ||
} | ||
|
||
} |
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