@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;
 }
示例#2
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    MemcacheService ms;

    Gson gson;
    String linkID;
    List<String> linkList;

    ms = MemcacheServiceFactory.getMemcacheService();

    gson = new Gson();
    linkID = req.getParameter("linkid");
    linkList = gson.fromJson(req.getParameter("linklist"), List.class);

    ms.put("LinkList_" + linkID, linkList);
  }
  @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");
    }
  }
  @Override
  public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
      throws IOException {

    final MemcacheService cacheShared = MemcacheServiceFactory.getMemcacheService();
    if (cacheShared.contains(MemCacheKey.activePoints)) {
      try {
        final Map<String, Point> points =
            (Map<String, Point>) cacheShared.get(MemCacheKey.activePoints);
        cacheShared.delete(
            MemCacheKey.activePoints); // TODO possible race condition with record value service
        for (final Point point : points.values()) {
          TaskFactory.getInstance().startMoveCachedValuesToStoreTask(point);
        }
      } catch (InvalidValueException e) {
        cacheShared.clearAll();
      }
    }
  }
 // 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");
   }
 }
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html");
    resp.getWriter().println("<html><body>");

    String keyname = req.getParameter("keyname");
    String value = req.getParameter("value");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    // Using the synchronous cache.
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

    // display every element of kind TaskData for /datastore
    if (req.getParameterMap().isEmpty()) {
      // querying from datastore
      resp.getWriter().println("<h3>Datastore results:</h3>");
      List<String> listOfKeys = new ArrayList<String>();
      Query q = new Query("TaskData");
      PreparedQuery pq = datastore.prepare(q);
      for (Entity result : pq.asIterable()) {
        String datastore_key = result.getKey().getName();
        String taskData_value = (String) result.getProperty("value");
        Date taskData_date = (Date) result.getProperty("date");
        resp.getWriter()
            .println(
                "<p>keyname = "
                    + datastore_key
                    + "  value = "
                    + taskData_value
                    + " date = "
                    + taskData_date.toString()
                    + "</p>");
        listOfKeys.add(datastore_key);
      }
      // check which of the keys exist in memcache
      String memcache_value;
      resp.getWriter().println("<h3>Memcache results:</h3>");
      for (String datastore_key : listOfKeys) {
        memcache_value = (String) syncCache.get(datastore_key);
        if (memcache_value != null) {
          // String decoded = new String(memcache_value, "UTF-8");
          resp.getWriter()
              .println("<p>keyname = " + datastore_key + " value = " + memcache_value + "</p>");
        }
      }
    }

    // display element of kind TaskData with key=keyname
    else if (keyname != null && value == null) {

      // first check in the cache
      String memcache_value = (String) syncCache.get(keyname); // Read from cache.
      // Get value from datastore
      Key task_key = KeyFactory.createKey("TaskData", keyname);
      try {
        Entity tne = datastore.get(task_key);
        if (memcache_value == null) {
          resp.getWriter().println("<h2>Datastore</h2>");
        } else {
          resp.getWriter().println("<h2>Both</h2>");
        }

      } catch (EntityNotFoundException e) {
        resp.getWriter().println("<h2>Neither</h2>");
      }
    }

    // store element of kind TaskData with key=keyname and value=value
    else if (keyname != null && value != null) {
      Entity tne = new Entity("TaskData", keyname);
      tne.setProperty("value", value);
      tne.setProperty("date", new Date());
      datastore.put(tne);
      syncCache.put(keyname, value); // Populate cache.
      resp.getWriter()
          .println("<h2>Stored " + keyname + " and " + value + " in Datastore and Memcache</h2>");
    } else {

      resp.getWriter().println("<h2>You entered wrong query parameters</h2>");
    }

    /*
       Entity tne = new Entity("TaskData", "Person");
    alice.setProperty("gender", "female");
    alice.setProperty("age", 20);
    */

    resp.getWriter().println("</body></html>");
  }
示例#8
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    int index;

    DatastoreService ds;
    MemcacheService ms;

    Cookie[] cookies;
    boolean insideFlag;

    String delpw;
    String paramOffset;
    String paramSize;
    int offset;
    int size;

    Key postObjGroupKey;
    Query q;
    List<Entity> postObjList;
    PostObj postObj;

    Gson gson;
    List<String> filelinkList;

    resp.setCharacterEncoding("UTF-8");
    resp.setContentType("text/plain");

    ds = DatastoreServiceFactory.getDatastoreService();
    ms = MemcacheServiceFactory.getMemcacheService();

    insideFlag = false;
    try {
      cookies = req.getCookies();
      if (cookies.length > 0) {
        if (ms.contains(cookies[0].getValue()) == true) {
          insideFlag = true;
        }
      }
    } catch (Exception e) {
      insideFlag = false;
    }

    delpw = req.getParameter("delpw");
    if (delpw != null) {
      if (delpw.equals("") == true) {
        delpw = null;
      }
    }

    paramOffset = req.getParameter("offset");
    if (paramOffset != null) {
      offset = Integer.valueOf(paramOffset);
    } else {
      offset = 0;
    }

    paramSize = req.getParameter("size");
    if (paramSize != null) {
      size = Integer.valueOf(paramSize);
    } else {
      size = 4096;
    }

    postObjGroupKey = KeyFactory.createKey("PostObjGroup", 1L);
    q = new Query("PostObj", postObjGroupKey);
    if (delpw != null) {
      q.addFilter("delpw", FilterOperator.EQUAL, delpw);
      q.addSort("delpw");
    }
    q.addSort("posttime", SortDirection.DESCENDING);
    postObjList = ds.prepare(q).asList(FetchOptions.Builder.withOffset(offset).limit(size));

    postObj = new PostObj();
    filelinkList = new ArrayList<String>();
    for (index = 0; index < postObjList.size(); index++) {
      postObj.getDB(postObjList.get(index));
      if ((postObj.flag.equals("showgallery") == false && insideFlag == true)
          || (postObj.flag.equals("showgallery") == true && insideFlag == false)
          || delpw != null) {
        filelinkList.add(postObj.filelink);
      }
    }

    gson = new Gson();
    resp.getWriter().print(gson.toJson(filelinkList));
  }