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 28 29 public class ViewBidHistory extends HttpServlet 30 { 31 32 private void printError(String errorMsg, ServletPrinter sp) 33 { 34 sp.printHTMLheader("RUBiS ERROR: View Bid History"); 35 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 36 sp.printHTML(errorMsg); 37 sp.printHTMLfooter(); 38 } 39 40 48 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 49 { 50 doPost(request, response); 51 } 52 53 61 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 62 { 63 ServletPrinter sp = null; 64 Context initialContext = null; 65 66 String value = request.getParameter("itemId"); 67 Integer itemId; 68 69 sp = new ServletPrinter(response, "ViewBidHistory"); 70 71 if ((value == null) || (value.equals(""))) 72 { 73 sp.printHTMLheader("RUBiS ERROR: View bids history"); 74 sp.printHTML("<h3>You must provide an item identifier !<br></h3>"); 75 sp.printHTMLfooter(); 76 return ; 77 } 78 else 79 itemId = new Integer (value); 80 81 sp.printHTMLheader("RUBiS: Bid history"); 82 83 try 84 { 85 initialContext = new InitialContext (); 86 } 87 catch (Exception e) 88 { 89 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 90 return ; 91 } 92 93 TopicConnectionFactory topicFactory = null; 94 TopicConnection connection = null; 95 TopicSession session = null; 96 Topic topic = null; 97 String html; 98 try 99 { 100 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 102 connection = topicFactory.createTopicConnection(); 104 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicViewBidHistory"); 106 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 109 catch (Exception e) 110 { 111 sp.printHTML("Cannot connect to message bean MDB_ViewBidHistory : " +e+"<br>"); 112 return ; 113 } 114 try 115 { 116 TopicRequestor requestor = new TopicRequestor (session, topic); 118 MapMessage message = session.createMapMessage(); 120 message.setInt("itemId", itemId.intValue()); 122 message.setJMSCorrelationID("bidHistory"); 123 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 126 connection.stop(); 127 html = reply.getText(); 129 requestor.close(); connection.close(); 132 } 133 catch (Exception e) 134 { 135 sp.printHTML("Cannot get bids history: " +e+"<br>"); 136 return ; 137 } 138 sp.printHTML(html); 139 sp.printHTMLfooter(); 140 } 141 142 } 143 | Popular Tags |