1 package ejava.examples.asyncmarket.web;
2
3 import java.io.IOException;
4
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.ejb.EJB;
10 import javax.servlet.RequestDispatcher;
11 import javax.servlet.ServletContext;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import ejava.examples.asyncmarket.Buyer;
21 import ejava.examples.asyncmarket.UserMgmt;
22 import ejava.examples.asyncmarket.bo.AuctionItem;
23 import ejava.examples.asyncmarket.bo.Order;
24 import ejava.examples.asyncmarket.bo.Person;
25 import ejava.examples.asyncmarket.ejb.BuyerLocal;
26 import ejava.examples.asyncmarket.ejb.UserMgmtLocal;
27
28 @SuppressWarnings("serial")
29 public class BuyerHandlerServlet extends HttpServlet {
30 private static Logger log = LoggerFactory.getLogger(BuyerHandlerServlet.class);
31 private Map<String, Handler> handlers = new HashMap<String, Handler>();
32 @EJB(beanInterface=BuyerLocal.class)
33 private Buyer buyer;
34 @EJB(beanInterface=UserMgmtLocal.class)
35 private UserMgmt userMgmt;
36
37 public static final String COMMAND_PARAM = "command";
38 public static final String EXCEPTION_PARAM = "exception";
39 public static final String HANDLER_TYPE_KEY = "type";
40 public static final String ADMIN_TYPE = "admin";
41 public static final String USER_TYPE = "user";
42 public static final String MAINMENU_COMMAND = "menu";
43 public static final String CREATEACCOUNT_COMMAND = "Create Account";
44 public static final String GET_AVAILABLE_ITEMS_COMMAND="Get Available Items";
45 public static final String CREATE_ORDER_COMMAND="Bid";
46 public static final String PLACE_ORDER_COMMAND="Place Order";
47 public static final String GET_ORDER_COMMAND="Get Order";
48 public static final String LOGOUT_COMMAND = "logout";
49 protected static final String DISPLAY_EXCEPTION_URL =
50 "/WEB-INF/content/DisplayException.jsp";
51 private static final String UNKNOWN_COMMAND_URL =
52 "/WEB-INF/content/UnknownCommand.jsp";
53
54 public void init() throws ServletException {
55 log.debug("init() called, buyer=" + buyer + ", userMgmt=" + userMgmt);
56 JNDIHelper jndi = null;
57 try {
58
59 handlers.put(MAINMENU_COMMAND, new AdminMenu());
60 handlers.put(CREATEACCOUNT_COMMAND, new CreateAccount());
61 handlers.put(GET_AVAILABLE_ITEMS_COMMAND, new GetAvailableItems());
62 handlers.put(CREATE_ORDER_COMMAND, new CreateOrder());
63 handlers.put(PLACE_ORDER_COMMAND, new PlaceOrder());
64 handlers.put(GET_ORDER_COMMAND, new GetOrder());
65 handlers.put(LOGOUT_COMMAND, new Logout());
66
67
68 ServletContext ctx = getServletContext();
69
70 if (buyer == null) {
71 buyer = jndi.getBuyer();
72 }
73 if (userMgmt == null) {
74 userMgmt = jndi.getUserMgmt();
75 }
76 }
77 catch (Exception ex) {
78 log.error("error initializing handler", ex);
79 throw new ServletException("error initializing handler", ex);
80 }
81 finally {
82 if (jndi != null) { jndi.close(); }
83 }
84 }
85
86 protected void doGet(HttpServletRequest request,
87 HttpServletResponse response)
88 throws ServletException, IOException {
89 log.debug("doGet() called");
90 String command = request.getParameter(COMMAND_PARAM);
91 log.debug("command=" + command);
92 try {
93 if (command != null) {
94 Handler handler = handlers.get(command);
95 if (handler != null) {
96 log.debug("handler=" + handler);
97 handler.handle(request, response);
98 }
99 else {
100 request.setAttribute("handlers", handlers);
101 RequestDispatcher rd =
102 getServletContext().getRequestDispatcher(
103 UNKNOWN_COMMAND_URL);
104 rd.forward(request, response);
105 }
106 }
107 else {
108 throw new Exception("no " + COMMAND_PARAM + " supplied");
109 }
110 }
111 catch (Exception ex) {
112 request.setAttribute(EXCEPTION_PARAM, ex);
113 RequestDispatcher rd = getServletContext().getRequestDispatcher(
114 UNKNOWN_COMMAND_URL);
115 rd.forward(request, response);
116 }
117 }
118
119 protected void doPost(HttpServletRequest request,
120 HttpServletResponse response)
121 throws ServletException, IOException {
122 log.debug("doPost() called, calling doGet()");
123 doGet(request, response);
124 }
125
126 public void destroy() {
127 log.debug("destroy() called");
128 }
129
130 private abstract class Handler {
131 protected static final String RESULT_PARAM = "result";
132 protected static final String MAINMENU_URL =
133 "/index.jsp";
134 protected static final String DISPLAYUSER_URL =
135 "/WEB-INF/content/DisplayUser.jsp";
136 protected static final String DISPLAYITEM_URL =
137 "/WEB-INF/content/DisplayItem.jsp";
138 protected static final String DISPLAYITEMS_URL =
139 "/WEB-INF/content/DisplayItems.jsp";
140 protected static final String BUYERMENU_URL =
141 "/WEB-INF/content/BuyerMenu.jsp";
142 protected static final String CREATEORDER_URL =
143 "/WEB-INF/content/CreateOrder.jsp";
144 protected static final String DISPLAYORDER_URL =
145 "/WEB-INF/content/DisplayOrder.jsp";
146 protected static final String ITEM = "item";
147 protected static final String USER = "user";
148 public abstract void handle(HttpServletRequest request,
149 HttpServletResponse response)
150 throws ServletException, IOException;
151 }
152
153 private class AdminMenu extends Handler {
154 public void handle(HttpServletRequest request,
155 HttpServletResponse response)
156 throws ServletException, IOException {
157 try {
158 RequestDispatcher rd =
159 getServletContext().getRequestDispatcher(BUYERMENU_URL);
160 rd.forward(request, response);
161 }
162 catch (Exception ex) {
163 request.setAttribute(EXCEPTION_PARAM, ex);
164 RequestDispatcher rd = getServletContext().getRequestDispatcher(
165 DISPLAY_EXCEPTION_URL);
166 rd.forward(request, response);
167 }
168 }
169 }
170
171 private class CreateAccount extends Handler {
172 public void handle(HttpServletRequest request,
173 HttpServletResponse response)
174 throws ServletException, IOException {
175 try {
176 String name = request.getParameter("name");
177 String userId = request.getParameter("userId");
178
179 Person user = new Person();
180 user.setName(name);
181 user.setUserId(userId);
182
183 userMgmt.createUser(userId, name);
184 user = userMgmt.getUserByUserId(userId);
185 request.setAttribute(USER, user);
186
187 RequestDispatcher rd =
188 getServletContext().getRequestDispatcher(DISPLAYUSER_URL);
189 rd.forward(request, response);
190 }
191 catch (Exception ex) {
192 request.setAttribute(EXCEPTION_PARAM, ex);
193 RequestDispatcher rd = getServletContext().getRequestDispatcher(
194 DISPLAY_EXCEPTION_URL);
195 rd.forward(request, response);
196 }
197 }
198 }
199
200 private class GetAvailableItems extends Handler {
201 public void handle(HttpServletRequest request,
202 HttpServletResponse response)
203 throws ServletException, IOException {
204 try {
205 List<AuctionItem> items = buyer.getAvailableItems(0, 100);
206 request.setAttribute("items", items);
207
208 RequestDispatcher rd =
209 getServletContext().getRequestDispatcher(DISPLAYITEMS_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
221 private class CreateOrder extends Handler {
222 public void handle(HttpServletRequest request,
223 HttpServletResponse response)
224 throws ServletException, IOException {
225 try {
226 String itemIdString = request.getParameter("itemId");
227 long itemId = Long.parseLong(itemIdString);
228 AuctionItem item = buyer.getItem(itemId);
229 request.setAttribute("item", item);
230
231 RequestDispatcher rd =
232 getServletContext().getRequestDispatcher(CREATEORDER_URL);
233 rd.forward(request, response);
234 }
235 catch (Exception ex) {
236 request.setAttribute(EXCEPTION_PARAM, ex);
237 RequestDispatcher rd = getServletContext().getRequestDispatcher(
238 DISPLAY_EXCEPTION_URL);
239 rd.forward(request, response);
240 }
241 }
242 }
243
244 private class PlaceOrder extends Handler {
245 public void handle(HttpServletRequest request,
246 HttpServletResponse response)
247 throws ServletException, IOException {
248 try {
249 String itemIdString = request.getParameter("itemId");
250 long itemId = Long.parseLong(itemIdString);
251 String userId = request.getParameter("userId");
252 String amountString = request.getParameter("maxAmount");
253 double maxAmount = Double.parseDouble(amountString);
254
255 log.debug("about to place order");
256 long orderId = buyer.placeOrder(itemId, userId, maxAmount);
257 Order order = buyer.getOrder(orderId);
258 log.debug("order placed, displaying results");
259
260 request.setAttribute("order", order);
261
262 RequestDispatcher rd =
263 getServletContext().getRequestDispatcher(DISPLAYORDER_URL);
264 rd.forward(request, response);
265 }
266 catch (Exception ex) {
267 request.setAttribute(EXCEPTION_PARAM, ex);
268 RequestDispatcher rd = getServletContext().getRequestDispatcher(
269 DISPLAY_EXCEPTION_URL);
270 rd.forward(request, response);
271 }
272 }
273 }
274
275 private class GetOrder extends Handler {
276 public void handle(HttpServletRequest request,
277 HttpServletResponse response)
278 throws ServletException, IOException {
279 try {
280 String orderIdString = request.getParameter("orderId");
281 long orderId = Long.parseLong(orderIdString);
282
283 Order order = buyer.getOrder(orderId);
284
285 request.setAttribute("order", order);
286
287 RequestDispatcher rd =
288 getServletContext().getRequestDispatcher(DISPLAYORDER_URL);
289 rd.forward(request, response);
290 }
291 catch (Exception ex) {
292 request.setAttribute(EXCEPTION_PARAM, ex);
293 RequestDispatcher rd = getServletContext().getRequestDispatcher(
294 DISPLAY_EXCEPTION_URL);
295 rd.forward(request, response);
296 }
297 }
298 }
299
300 private class Logout extends Handler {
301 public void handle(HttpServletRequest request,
302 HttpServletResponse response)
303 throws ServletException, IOException {
304 try {
305 request.getSession().invalidate();
306
307 RequestDispatcher rd =
308 getServletContext().getRequestDispatcher(MAINMENU_URL);
309 rd.forward(request, response);
310 }
311 catch (Exception ex) {
312 request.setAttribute(EXCEPTION_PARAM, ex);
313 RequestDispatcher rd = getServletContext().getRequestDispatcher(
314 DISPLAY_EXCEPTION_URL);
315 rd.forward(request, response);
316 }
317 }
318 }
319 }