1 package edu.rice.rubis.beans.servlets; 2 3 import java.io.IOException ; 4 5 import javax.jms.MapMessage ; 6 import javax.jms.Session ; 7 import javax.jms.Topic ; 8 import javax.jms.TopicConnection ; 9 import javax.jms.TopicConnectionFactory ; 10 import javax.jms.TopicPublisher ; 11 import javax.jms.TopicSession ; 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.http.HttpServlet ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 35 36 public class StoreBid extends HttpServlet 37 { 38 39 private void printError(String errorMsg, ServletPrinter sp) 40 { 41 sp.printHTMLheader("RUBiS ERROR: StoreBid"); 42 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 43 sp.printHTML(errorMsg); 44 sp.printHTMLfooter(); 45 } 46 47 48 56 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 57 { 58 doPost(request, response); 59 } 60 61 69 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 70 { 71 ServletPrinter sp = null; 72 Context initialContext = null; 73 Integer userId; Integer itemId; float minBid; float bid; float maxBid; int maxQty; int qty; 81 sp = new ServletPrinter(response, "StoreBid"); 82 83 84 85 String value = request.getParameter("userId"); 86 if ((value == null) || (value.equals(""))) 87 { 88 printError("<h3>You must provide a user identifier !<br></h3>", sp); 89 return ; 90 } 91 else 92 userId = new Integer (value); 93 94 value = request.getParameter("itemId"); 95 if ((value == null) || (value.equals(""))) 96 { 97 printError("<h3>You must provide an item identifier !<br></h3>", sp); 98 return ; 99 } 100 else 101 itemId = new Integer (value); 102 103 value = request.getParameter("minBid"); 104 if ((value == null) || (value.equals(""))) 105 { 106 printError("<h3>You must provide a minimum bid !<br></h3>", sp); 107 return ; 108 } 109 else 110 { 111 Float foo = new Float (value); 112 minBid = foo.floatValue(); 113 } 114 115 value = request.getParameter("bid"); 116 if ((value == null) || (value.equals(""))) 117 { 118 printError("<h3>You must provide a bid !<br></h3>", sp); 119 return ; 120 } 121 else 122 { 123 Float foo = new Float (value); 124 bid = foo.floatValue(); 125 } 126 127 value = request.getParameter("maxBid"); 128 if ((value == null) || (value.equals(""))) 129 { 130 printError("<h3>You must provide a maximum bid !<br></h3>", sp); 131 return ; 132 } 133 else 134 { 135 Float foo = new Float (value); 136 maxBid = foo.floatValue(); 137 } 138 139 value = request.getParameter("maxQty"); 140 if ((value == null) || (value.equals(""))) 141 { 142 printError("<h3>You must provide a maximum quantity !<br></h3>", sp); 143 return ; 144 } 145 else 146 { 147 Integer foo = new Integer (value); 148 maxQty = foo.intValue(); 149 } 150 151 value = request.getParameter("qty"); 152 if ((value == null) || (value.equals(""))) 153 { 154 printError("<h3>You must provide a quantity !<br></h3>", sp); 155 return ; 156 } 157 else 158 { 159 Integer foo = new Integer (value); 160 qty = foo.intValue(); 161 } 162 163 164 165 if (qty > maxQty) 166 { 167 printError("<h3>You cannot request "+qty+" items because only "+maxQty+" are proposed !<br></h3>", sp); 168 return ; 169 } 170 if (bid < minBid) 171 { 172 printError("<h3>Your bid of $"+bid+" is not acceptable because it is below the $"+minBid+" minimum bid !<br></h3>", sp); 173 return ; 174 } 175 if (maxBid < minBid) 176 { 177 printError("<h3>Your maximum bid of $"+maxBid+" is not acceptable because it is below the $"+minBid+" minimum bid !<br></h3>", sp); 178 return ; 179 } 180 if (maxBid < bid) 181 { 182 printError("<h3>Your maximum bid of $"+maxBid+" is not acceptable because it is below your current bid of $"+bid+" !<br></h3>", sp); 183 return ; 184 } 185 186 try 187 { 188 initialContext = new InitialContext (); 189 } 190 catch (Exception e) 191 { 192 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 193 return ; 194 } 195 196 TopicConnectionFactory topicFactory = null; 197 TopicConnection connection = null; 198 TopicSession session = null; 199 Topic topic = null; 200 try 201 { 202 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 204 connection = topicFactory.createTopicConnection(); 206 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicStoreBid"); 208 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 211 catch (Exception e) 212 { 213 sp.printHTML("Cannot connect to message bean MDB_StoreBid : " +e+"<br>"); 214 return ; 215 } 216 try 217 { 218 sp.printHTMLheader("RUBiS: Bidding result"); 219 TopicPublisher sender = session.createPublisher(topic); MapMessage message = session.createMapMessage(); 223 message.setInt("userId", userId.intValue()); 225 message.setInt("itemId", itemId.intValue()); 226 message.setFloat("bid", bid); 227 message.setFloat("maxBid", maxBid); 228 message.setInt("quantity", qty); 229 connection.start(); sender.publish(message); 232 connection.stop(); 233 sender.close(); 235 session.close(); 236 connection.close(); 237 } 238 catch (Exception e) 239 { 240 printError("Error while storing the bid (got exception: " +e+")<br>", sp); 241 return ; 242 } 243 sp.printHTML("<center><h2>Your bid has been successfully processed.</h2></center>\n"); 244 sp.printHTMLfooter(); 245 } 246 247 } 248 | Popular Tags |