| 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 35 36 public class RegisterUser extends HttpServlet  37 { 38 39 private void printError(String errorMsg, ServletPrinter sp) 40 { 41 sp.printHTMLheader("RUBiS ERROR: Register user"); 42 sp.printHTML("<h2>Your registration has not been processed due to the following error :</h2><br>"); 43 sp.printHTML(errorMsg); 44 sp.printHTMLfooter(); 45 } 46 47 55 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException  56 { 57 ServletPrinter sp = null; 58 String firstname=null, lastname=null, nickname=null, email=null, password=null; 59 int regionId = 0; 60 int userId; 61 String creationDate, regionName; 62 63 sp = new ServletPrinter(response, "RegisterUser"); 64 65 Context initialContext = null; 66 try 67 { 68 initialContext = new InitialContext (); 69 } 70 catch (Exception e) 71 { 72 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 73 return ; 74 } 75 76 String value = request.getParameter("firstname"); 77 if ((value == null) || (value.equals(""))) 78 { 79 printError("You must provide a first name!<br>", sp); 80 return ; 81 } 82 else 83 firstname = value; 84 85 value = request.getParameter("lastname"); 86 if ((value == null) || (value.equals(""))) 87 { 88 printError("You must provide a last name!<br>", sp); 89 return ; 90 } 91 else 92 lastname = value; 93 94 value = request.getParameter("nickname"); 95 if ((value == null) || (value.equals(""))) 96 { 97 printError("You must provide a nick name!<br>", sp); 98 return ; 99 } 100 else 101 nickname = value; 102 103 value = request.getParameter("email"); 104 if ((value == null) || (value.equals(""))) 105 { 106 printError("You must provide an email address!<br>", sp); 107 return ; 108 } 109 else 110 email = value; 111 112 value = request.getParameter("password"); 113 if ((value == null) || (value.equals(""))) 114 { 115 printError("You must provide a password!<br>", sp); 116 return ; 117 } 118 else 119 password = value; 120 121 122 value = request.getParameter("region"); 123 if ((value == null) || (value.equals(""))) 124 { 125 printError("You must provide a valid region!<br>", sp); 126 return ; 127 } 128 else 129 regionName = value; 130 131 TopicConnectionFactory topicFactory = null; 133 TopicConnection connection = null; 134 TopicSession session = null; 135 Topic topic = null; 136 String html; 137 try 138 { 139 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 141 connection = topicFactory.createTopicConnection(); 143 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicRegisterUser"); 145 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 148 catch (Exception e) 149 { 150 sp.printHTML("Cannot connect to message bean MDB_RegisterUser : " +e+"<br>"); 151 return ; 152 } 153 try 154 { 155 TopicRequestor requestor = new TopicRequestor (session, topic); 157 MapMessage message = session.createMapMessage(); 159 message.setString("firstname", firstname); 161 message.setString("lastname", lastname); 162 message.setString("nickname", nickname); 163 message.setString("email", email); 164 message.setString("password", password); 165 message.setString("region", regionName); 166 message.setJMSCorrelationID("registerUser"); 167 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 170 connection.stop(); 171 html = reply.getText(); 173 requestor.close(); connection.close(); 176 } 177 catch (Exception e) 178 { 179 sp.printHTML("User registration failed: " +e+"<br>"); 180 return ; 181 } 182 sp.printHTMLheader("RUBiS: Welcome to "+nickname); 183 184 sp.printHTML("<h2>Your registration has been processed successfully</h2><br>\n"); 185 sp.printHTML("<h3>Welcome "+nickname+"</h3>\n"); 186 sp.printHTML("RUBiS has stored the following information about you:<br>\n"); 187 sp.printHTML("First Name : "+firstname+"<br>\n"); 188 sp.printHTML("Last Name : "+lastname+"<br>\n"); 189 sp.printHTML("Nick Name : "+nickname+"<br>\n"); 190 sp.printHTML("Email : "+email+"<br>\n"); 191 sp.printHTML("Password : "+password+"<br>\n"); 192 sp.printHTML("Region : "+regionName+"<br>\n"); 193 sp.printHTML("<br>The following information has been automatically generated by RUBiS:<br>\n"); 194 sp.printHTML(html); 195 sp.printHTMLfooter(); 196 197 } 198 199 200 208 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException  209 { 210 doGet(request, response); 211 } 212 } 213 | Popular Tags |