@PUT @Consumes(MediaType.APPLICATION_XML) public Response putTaskData(String value) { Response res = null; // add your code here // first check if the Entity exists in the datastore DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); Key entKey = KeyFactory.createKey("TaskData", keyname); Date date = new Date(); try { // if it is, signal that we updated the entity Entity ts = datastore.get(entKey); ts.setProperty("value", value); ts.setProperty("date", date); datastore.put(ts); res = Response.noContent().build(); } catch (EntityNotFoundException e) { // if it is not, create it and // signal that we created the entity in the datastore Entity taskdata = new Entity("TaskData", keyname); taskdata.setProperty("value", value); taskdata.setProperty("date", date); datastore.put(taskdata); res = Response.created(uriInfo.getAbsolutePath()).build(); } TaskData td = new TaskData(keyname, value, date); syncCache.put(keyname, td); return res; }
@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(); } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public String addRoute(Route route, @Context HttpServletResponse servletResponse) throws IOException { try { Entity routeEntity = new Entity("Route"); routeEntity.setProperty("originPlaceID", route.getOrigin().getGooglePlaceId()); routeEntity.setProperty("destinationPlaceID", route.getDestination().getGooglePlaceId()); routeEntity.setProperty("duration", route.getDuration()); routeEntity.setProperty("distance", route.getDistance()); routeEntity.setProperty("origin", route.getOrigin().getAddress()); routeEntity.setProperty("destination", route.getDestination().getAddress()); Text mapJsonAsText = route.getMapJsonAsText(); routeEntity.setProperty("routeJSON", mapJsonAsText); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); String cacheKey = "route-" + key.nextLong(); syncCache.put(cacheKey, routeEntity); return new JSONObject().append("status", "OK").append("key", cacheKey).toString(); } catch (Exception e) { return new JSONObject() .append("status", "fail") .append("message", "Could not cache object.") .toString(); } }
@DELETE @Path("{route_id}") @Produces(MediaType.APPLICATION_JSON) public String deletePlace(@PathParam("route_id") String routeID) { syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); JSONObject reply = new JSONObject(); try { syncCache.delete(routeID); return reply .append("status", "OK") .append("message", "successfully deleted route " + routeID + " from " + "memcache") .toString(); } catch (Exception e) { return new JSONObject().append("status", "fail").append("message", e.getMessage()).toString(); } }