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 33 34 35 public class PutComment extends HttpServlet 36 { 37 38 private void printError(String errorMsg, ServletPrinter sp) 39 { 40 sp.printHTMLheader("RUBiS ERROR: PutComment"); 41 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 42 sp.printHTML(errorMsg); 43 sp.printHTMLfooter(); 44 } 45 46 47 55 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 56 { 57 ServletPrinter sp = null; 58 String toStr = request.getParameter("to"); 59 String itemStr = request.getParameter("itemId"); 60 String username = request.getParameter("nickname"); 61 String password = request.getParameter("password"); 62 sp = new ServletPrinter(response, "PutComment"); 63 64 if ((toStr == null) || (toStr.equals(""))) 65 { 66 printError("<h3>You must provide a 'to user' identifier !<br></h3>", sp); 67 return ; 68 } 69 if ((itemStr == null) || (itemStr.equals(""))) 70 { 71 printError("<h3>A valid item identifier is required!<br></h3>", sp); 72 return ; 73 } 74 if ((username == null) || (username.equals(""))) 75 { 76 printError("<h3>You must provide a username !<br></h3>", sp); 77 return ; 78 } 79 if ((password == null) || (password.equals(""))) 80 { 81 printError("<h3>You must provide a password !<br></h3>", sp); 82 return ; 83 } 84 85 Integer itemId = new Integer (itemStr); 86 Integer toId = new Integer (toStr); 87 88 Context initialContext = null; 89 try 90 { 91 initialContext = new InitialContext (); 92 } 93 catch (Exception e) 94 { 95 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 96 return ; 97 } 98 TopicConnectionFactory topicFactory = null; 99 TopicConnection connection = null; 100 TopicSession session = null; 101 Topic topic = null; 102 String html; 103 try 104 { 105 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 107 connection = topicFactory.createTopicConnection(); 109 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicPutComment"); 111 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 114 catch (Exception e) 115 { 116 sp.printHTML("Cannot connect to message bean MDB_PutComment : " +e+"<br>"); 117 return ; 118 } 119 120 try 121 { 122 sp.printHTMLheader("RUBiS: Comment service"); 123 TopicRequestor requestor = new TopicRequestor (session, topic); 125 MapMessage message = session.createMapMessage(); 127 message.setInt("itemId", itemId.intValue()); 129 message.setInt("toId", toId.intValue()); 130 message.setString("username", username); 131 message.setString("password", password); 132 message.setJMSCorrelationID("putComment"); 133 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 136 connection.stop(); 137 html = reply.getText(); 139 requestor.close(); connection.close(); 142 } 143 catch (Exception e) 144 { 145 sp.printHTML("Cannot get the html form: " +e+"<br>"); 146 return ; 147 } 148 sp.printHTML(html); 150 sp.printHTML("<tr><td><b>Rating</b>\n"+ 151 "<td><SELECT name=rating>\n"+ 152 "<OPTION value=\"5\">Excellent</OPTION>\n"+ 153 "<OPTION value=\"3\">Average</OPTION>\n"+ 154 "<OPTION selected value=\"0\">Neutral</OPTION>\n"+ 155 "<OPTION value=\"-3\">Below average</OPTION>\n"+ 156 "<OPTION value=\"-5\">Bad</OPTION>\n"+ 157 "</SELECT></table><p><br>\n"+ 158 "<TEXTAREA rows=\"20\" cols=\"80\" name=\"comment\">Write your comment here</TEXTAREA><br><p>\n"+ 159 "<input type=submit value=\"Post this comment now!\"></center><p>\n"); 160 sp.printHTMLfooter(); 161 } 162 163 171 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 172 { 173 doGet(request, response); 174 } 175 } 176 | Popular Tags |