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 StoreBuyNow extends HttpServlet 37 { 38 39 private void printError(String errorMsg, ServletPrinter sp) 40 { 41 sp.printHTMLheader("RUBiS ERROR: StoreBuyNow"); 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 74 Integer userId; Integer itemId; float minBuyNow; float BuyNow; float maxBuyNow; int maxQty; int qty; 82 sp = new ServletPrinter(response, "StoreBuyNow"); 83 84 85 86 String value = request.getParameter("userId"); 87 if ((value == null) || (value.equals(""))) 88 { 89 printError("<h3>You must provide a user identifier !<br></h3>", sp); 90 return ; 91 } 92 else 93 userId = new Integer (value); 94 95 value = request.getParameter("itemId"); 96 if ((value == null) || (value.equals(""))) 97 { 98 printError("<h3>You must provide an item identifier !<br></h3>", sp); 99 return ; 100 } 101 else 102 itemId = new Integer (value); 103 104 105 value = request.getParameter("maxQty"); 106 if ((value == null) || (value.equals(""))) 107 { 108 printError("<h3>You must provide a maximum quantity !<br></h3>", sp); 109 return ; 110 } 111 else 112 { 113 Integer foo = new Integer (value); 114 maxQty = foo.intValue(); 115 } 116 117 value = request.getParameter("qty"); 118 if ((value == null) || (value.equals(""))) 119 { 120 printError("<h3>You must provide a quantity !<br></h3>", sp); 121 return ; 122 } 123 else 124 { 125 Integer foo = new Integer (value); 126 qty = foo.intValue(); 127 } 128 129 130 131 if (qty > maxQty) 132 { 133 printError("<h3>You cannot request "+qty+" items because only "+maxQty+" are proposed !<br></h3>", sp); 134 return ; 135 } 136 137 try 138 { 139 initialContext = new InitialContext (); 140 } 141 catch (Exception e) 142 { 143 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 144 return ; 145 } 146 147 TopicConnectionFactory topicFactory = null; 148 TopicConnection connection = null; 149 TopicSession session = null; 150 Topic topic = null; 151 try 152 { 153 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 155 connection = topicFactory.createTopicConnection(); 157 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicStoreBuyNow"); 159 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 162 catch (Exception e) 163 { 164 sp.printHTML("Cannot connect to message bean MDB_StoreBuyNow : " +e+"<br>"); 165 return ; 166 } 167 try 168 { 169 sp.printHTMLheader("RUBiS: BuyNow result"); 170 TopicPublisher sender = session.createPublisher(topic); MapMessage message = session.createMapMessage(); 174 message.setInt("itemId", itemId.intValue()); 176 message.setInt("userId", userId.intValue()); 177 message.setInt("quantity", qty); 178 connection.start(); sender.publish(message); 181 connection.stop(); 182 sender.close(); 184 session.close(); 185 connection.close(); 186 } 187 catch (Exception e) 188 { 189 printError("Error while storing the buyNow (got exception: " +e+")<br>", sp); 190 return ; 191 } 192 if (qty == 1) 193 sp.printHTML("<center><h2>Your have successfully bought this item.</h2></center>\n"); 194 else 195 sp.printHTML("<center><h2>Your have successfully bought these items.</h2></center>\n"); 196 sp.printHTMLfooter(); 197 } 198 199 } 200 | Popular Tags |