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 32 33 34 public class PutBid extends HttpServlet 35 { 36 37 private void printError(String errorMsg, ServletPrinter sp) 38 { 39 sp.printHTMLheader("RUBiS ERROR: PutBid"); 40 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 41 sp.printHTML(errorMsg); 42 sp.printHTMLfooter(); 43 } 44 45 46 54 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 55 { 56 ServletPrinter sp = null; 57 String itemStr = request.getParameter("itemId"); 58 String name = request.getParameter("nickname"); 59 String pass = request.getParameter("password"); 60 sp = new ServletPrinter(response, "PubBid"); 61 62 if ((itemStr == null) || (itemStr.equals("")) || 63 (name == null) || (name.equals(""))|| 64 (pass == null) || (pass.equals(""))) 65 { 66 printError("Item id, name and password are required - Cannot process the request<br>", sp); 67 return ; 68 } 69 Integer itemId = new Integer (itemStr); 70 71 Context initialContext = null; 72 try 73 { 74 initialContext = new InitialContext (); 75 } 76 catch (Exception e) 77 { 78 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 79 return ; 80 } 81 82 TopicConnectionFactory topicFactory = null; 83 TopicConnection connection = null; 84 TopicSession session = null; 85 Topic topic = null; 86 String html; 87 try 88 { 89 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 91 connection = topicFactory.createTopicConnection(); 93 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicPutBid"); 95 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 98 catch (Exception e) 99 { 100 sp.printHTML("Cannot connect to message bean MDB_PutBid : " +e+"<br>"); 101 return ; 102 } 103 try 104 { 105 sp.printHTMLheader("RUBiS: Bidding service"); 106 TopicRequestor requestor = new TopicRequestor (session, topic); 108 MapMessage message = session.createMapMessage(); 110 message.setInt("itemId", itemId.intValue()); 112 message.setString("username", name); 113 message.setString("password", pass); 114 message.setJMSCorrelationID("putBid"); 115 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 118 connection.stop(); 119 html = reply.getText(); 121 requestor.close(); connection.close(); 124 } 125 catch (Exception e) 126 { 127 sp.printHTML("Bidding process failed: " +e+"<br>"); 128 return ; 129 } 130 sp.printHTML(html); 132 sp.printHTMLfooter(); 133 134 } 135 136 144 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 145 { 146 doGet(request, response); 147 } 148 } 149 | Popular Tags |