View Javadoc
1   package ejava.examples.asyncmarket.web;
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.http.HttpServlet;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import ejava.examples.asyncmarket.AuctionMgmt;
18  import ejava.examples.asyncmarket.UserMgmt;
19  import ejava.examples.asyncmarket.ejb.AuctionMgmtLocal;
20  import ejava.examples.asyncmarket.ejb.UserMgmtLocal;
21  
22  @SuppressWarnings("serial")
23  public class AuctionAdminHandlerServlet extends HttpServlet {
24      private static Logger logger = LoggerFactory.getLogger(AuctionAdminHandlerServlet.class);
25      private Map<String, Handler> handlers = new HashMap<String, Handler>();
26      @EJB(beanInterface=AuctionMgmtLocal.class)
27      private AuctionMgmt auctionMgmt;
28      @EJB(beanInterface=UserMgmtLocal.class)
29      private UserMgmt userMgmt;
30  
31      public static final String COMMAND_PARAM = "command";
32      public static final String EXCEPTION_PARAM = "exception";
33      public static final String HANDLER_TYPE_KEY = "type";
34      public static final String ADMIN_TYPE = "admin";
35      public static final String USER_TYPE = "user";
36      public static final String MAINMENU_COMMAND = "menu";
37      public static final String CANCELTIMERS_COMMAND = "Cancel Timers";        
38      public static final String INITTIMERS_COMMAND = "Init Timers";        
39      public static final String REMOVEACCOUNT_COMMAND = "Remove Account";        
40      public static final String LOGOUT_COMMAND = "logout";        
41      protected static final String DISPLAY_EXCEPTION_URL = 
42          "/WEB-INF/content/DisplayException.jsp";
43      private static final String UNKNOWN_COMMAND_URL = 
44          "/WEB-INF/content/UnknownCommand.jsp";
45  
46      
47      public void init() throws ServletException {
48          logger.debug("init() called, auctionMgmt=" + auctionMgmt + ", userMgmt=" + userMgmt);
49          try {
50              //build a list of handlers for individual commands
51              handlers.put(MAINMENU_COMMAND, new AdminMenu());
52              handlers.put(CANCELTIMERS_COMMAND, new CancelTimers());
53              handlers.put(INITTIMERS_COMMAND, new InitTimers());
54              handlers.put(REMOVEACCOUNT_COMMAND, new RemoveAccount());
55              handlers.put(LOGOUT_COMMAND, new Logout());
56          }
57          catch (Exception ex) {
58              logger.error("error initializing handler", ex);
59              throw new ServletException("error initializing handler", ex);
60          }
61      }
62  
63      protected void doGet(HttpServletRequest request, 
64                           HttpServletResponse response) 
65          throws ServletException, IOException {
66          logger.debug("doGet() called");
67          String command = request.getParameter(COMMAND_PARAM);
68          logger.debug("command=" + command);
69          try {            
70              if (command != null) {
71                  Handler handler = handlers.get(command);
72                  if (handler != null) {
73                      handler.handle(request, response);
74                  }
75                  else {
76                      request.setAttribute("handlers", handlers);
77                      RequestDispatcher rd = 
78                          getServletContext().getRequestDispatcher(
79                              UNKNOWN_COMMAND_URL);
80                              rd.forward(request, response);
81                  }
82              }
83              else {
84                  throw new Exception("no " + COMMAND_PARAM + " supplied"); 
85              }
86          }
87          catch (Exception ex) {
88              request.setAttribute(EXCEPTION_PARAM, ex);
89              RequestDispatcher rd = getServletContext().getRequestDispatcher(
90                      UNKNOWN_COMMAND_URL);
91                      rd.forward(request, response);
92          }
93      }
94  
95      protected void doPost(HttpServletRequest request, 
96                            HttpServletResponse response) 
97          throws ServletException, IOException {
98          logger.debug("doPost() called, calling doGet()");
99          doGet(request, response);
100     }
101 
102     public void destroy() {
103         logger.debug("destroy() called");
104     }
105     
106     private abstract class Handler {
107         protected static final String RESULT_PARAM = "result";
108         protected static final String MAINMENU_URL = 
109             "/index.jsp";
110         protected static final String ADMINMENU_URL = 
111             "/WEB-INF/content/AdminMenu.jsp";
112         public abstract void handle(HttpServletRequest request, 
113                 HttpServletResponse response) 
114                 throws ServletException, IOException;        
115     }
116     
117     private class AdminMenu extends Handler {
118         public void handle(HttpServletRequest request, 
119                 HttpServletResponse response) 
120                 throws ServletException, IOException {
121             try {
122                 RequestDispatcher rd = 
123                   getServletContext().getRequestDispatcher(ADMINMENU_URL);
124                 rd.forward(request, response);                
125             }
126             catch (Exception ex) {
127                 request.setAttribute(EXCEPTION_PARAM, ex);
128                 RequestDispatcher rd = getServletContext().getRequestDispatcher(
129                     DISPLAY_EXCEPTION_URL);
130                 rd.forward(request, response);
131             }
132         }
133     }
134 
135     private class CancelTimers extends Handler {
136         public void handle(HttpServletRequest request, 
137                 HttpServletResponse response) 
138                 throws ServletException, IOException {
139             try {
140                 auctionMgmt.cancelTimers();
141                 
142                 RequestDispatcher rd = 
143                   getServletContext().getRequestDispatcher(ADMINMENU_URL);
144                 rd.forward(request, response);                
145             }
146             catch (Exception ex) {
147                 request.setAttribute(EXCEPTION_PARAM, ex);
148                 RequestDispatcher rd = getServletContext().getRequestDispatcher(
149                     DISPLAY_EXCEPTION_URL);
150                 rd.forward(request, response);
151             }
152         }
153     }
154 
155     private class InitTimers extends Handler {
156         public void handle(HttpServletRequest request, 
157                 HttpServletResponse response) 
158                 throws ServletException, IOException {
159             try {
160                 String checkIntervalTimerStr = 
161                     request.getParameter("checkIntervalTimer");
162                 long checkIntervalTimer = 
163                     Long.parseLong(checkIntervalTimerStr);
164                 auctionMgmt.initTimers(checkIntervalTimer);
165                 
166                 RequestDispatcher rd = 
167                   getServletContext().getRequestDispatcher(ADMINMENU_URL);
168                 rd.forward(request, response);                
169             }
170             catch (Exception ex) {
171                 request.setAttribute(EXCEPTION_PARAM, ex);
172                 RequestDispatcher rd = getServletContext().getRequestDispatcher(
173                     DISPLAY_EXCEPTION_URL);
174                 rd.forward(request, response);
175             }
176         }
177     }
178 
179     private class RemoveAccount extends Handler {
180         public void handle(HttpServletRequest request, 
181                 HttpServletResponse response) 
182                 throws ServletException, IOException {
183             try {
184                 String userId = request.getParameter("userId");
185 
186                 userMgmt.removeUser(userId);
187                 
188                 RequestDispatcher rd = 
189                   getServletContext().getRequestDispatcher(ADMINMENU_URL);
190                 rd.forward(request, response);                
191             }
192             catch (Exception ex) {
193                 request.setAttribute(EXCEPTION_PARAM, ex);
194                 RequestDispatcher rd = getServletContext().getRequestDispatcher(
195                     DISPLAY_EXCEPTION_URL);
196                 rd.forward(request, response);
197             }
198         }
199     }
200 
201     private class Logout extends Handler {
202         public void handle(HttpServletRequest request, 
203                 HttpServletResponse response) 
204                 throws ServletException, IOException {
205             try {
206                 request.getSession().invalidate();
207                 
208                 RequestDispatcher rd = 
209                   getServletContext().getRequestDispatcher(MAINMENU_URL);
210                 rd.forward(request, response);                
211             }
212             catch (Exception ex) {
213                 request.setAttribute(EXCEPTION_PARAM, ex);
214                 RequestDispatcher rd = getServletContext().getRequestDispatcher(
215                     DISPLAY_EXCEPTION_URL);
216                 rd.forward(request, response);
217             }
218         }
219     }
220 }