View Javadoc
1   package info.ejava.examples.jaxrs.todos.ui;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import javax.ejb.EJB;
8   import javax.servlet.RequestDispatcher;
9   import javax.servlet.ServletException;
10  import javax.servlet.annotation.WebServlet;
11  import javax.servlet.http.HttpServlet;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import info.ejava.examples.jaxrs.todos.dto.TodoItemDTO;
19  import info.ejava.examples.jaxrs.todos.dto.TodoListDTO;
20  import info.ejava.examples.jaxrs.todos.dto.TodoListListDTO;
21  import info.ejava.examples.jaxrs.todos.ejb.ClientErrorException;
22  import info.ejava.examples.jaxrs.todos.ejb.TodosMgmtRemote;
23  
24  @WebServlet(urlPatterns= {"/ui/todo_lists/*"})
25  public class TodoListController extends HttpServlet {
26      private static final long serialVersionUID = 1L;
27      private static final Logger logger = LoggerFactory.getLogger(TodoListController.class);
28      
29      @EJB
30      private TodosMgmtRemote todosMgmt;
31      private Map<String, Action> actions = new HashMap<>();
32      
33      @Override
34      public void init() throws ServletException {
35          actions.put("deleteList", new DeleteTodoListAction());
36          actions.put("setPriority", new SetPriorityAction());        
37          actions.put("deleteItem", new DeleteTodoItemAction());        
38      }
39      
40      private int getInt(String value, int defaultValue) {
41          try {
42              return value==null ? 0 : Integer.parseInt(value);
43          } catch (Exception ex) {
44              return 0;
45          }
46      }
47      
48      private Map<String, String> getPathParams(HttpServletRequest req) {
49          String pathInfo = req.getPathInfo();
50          String[] tokens = pathInfo.split("/");
51          String listName = null;
52          String itemName = null;
53          for (int i=0; i<tokens.length; i++) {
54              if (i==1) {
55                  listName = tokens[i];                
56              } else if ("todo_items".equals(tokens[i]) && i+1<tokens.length) {
57                  itemName = tokens[++i];
58              }
59          }
60          Map<String, String> pathParams = new HashMap<>();
61          if (listName!=null) {
62              pathParams.put("listName", listName);
63              if (itemName!=null) {
64                  pathParams.put("itemName", itemName);
65              } 
66          }
67          return pathParams;
68      }
69      
70      @Override
71      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
72          Map<String, String> pathParams = getPathParams(req);
73          String listName = pathParams.get("listName");
74  
75          try {
76              TodoListDTO todoList = todosMgmt.getTodoList(listName);
77              req.setAttribute("todoList", todoList);
78              RequestDispatcher rd = getServletContext().getRequestDispatcher(
79                      "/WEB-INF/content/DisplayTodoList.jsp");
80              rd.forward(req, resp);
81          } catch (Exception ex) {
82              logger.error("error getting todoList:" + ex);
83              req.setAttribute("exception", ex);
84              RequestDispatcher rd = getServletContext().getRequestDispatcher(
85                  "/WEB-INF/content/DisplayException.jsp");
86              rd.forward(req, resp);
87          }
88      }
89      
90      @Override
91      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
92          Map<String, String> pathParams = getPathParams(req);
93          String listName = pathParams.get("listName");
94          String itemName = pathParams.get("itemName");
95          
96          try {
97              String actionName = req.getParameter("action");
98              if (actionName==null) {
99                  throw new ServletException("no action provided");
100             }
101             
102             Action action = actions.get(actionName);
103             if (action==null) {
104                 throw new ServletException("no action found");
105             }
106             
107             TodoListDTO todoList = todosMgmt.getTodoList(listName);
108             if (todoList==null) {
109                 throw new ServletException("listName not found");                
110             }
111             
112             TodoItemDTO todoItem = itemName!=null ? todoList.getListItem(itemName) : null;
113             if (itemName!=null && todoItem==null) {
114                 throw new ServletException("itemName not found within todoList");
115             }
116             
117             action.execute(req, resp, todoList, todoItem);
118         } catch (Exception ex) {
119             logger.error("error getting todoList:" + ex);
120             req.setAttribute("exception", ex);
121             RequestDispatcher rd = getServletContext().getRequestDispatcher(
122                 "/WEB-INF/content/DisplayException.jsp");
123             rd.forward(req, resp);
124         }
125     }
126 
127     private interface Action {
128         void execute(HttpServletRequest req, HttpServletResponse resp,
129                 TodoListDTO todoList, TodoItemDTO todoItem) 
130                 throws ClientErrorException, ServletException, IOException;
131     }
132     
133     private class DeleteTodoListAction implements Action {
134         @Override
135         public void execute(HttpServletRequest req, HttpServletResponse resp,
136                 TodoListDTO todoList, TodoItemDTO todoItem) 
137                         throws ClientErrorException, ServletException, IOException {
138             todosMgmt.deleteTodoList(todoList.getName());
139             TodoListListDTO todoLists = todosMgmt.getTodoLists(0, 0);
140             
141             req.setAttribute("todoLists", todoLists.getTodoLists());
142             RequestDispatcher rd = getServletContext().getRequestDispatcher(
143                     "/WEB-INF/content/DisplayTodoLists.jsp");
144             rd.forward(req, resp);
145         }        
146     }
147     
148     private class SetPriorityAction implements Action {
149         @Override
150         public void execute(HttpServletRequest req, HttpServletResponse resp,
151                 TodoListDTO todoList, TodoItemDTO todoItem) 
152                         throws ClientErrorException, ServletException, IOException {
153             int priority = getInt(req.getParameter("priority"), 10);
154             todoItem.setPriority(priority);
155             todosMgmt.updateTodoListItem(todoList.getName(), todoItem.getName(), todoItem);
156                 //get list with new sorted order
157             todoList = todosMgmt.getTodoList(todoList.getName());
158             
159             req.setAttribute("todoList", todoList);
160             RequestDispatcher rd = getServletContext().getRequestDispatcher(
161                     "/WEB-INF/content/DisplayTodoList.jsp");
162             rd.forward(req, resp);
163         }
164     }
165     
166     private class DeleteTodoItemAction implements Action {
167         @Override
168         public void execute(HttpServletRequest req, HttpServletResponse resp,
169                 TodoListDTO todoList, TodoItemDTO todoItem) 
170                         throws ClientErrorException, ServletException, IOException {
171             todosMgmt.deleteTodoListItem(todoList.getName(), todoItem.getName());
172                 //get list with new sorted order
173             todoList = todosMgmt.getTodoList(todoList.getName());
174             
175             req.setAttribute("todoList", todoList);
176             RequestDispatcher rd = getServletContext().getRequestDispatcher(
177                     "/WEB-INF/content/DisplayTodoList.jsp");
178             rd.forward(req, resp);
179         }
180     }
181 }