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