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.TextMessage ; 8 import javax.jms.Topic ; 9 import javax.jms.TopicConnection ; 10 import javax.jms.TopicConnectionFactory ; 11 import javax.jms.TopicRequestor ; 12 import javax.jms.TopicSession ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 31 32 33 public class BuyNow extends HttpServlet 34 { 35 36 private void printError(String errorMsg, ServletPrinter sp) 37 { 38 sp.printHTMLheader("RUBiS ERROR: Buy Now"); 39 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 40 sp.printHTML(errorMsg); 41 sp.printHTMLfooter(); 42 } 43 44 45 53 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 54 { 55 ServletPrinter sp = null; 56 String itemStr = request.getParameter("itemId"); 57 String name = request.getParameter("nickname"); 58 String pass = request.getParameter("password"); 59 sp = new ServletPrinter(response, "BuyNow"); 60 61 if ((itemStr == null) || (itemStr.equals("")) || 62 (name == null) || (name.equals(""))|| 63 (pass == null) || (pass.equals(""))) 64 { 65 printError("Item id, name and password are required - Cannot process the request<br>", sp); 66 return ; 67 } 68 Integer itemId = new Integer (itemStr); 69 70 Context initialContext = null; 71 try 72 { 73 initialContext = new InitialContext (); 74 } 75 catch (Exception e) 76 { 77 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 78 return ; 79 } 80 81 TopicConnectionFactory topicFactory = null; 82 TopicConnection connection = null; 83 TopicSession session = null; 84 Topic topic = null; 85 String html; 86 try 87 { 88 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 90 connection = topicFactory.createTopicConnection(); 92 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicBuyNow"); 94 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 97 catch (Exception e) 98 { 99 sp.printHTML("Cannot connect to message bean MDB_BuyNow : " +e+"<br>"); 100 return ; 101 } 102 try 103 { 104 sp.printHTMLheader("RUBiS: Buy now"); 105 TopicRequestor requestor = new TopicRequestor (session, topic); 107 MapMessage message = session.createMapMessage(); 109 message.setInt("itemId", itemId.intValue()); 111 message.setString("username", name); 112 message.setString("password", pass); 113 message.setJMSCorrelationID("buyNow"); 114 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 117 connection.stop(); 118 html = reply.getText(); 120 requestor.close(); connection.close(); 123 } 124 catch (Exception e) 125 { 126 sp.printHTML("Buy now failed: " +e+"<br>"); 127 return ; 128 } 129 sp.printHTML(html); 130 sp.printHTMLfooter(); 131 } 132 133 141 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 142 { 143 doGet(request, response); 144 } 145 } 146 | Popular Tags |