KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > servlets > PutComment


1 package edu.rice.rubis.beans.servlets;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.jms.MapMessage JavaDoc;
6 import javax.jms.Session JavaDoc;
7 import javax.jms.TextMessage JavaDoc;
8 import javax.jms.Topic JavaDoc;
9 import javax.jms.TopicConnection JavaDoc;
10 import javax.jms.TopicConnectionFactory JavaDoc;
11 import javax.jms.TopicRequestor JavaDoc;
12 import javax.jms.TopicSession JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 /** This servlets display the page allowing a user to put a comment
21  * on an item.
22  * It must be called this way :
23  * <pre>
24  * http://..../PutComment?to=ww&itemId=xx&nickname=yy&password=zz
25  * where ww is the id of the user that will receive the comment
26  * xx is the item id
27  * yy is the nick name of the user
28  * zz is the user password
29  * </pre>
30  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
31  * @version 1.0
32  */

33
34
35 public class PutComment extends HttpServlet JavaDoc
36 {
37
38   private void printError(String JavaDoc 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   /**
48    * Describe <code>doGet</code> method here.
49    *
50    * @param request a <code>HttpServletRequest</code> value
51    * @param response a <code>HttpServletResponse</code> value
52    * @exception IOException if an error occurs
53    * @exception ServletException if an error occurs
54    */

55   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
56   {
57     ServletPrinter sp = null;
58     String JavaDoc toStr = request.getParameter("to");
59     String JavaDoc itemStr = request.getParameter("itemId");
60     String JavaDoc username = request.getParameter("nickname");
61     String JavaDoc 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 JavaDoc itemId = new Integer JavaDoc(itemStr);
86     Integer JavaDoc toId = new Integer JavaDoc(toStr);
87
88     Context JavaDoc initialContext = null;
89     try
90     {
91       initialContext = new InitialContext JavaDoc();
92     }
93     catch (Exception JavaDoc e)
94     {
95       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
96       return ;
97     }
98     TopicConnectionFactory JavaDoc topicFactory = null;
99     TopicConnection JavaDoc connection = null;
100     TopicSession JavaDoc session = null;
101     Topic JavaDoc topic = null;
102     String JavaDoc html;
103     try
104     {
105       // lookup the connection factory
106
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
107       // create a connection to the JMS provider
108
connection = topicFactory.createTopicConnection();
109       // lookup the destination
110
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicPutComment");
111       // create a session
112
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
113
}
114     catch (Exception JavaDoc 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       // create a requestor to receive the reply
124
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
125       // create a message
126
MapMessage JavaDoc message = session.createMapMessage();
127       // set parameters
128
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       // send the message and receive the reply
134
connection.start(); // allows message to be delivered (default is connection stopped)
135
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
136       connection.stop();
137       // read the reply
138
html = reply.getText();
139       // close connection and session
140
requestor.close(); // also close the session
141
connection.close();
142     }
143     catch (Exception JavaDoc e)
144     {
145       sp.printHTML("Cannot get the html form: " +e+"<br>");
146       return ;
147     }
148     // Display the comment form
149
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   /**
164    * Call the <code>doGet</code> method.
165    *
166    * @param request a <code>HttpServletRequest</code> value
167    * @param response a <code>HttpServletResponse</code> value
168    * @exception IOException if an error occurs
169    * @exception ServletException if an error occurs
170    */

171   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
172   {
173     doGet(request, response);
174   }
175 }
176
Popular Tags