I have a servelet which can take a request from either a rest service or from a jsp form post that will both call a internal method (internalAddPodcast()) to add an entity to the datastore.
When I hit the internalAddPodcast() from the jsp page, it works fine, I can see the that the entity has been added successfully by querying for it right after adding. BUT when I do it from the rest method addPodcast() the datastore.put() doesn’t seem to be actually adding to the datastore, because I try and retreive it right after put() and nothing is coming back. Look down near the bottom of this class where I put the comment “//THIS QUERY IS EMPTY WHEN ADDED FROM THE REST SERVICE :(” That is where I expect to have some results come back, especially the entity that I just put in the data store.
package com.aol.sharepodder;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@Path("/add/podcast/")
public class AddPodcastServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(AddPodcastServlet.class
.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String email = req.getParameter("email");
String collectionName = req.getParameter("collectionName");
String podcast_url = req.getParameter("podcast_url");
String podcast_description = req.getParameter("podcast_description");
String podcast_title = req.getParameter("podcast_title");
log.info("--post adding " + collectionName);
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
resp.sendRedirect("/collection_details.jsp?collectionName="
+ collectionName + "&email=" + email);
}
@POST
@Produces("text/plain")
@Consumes("application/x-www-form-urlencoded")
public String addPodcast(
@DefaultValue("barrand@gmail.com") @FormParam("email") String email,
@DefaultValue("default") @FormParam("collectionName") String collectionName,
@DefaultValue("") @FormParam("podcast_url") String podcast_url,
@DefaultValue("") @FormParam("podcast_description") String podcast_description,
@DefaultValue("") @FormParam("podcast_title") String podcast_title) {
try {
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
if (podcast_url == "") {
return "No url supplied";
}
return "true";
} catch (Exception e) {
return e.getMessage();
}
}
private void internalAddPodcast(String email, String collectionName,
String podcast_url, String podcast_description, String podcast_title) {
log.info("--INTERNAL ADD ");
log.info("--email " + email);
log.info("--collectionName " + collectionName);
log.info("--podcast_url " + podcast_url);
log.info("--podcast_description " + podcast_description);
log.info("--podcast_title " + podcast_title);
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
Entity podcast = new Entity("Podcast");
podcast.setProperty("collectionName", collectionName);
podcast.setProperty("user", user);
podcast.setProperty("email", email);
Date date = new Date();
podcast.setProperty("date", date);
podcast.setProperty("podcast_title", podcast_title);
podcast.setProperty("podcast_description", podcast_description);
podcast.setProperty("podcast_url", podcast_url);
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
datastore.put(podcast);
//try to log the podcast that I just got done adding to the datastore
Query query = new Query("Podcast");
PreparedQuery pq = datastore.prepare(query);
//THIS QUERY IS EMPTY WHEN ADDED FROM THE REST SERVICE :(
for (Entity p : pq.asIterable()) {
log.info("_loop " + " - " + KeyFactory.keyToString(p.getKey())
+ " -- " + p.getProperty("podcast_title") + " - "
+ p.getProperty("podcast_url"));
}
}
}
Any ideas what I’m doing wrong, and why the entity I’m trying to add from the rest method isn’t getting added to the data store.
I know that in both cases, (either from the jsp post, or the rest service) when I get to the internalAddPodcast() all the method params are coming in correctly.
Ah HA! I found it. I wasn’t logging the exception that was being thrown. Basically I was trying to store a string property that was more than 500 characters and it was throwing an exception that I needed to pay attention to 🙂 So it was never getting to the datastore.put()