1 package ejava.examples.asyncmarket.web;
2
3 import java.io.IOException;
4 import java.util.Calendar;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.ejb.EJB;
9 import javax.servlet.RequestDispatcher;
10 import javax.servlet.ServletContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import ejava.examples.asyncmarket.Seller;
20 import ejava.examples.asyncmarket.UserMgmt;
21 import ejava.examples.asyncmarket.bo.AuctionItem;
22 import ejava.examples.asyncmarket.bo.Person;
23 import ejava.examples.asyncmarket.ejb.SellerLocal;
24 import ejava.examples.asyncmarket.ejb.UserMgmtLocal;
25
26 @SuppressWarnings("serial")
27 public class SellerHandlerServlet extends HttpServlet {
28 private static Log log = LogFactory.getLog(SellerHandlerServlet.class);
29 private Map<String, Handler> handlers = new HashMap<String, Handler>();
30 @EJB(beanInterface=SellerLocal.class)
31 private Seller seller;
32 @EJB(beanInterface=UserMgmtLocal.class)
33 private UserMgmt userMgmt;
34
35 public static final String COMMAND_PARAM = "command";
36 public static final String EXCEPTION_PARAM = "exception";
37 public static final String HANDLER_TYPE_KEY = "type";
38 public static final String ADMIN_TYPE = "admin";
39 public static final String USER_TYPE = "user";
40 public static final String MAINMENU_COMMAND = "menu";
41 public static final String CREATEACCOUNT_COMMAND = "Create Account";
42 public static final String SELLPRODUCT_COMMAND = "Sell Product";
43 public static final String GETITEMS_COMMAND = "Get Items";
44 public static final String LOGOUT_COMMAND = "logout";
45 protected static final String DISPLAY_EXCEPTION_URL =
46 "/WEB-INF/content/DisplayException.jsp";
47 private static final String UNKNOWN_COMMAND_URL =
48 "/WEB-INF/content/UnknownCommand.jsp";
49
50 public void init() throws ServletException {
51 log.debug("init() called, seller=" + seller + ", userMgmt=" + userMgmt);
52 JNDIHelper jndi = null;;
53 try {
54
55 handlers.put(MAINMENU_COMMAND, new AdminMenu());
56 handlers.put(CREATEACCOUNT_COMMAND, new CreateAccount());
57 handlers.put(SELLPRODUCT_COMMAND, new SellProduct());
58 handlers.put(GETITEMS_COMMAND, new GetItems());
59 handlers.put(LOGOUT_COMMAND, new Logout());
60
61
62 ServletContext ctx = getServletContext();
63
64 if (seller == null) {
65 seller = jndi.getSeller();
66 }
67 if (userMgmt == null) {
68 userMgmt = jndi.getUserMgmt();
69 }
70 }
71 catch (Exception ex) {
72 log.fatal("error initializing handler", ex);
73 throw new ServletException("error initializing handler", ex);
74 }
75 finally {
76 if (jndi != null) {
77 jndi.close();
78 }
79 }
80 }
81
82 protected void doGet(HttpServletRequest request,
83 HttpServletResponse response)
84 throws ServletException, IOException {
85 log.debug("doGet() called");
86 String command = request.getParameter(COMMAND_PARAM);
87 log.debug("command=" + command);
88 try {
89 if (command != null) {
90 Handler handler = handlers.get(command);
91 if (handler != null) {
92 log.debug("handler=" + handler);
93 handler.handle(request, response);
94 }
95 else {
96 request.setAttribute("handlers", handlers);
97 RequestDispatcher rd =
98 getServletContext().getRequestDispatcher(
99 UNKNOWN_COMMAND_URL);
100 rd.forward(request, response);
101 }
102 }
103 else {
104 throw new Exception("no " + COMMAND_PARAM + " supplied");
105 }
106 }
107 catch (Exception ex) {
108 request.setAttribute(EXCEPTION_PARAM, ex);
109 RequestDispatcher rd = getServletContext().getRequestDispatcher(
110 UNKNOWN_COMMAND_URL);
111 rd.forward(request, response);
112 }
113 }
114
115 protected void doPost(HttpServletRequest request,
116 HttpServletResponse response)
117 throws ServletException, IOException {
118 log.debug("doPost() called, calling doGet()");
119 doGet(request, response);
120 }
121
122 public void destroy() {
123 log.debug("destroy() called");
124 }
125
126 private abstract class Handler {
127 protected static final String RESULT_PARAM = "result";
128 protected static final String MAINMENU_URL =
129 "/index.jsp";
130 protected static final String DISPLAYUSER_URL =
131 "/WEB-INF/content/DisplayUser.jsp";
132 protected static final String DISPLAYITEM_URL =
133 "/WEB-INF/content/DisplayItem.jsp";
134 protected static final String SELLERMENU_URL =
135 "/WEB-INF/content/SellerMenu.jsp";
136 protected static final String ITEM = "item";
137 protected static final String USER = "user";
138 public abstract void handle(HttpServletRequest request,
139 HttpServletResponse response)
140 throws ServletException, IOException;
141 }
142
143 private class AdminMenu extends Handler {
144 public void handle(HttpServletRequest request,
145 HttpServletResponse response)
146 throws ServletException, IOException {
147 try {
148 RequestDispatcher rd =
149 getServletContext().getRequestDispatcher(SELLERMENU_URL);
150 rd.forward(request, response);
151 }
152 catch (Exception ex) {
153 request.setAttribute(EXCEPTION_PARAM, ex);
154 RequestDispatcher rd = getServletContext().getRequestDispatcher(
155 DISPLAY_EXCEPTION_URL);
156 rd.forward(request, response);
157 }
158 }
159 }
160
161 private class CreateAccount extends Handler {
162 public void handle(HttpServletRequest request,
163 HttpServletResponse response)
164 throws ServletException, IOException {
165 try {
166 String name = request.getParameter("name");
167 String userId = request.getParameter("userId");
168
169 Person user = new Person();
170 user.setName(name);
171 user.setUserId(userId);
172
173 userMgmt.createUser(userId, name);
174 user = userMgmt.getUserByUserId(userId);
175 request.setAttribute(USER, user);
176
177 RequestDispatcher rd =
178 getServletContext().getRequestDispatcher(DISPLAYUSER_URL);
179 rd.forward(request, response);
180 }
181 catch (Exception ex) {
182 request.setAttribute(EXCEPTION_PARAM, ex);
183 RequestDispatcher rd = getServletContext().getRequestDispatcher(
184 DISPLAY_EXCEPTION_URL);
185 rd.forward(request, response);
186 }
187 }
188 }
189
190 private class SellProduct extends Handler {
191 public void handle(HttpServletRequest request,
192 HttpServletResponse response)
193 throws ServletException, IOException {
194 try {
195 log.debug("in sell product");
196 String name = request.getParameter("name");
197 String delayString = request.getParameter("delay");
198 int delay = Integer.parseInt(delayString);
199 String minBidString = request.getParameter("midBid");
200 double minBid = Double.parseDouble(minBidString);
201 String sellerId = request.getParameter("userId");
202
203 AuctionItem item = new AuctionItem();
204 item.setName(name);
205 item.setMinBid(minBid);
206 Calendar cal = Calendar.getInstance();
207 item.setStartDate(cal.getTime());
208 cal.add(Calendar.SECOND, delay);
209 item.setEndDate(cal.getTime());
210
211 log.debug("calling EJB");
212 long itemId = seller.sellProduct(sellerId, item);
213 item = seller.getItem(itemId);
214 request.setAttribute(ITEM, item);
215 log.debug("about to forward");
216
217 RequestDispatcher rd =
218 getServletContext().getRequestDispatcher(DISPLAYITEM_URL);
219 rd.forward(request, response);
220 }
221 catch (Exception ex) {
222 request.setAttribute(EXCEPTION_PARAM, ex);
223 RequestDispatcher rd = getServletContext().getRequestDispatcher(
224 DISPLAY_EXCEPTION_URL);
225 rd.forward(request, response);
226 }
227 }
228 }
229
230 private class GetItems extends Handler {
231 public void handle(HttpServletRequest request,
232 HttpServletResponse response)
233 throws ServletException, IOException {
234 try {
235 String userId = request.getParameter("userId");
236
237 Person user = userMgmt.getUserByUserId(userId);
238
239 request.setAttribute(USER, user);
240
241 RequestDispatcher rd =
242 getServletContext().getRequestDispatcher(DISPLAYUSER_URL);
243 rd.forward(request, response);
244 }
245 catch (Exception ex) {
246 request.setAttribute(EXCEPTION_PARAM, ex);
247 RequestDispatcher rd = getServletContext().getRequestDispatcher(
248 DISPLAY_EXCEPTION_URL);
249 rd.forward(request, response);
250 }
251 }
252 }
253
254 private class Logout extends Handler {
255 public void handle(HttpServletRequest request,
256 HttpServletResponse response)
257 throws ServletException, IOException {
258 try {
259 request.getSession().invalidate();
260
261 RequestDispatcher rd =
262 getServletContext().getRequestDispatcher(MAINMENU_URL);
263 rd.forward(request, response);
264 }
265 catch (Exception ex) {
266 request.setAttribute(EXCEPTION_PARAM, ex);
267 RequestDispatcher rd = getServletContext().getRequestDispatcher(
268 DISPLAY_EXCEPTION_URL);
269 rd.forward(request, response);
270 }
271 }
272 }
273 }