@GET @Path("{route_id}") @Produces(MediaType.APPLICATION_JSON) public String getPlace(@PathParam("route_id") String routeID) { JSONObject reply = new JSONObject(); try { syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); Entity result = (Entity) syncCache.get(routeID); if (result != null) return reply .append("status", "OK") .append("message", "Found in memcache.") .append("Route Object", result) .toString(); return reply.append("status", "fail").append("message", "Not found in memcache.").toString(); } catch (Exception e) { return new JSONObject() .append("status", "fail") .append("message", "Route object with ID " + routeID + " not found.") .toString(); } }
// for the application @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public TaskData getTaskData() { // same code as above method DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); if (syncCache.get(keyname) != null) { TaskData ts = (TaskData) syncCache.get(keyname); return ts; } Key entKey = KeyFactory.createKey("TaskData", keyname); try { Entity ent = datastore.get(entKey); TaskData ts = new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date")); return ts; } catch (EntityNotFoundException e) { throw new RuntimeException("Get: TaskData with " + keyname + " not found"); } }
// for the browser @GET @Produces(MediaType.TEXT_XML) public TaskData getTaskDataHTML() { // add your code here (get Entity from datastore using this.keyname) // throw new RuntimeException("Get: TaskData with " + keyname + " not found"); // if not found DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); if (syncCache.get(keyname) != null) { TaskData ts = (TaskData) syncCache.get(keyname); return ts; } Key entKey = KeyFactory.createKey("TaskData", keyname); try { Entity ent = datastore.get(entKey); TaskData ts = new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date")); return ts; } catch (EntityNotFoundException e) { throw new RuntimeException("Get: TaskData with " + keyname + " not found"); } }
@DELETE public void deleteIt() { // delete an entity from the datastore // just print a message upon exception (don't throw) DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); if (syncCache.get(keyname) != null) { syncCache.delete(keyname); } Key entKey = KeyFactory.createKey("TaskData", keyname); try { Entity ent = datastore.get(entKey); datastore.delete(entKey); System.err.println("TaskData deleted in Datastore"); } catch (EntityNotFoundException e) { System.err.println("TaskData not found in Datastore"); } }