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.Topic ; 8 import javax.jms.TopicConnection ; 9 import javax.jms.TopicConnectionFactory ; 10 import javax.jms.TopicPublisher ; 11 import javax.jms.TopicSession ; 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.http.HttpServlet ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 35 36 public class StoreComment extends HttpServlet 37 { 38 39 private void printError(String errorMsg, ServletPrinter sp) 40 { 41 sp.printHTMLheader("RUBiS ERROR: StoreComment"); 42 sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>"); 43 sp.printHTML(errorMsg); 44 sp.printHTMLfooter(); 45 } 46 47 48 56 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 57 { 58 doPost(request, response); 59 } 60 61 69 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 70 { 71 ServletPrinter sp = null; 72 Context initialContext = null; 73 74 Integer toId; Integer fromId; Integer itemId; String comment; Integer rating; 80 sp = new ServletPrinter(response, "StoreComment"); 81 82 83 84 String value = request.getParameter("to"); 85 if ((value == null) || (value.equals(""))) 86 { 87 printError("<h3>You must provide a 'to user' identifier !<br></h3>", sp); 88 return ; 89 } 90 else 91 toId = new Integer (value); 92 93 value = request.getParameter("from"); 94 if ((value == null) || (value.equals(""))) 95 { 96 printError("<h3>You must provide a 'from user' identifier !<br></h3>", sp); 97 return ; 98 } 99 else 100 fromId = new Integer (value); 101 102 value = request.getParameter("itemId"); 103 if ((value == null) || (value.equals(""))) 104 { 105 printError("<h3>You must provide an item identifier !<br></h3>", sp); 106 return ; 107 } 108 else 109 itemId = new Integer (value); 110 111 value = request.getParameter("rating"); 112 if ((value == null) || (value.equals(""))) 113 { 114 printError("<h3>You must provide a rating !<br></h3>", sp); 115 return ; 116 } 117 else 118 rating = new Integer (value); 119 120 comment = request.getParameter("comment"); 121 if ((comment == null) || (comment.equals(""))) 122 { 123 printError("<h3>You must provide a comment !<br></h3>", sp); 124 return ; 125 } 126 try 127 { 128 initialContext = new InitialContext (); 129 } 130 catch (Exception e) 131 { 132 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 133 return ; 134 } 135 136 TopicConnectionFactory topicFactory = null; 137 TopicConnection connection = null; 138 TopicSession session = null; 139 Topic topic = null; 140 try 141 { 142 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 144 connection = topicFactory.createTopicConnection(); 146 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicStoreComment"); 148 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 151 catch (Exception e) 152 { 153 sp.printHTML("Cannot connect to message bean MDB_StoreComment : " +e+"<br>"); 154 return ; 155 } 156 try 157 { 158 sp.printHTMLheader("RUBiS: Comment posting"); 159 TopicPublisher sender = session.createPublisher(topic); MapMessage message = session.createMapMessage(); 163 message.setInt("fromId", fromId.intValue()); 165 message.setInt("toId", toId.intValue()); 166 message.setInt("itemId", itemId.intValue()); 167 message.setInt("rating", rating.intValue()); 168 message.setString("comment", comment); 169 connection.start(); sender.publish(message); 172 connection.stop(); 173 sender.close(); 175 session.close(); 176 connection.close(); 177 } 178 catch (Exception e) 179 { 180 printError("Error while storing the comment (got exception: " +e+")<br>", sp); 181 return ; 182 } 183 sp.printHTML("<center><h2>Your comment has been successfully posted.</h2></center>\n"); 184 sp.printHTMLfooter(); 185 186 } 187 188 } 189 | Popular Tags |