Commits

Samuel Le Berrigaud committed 34894bb

Updating stage 5 & 6:
- Refactoring the enforce logged in method,
- Enforcing on POST as well.

  • Participants
  • Parent commits 99b4813

Comments (0)

Files changed (2)

File ao-tutorial-stage5/src/main/java/com/atlassian/tutorial/ao/todo/TodoServlet.java

     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
     {
-        // is the user is not connected, redirect to the login page
-        if (userManager.getRemoteUsername() == null)
-        {
-            res.sendRedirect(req.getContextPath() + "/plugins/servlet/login");
-        }
+        enforceLoggedIn(req, res);
 
         final PrintWriter w = res.getWriter();
         w.printf("<h1>Todos (%s)</h1>", userManager.getRemoteUsername());
 
         w.write("<ol>");
 
-        for (Todo todo : todoService.all()) // (2)
+        for (Todo todo : todoService.all())
         {
             w.printf("<li><%2$s> %s </%2$s></li>", todo.getDescription(), todo.isComplete() ? "strike" : "strong");
         }
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
     {
+        enforceLoggedIn(req, res);
+
         final String description = req.getParameter("task");
         todoService.add(description);
         res.sendRedirect(req.getContextPath() + "/plugins/servlet/todo/list");
     }
+
+    private void enforceLoggedIn(HttpServletRequest req, HttpServletResponse res) throws IOException
+    {
+        if (userManager.getRemoteUsername() == null)
+        {
+            res.sendRedirect(req.getContextPath() + "/plugins/servlet/login");
+        }
+    }
 }

File ao-tutorial-stage6/src/main/java/com/atlassian/tutorial/ao/todo/TodoServlet.java

     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
     {
-        // is the user is not connected, redirect to the login page
-        if (userManager.getRemoteUsername() == null)
-        {
-            res.sendRedirect(req.getContextPath() + "/plugins/servlet/login");
-        }
+        enforceLoggedIn(req, res);
 
         final PrintWriter w = res.getWriter();
         w.printf("<h1>Todos (%s)</h1>", userManager.getRemoteUsername());
 
         w.write("<ol>");
 
-        for (Todo todo : todoService.all()) // (2)
+        for (Todo todo : todoService.all())
         {
             w.printf("<li><%2$s> %s </%2$s></li>", todo.getDescription(), todo.isComplete() ? "strike" : "strong");
         }
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
     {
+        enforceLoggedIn(req, res);
+
         final String description = req.getParameter("task");
         todoService.add(description);
         res.sendRedirect(req.getContextPath() + "/plugins/servlet/todo/list");
     }
+
+    private void enforceLoggedIn(HttpServletRequest req, HttpServletResponse res) throws IOException
+    {
+        if (userManager.getRemoteUsername() == null)
+        {
+            res.sendRedirect(req.getContextPath() + "/plugins/servlet/login");
+        }
+    }
 }