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 29 30 31 public class ViewItem extends HttpServlet 32 { 33 34 private void printError(String errorMsg, ServletPrinter sp) 35 { 36 sp.printHTMLheader("RUBiS ERROR: View item"); 37 sp.printHTML("<h2>We cannot process your request due to the following error :</h2><br>"); 38 sp.printHTML(errorMsg); 39 sp.printHTMLfooter(); 40 } 41 42 50 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 51 { 52 ServletPrinter sp = null; 53 sp = new ServletPrinter(response, "ViewItem"); 54 55 String value = request.getParameter("itemId"); 56 if ((value == null) || (value.equals(""))) 57 { 58 printError("No item identifier received - Cannot process the request<br>", sp); 59 return ; 60 } 61 sp.printHTMLheader("RUBiS: Viewing Item \n"); 62 Context initialContext = null; 63 try 64 { 65 initialContext = new InitialContext (); 66 } 67 catch (Exception e) 68 { 69 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 70 return ; 71 } 72 73 TopicConnectionFactory topicFactory = null; 74 TopicConnection connection = null; 75 TopicSession session = null; 76 Topic topic = null; 77 String html; 78 try 79 { 80 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 82 connection = topicFactory.createTopicConnection(); 84 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicViewItem"); 86 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 89 catch (Exception e) 90 { 91 sp.printHTML("Cannot connect to message bean MDB_ViewItem : " +e+"<br>"); 92 return ; 93 } 94 try 95 { 96 Integer itemId = new Integer (value); 97 TopicRequestor requestor = new TopicRequestor (session, topic); 99 MapMessage message = session.createMapMessage(); 101 if (itemId != null) 103 message.setInt("itemId", itemId.intValue()); 104 message.setInt("userId", -1); 105 message.setJMSCorrelationID("viewItem"); 106 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 109 connection.stop(); 110 html = reply.getText(); 112 requestor.close(); connection.close(); 115 } 116 catch (Exception e) 117 { 118 sp.printHTML("Cannot get item description: " +e+"<br>"); 119 return ; 120 } 121 sp.printHTML(html); 122 sp.printHTMLfooter(); 123 124 } 125 126 134 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 135 { 136 doGet(request, response); 137 } 138 } 139 | Popular Tags |