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 ViewUserInfo extends HttpServlet 30 { 31 32 private void printError(String errorMsg, ServletPrinter sp) 33 { 34 sp.printHTMLheader("RUBiS ERROR: View user info"); 35 sp.printHTML("<h2>We cannot process your request due to the following error :</h2><br>"); 36 sp.printHTML(errorMsg); 37 sp.printHTMLfooter(); 38 } 39 40 41 49 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 50 { 51 doPost(request, response); 52 } 53 54 62 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 63 { 64 ServletPrinter sp = null; 65 Context initialContext = null; 66 67 String value = request.getParameter("userId"); 68 Integer userId; 69 70 sp = new ServletPrinter(response, "ViewUserInfo"); 71 72 if ((value == null) || (value.equals(""))) 73 { 74 sp.printHTMLheader("RUBiS ERROR: View user information"); 75 sp.printHTML("<h3>You must provide a user identifier !<br></h3>"); 76 sp.printHTMLfooter(); 77 return ; 78 } 79 else 80 userId = new Integer (value); 81 82 sp.printHTMLheader("RUBiS: View user information"); 83 84 try 85 { 86 initialContext = new InitialContext (); 87 } 88 catch (Exception e) 89 { 90 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 91 return ; 92 } 93 94 TopicConnectionFactory topicFactory = null; 95 TopicConnection connection = null; 96 TopicSession session = null; 97 Topic topic = null; 98 String html; 99 try 100 { 101 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 103 connection = topicFactory.createTopicConnection(); 105 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicViewUserInfo"); 107 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 110 catch (Exception e) 111 { 112 sp.printHTML("Cannot connect to message bean MDB_ViewUserInfo : " +e+"<br>"); 113 return ; 114 } 115 try 116 { 117 Integer itemId = new Integer (value); 118 TopicRequestor requestor = new TopicRequestor (session, topic); 120 MapMessage message = session.createMapMessage(); 122 message.setInt("userId", userId.intValue()); 124 message.setJMSCorrelationID("viewUserId"); 125 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 128 connection.stop(); 129 html = reply.getText(); 131 requestor.close(); connection.close(); 134 } 135 catch (Exception e) 136 { 137 sp.printHTML("Cannot get user description: " +e+"<br>"); 138 return ; 139 } 140 sp.printHTML(html); 141 sp.printHTMLfooter(); 142 143 } 144 145 } 146 | Popular Tags |