Пример #1
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    if (!TDUserService.isUserLoggedIn(req.getSession(false))) {
      resp.sendRedirect("login.jsp");
      return;
    }

    String dishIDs = req.getParameter("dishID");
    String photoIDs = req.getParameter("photoID");

    long dishID = 0;
    long photoID = 0;

    try {
      // check the user is logged in
      TDUserService.getUser(req.getSession());

      if (photoIDs != null) {
        photoID = Long.valueOf(photoIDs);
        dishID = Long.valueOf(dishIDs);
        Photo photo = Datastore.get(KeyFactory.createKey(Photo.class.getSimpleName(), photoID));
        photo.rotateImage();
        Datastore.put(photo);
        resp.sendRedirect("/editDish.jsp?dishID=" + dishID);
      }
    } catch (UserNotLoggedInException e) {
      // forward to login screen
      resp.sendRedirect("login.jsp");
    } catch (UserNotFoundException e) {
      // forward to login screen
      resp.sendRedirect("login.jsp");
    }
  }
Пример #2
0
  /**
   * Get the API Key
   *
   * @return user's api key
   */
  public String getApiKey() {
    if (null == this.ApiKey) {
      this.ApiKey = UUID.randomUUID().toString();
    }

    Datastore.put(this);
    return this.ApiKey;
  }
Пример #3
0
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // topdish1.appspot.com/api/dishDetail?id[]=1&id[]=2&id[]=5...

    String[] ids = req.getParameterValues(APIConstants.ID_ARRAY);
    Set<Key> dishKeys = new HashSet<Key>();

    for (int i = 0; i < ids.length; i++) {
      try {
        Long id = Long.parseLong(ids[i]);
        dishKeys.add(KeyFactory.createKey(Dish.class.getSimpleName(), id));
      } catch (NumberFormatException e) {
        // malformed input
      } catch (JDOObjectNotFoundException e) {
        // object not found, skipping
      }
    }

    final Set<Dish> dishes = Datastore.get(dishKeys);
    final Set<DishLite> dishLites = ConvertToLite.convertDishes(dishes);

    if (!dishLites.isEmpty()) {
      final JSONArray array = new JSONArray();

      // Traverse dishes
      for (final DishLite dish : dishLites) {
        try {
          // Put a new JSONObject of the Dish in the Array
          array.put(new JSONObject(new Gson().toJson(dish)));

        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      // Return array (empty or not)
      resp.getWriter().write(APIUtils.generateJSONSuccessMessage(DishConstants.DISHES, array));
    } else resp.getWriter().write(APIUtils.generateJSONFailureMessage("No dishes found"));
  }
Пример #4
0
  /**
   * Handles both a POST and a GET <br>
   * Note: This is required as the POST will come from a Mobile User, where as the GET will occur on
   * redirect from Google Auth
   *
   * @param req - the request
   * @param resp - the response
   * @throws ServletException
   * @throws IOException
   */
  private void doLogic(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    // Get Writer
    final PrintWriter pw = resp.getWriter();

    try {

      // Get redirection url
      final String redirect = req.getParameter(APIConstants.REDIRECT);

      Logger.getLogger(TAG).info("Final Redirection is: " + redirect);

      // If login was successful (or user is already logged in)
      if (TDUserService.isGoogleUser(req)) {
        Logger.getLogger(TAG).info("User logged in, redirecting to: " + redirect);

        try {

          TDUser user = null;

          try {
            // Get the user
            user = TDUserService.getUser(req.getSession());
          } catch (Exception e) {

            Logger.getLogger(TAG).info(e.getMessage() + " means no user.");
          }

          if (null == user) {
            Logger.getLogger(TAG).info("No user exists, creating a new user");
            final User gUser = UserServiceFactory.getUserService().getCurrentUser();
            final String nickname =
                (null != gUser.getNickname()
                        && !gUser.getNickname().isEmpty()
                        && gUser.getNickname().indexOf("@") >= 0
                    ? (gUser.getNickname().substring(0, gUser.getNickname().indexOf("@")))
                    : gUser.getEmail());
            user = new TDUser(gUser, nickname, gUser.getEmail());
            Datastore.put(user);
          } else {
            Logger.getLogger(TAG).info("User " + user.getKey() + " found.");
          }

          Logger.getLogger(TAG).info("User's API Key is: " + user.getApiKey());

          // Redirect to given url with the TDUser Id
          resp.sendRedirect(
              redirect
                  + (redirect.contains("?") ? "&" : "?")
                  + UserConstants.TDUSER_ID
                  + "="
                  + user.getKey().getId()
                  + "&"
                  + UserConstants.API_KEY
                  + "="
                  + URLEncoder.encode(user.getApiKey(), "UTF-8"));
        } catch (Exception e) {
          Logger.getLogger(TAG).error(e.getMessage(), e);
          // Ensure some kind of redirect
          resp.sendRedirect(redirect);
        }
      } else {
        // Create a url
        final String url = TDUserService.getGoogleLoginURL("/api/googleAuth?redirect=" + redirect);
        Logger.getLogger(TAG).info("User not logged in. Sending to Google Auth, URL: " + url);

        // Redirect to that url
        resp.sendRedirect(url);
      }
    } catch (Exception e) {
      e.printStackTrace();
      Logger.getLogger(TAG).error(e.getMessage());
      // Notify of error
      pw.write(APIUtils.generateJSONFailureMessage(e));
    } finally {
      pw.flush();
      pw.close();
    }
  }