@SuppressWarnings({ "unchecked", "unused" })
  @ApiMethod(name = "listMessages")
  public CollectionResponse<MessageData> listMessages(
      @Nullable @Named("cursor") String cursorString,
      @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;  
    Cursor cursor = null;
    List<MessageData> execute = null;
    
    try {
      mgr = getEntityManager();
      // query for messages, newest message first
      Query query = mgr
          .createQuery("select from MessageData as MessageData order by timestamp desc");
      if (cursorString != null && cursorString != "") {
        cursor = Cursor.fromWebSafeString(cursorString);
        query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
      }

      if (limit != null) {
        query.setFirstResult(0);
        query.setMaxResults(limit);
      }

      execute = (List<MessageData>) query.getResultList();
      cursor = JPACursorHelper.getCursor(execute);
      if (cursor != null)
        cursorString = cursor.toWebSafeString();

      // Tight loop for fetching all entities from datastore and accomodate
      // for lazy fetch.
      for (MessageData obj : execute) {
        ;
      }
    } finally {
      mgr.close();
    }

    return CollectionResponse.<MessageData> builder().setItems(execute)
        .setNextPageToken(cursorString).build();
  }