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"); } }
/** * 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; }
/** * 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(); } }