KickJava   Java API By Example, From Geeks To Geeks.

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


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.Topic JavaDoc;
8 import javax.jms.TopicConnection JavaDoc;
9 import javax.jms.TopicConnectionFactory JavaDoc;
10 import javax.jms.TopicPublisher JavaDoc;
11 import javax.jms.TopicSession JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 /** This servlets records a comment in the database and display
20  * the result of the transaction.
21  * It must be called this way :
22  * <pre>
23  * http://..../StoreComment?itemId=aa&userId=bb&minComment=cc&maxQty=dd&comment=ee&maxComment=ff&qty=gg
24  * where: aa is the item id
25  * bb is the user id
26  * cc is the minimum acceptable comment for this item
27  * dd is the maximum quantity available for this item
28  * ee is the user comment
29  * ff is the maximum comment the user wants
30  * gg is the quantity asked by the user
31  * </pre>
32  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
33  * @version 1.0
34  */

35
36 public class StoreComment extends HttpServlet JavaDoc
37 {
38
39   private void printError(String JavaDoc 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   /**
49    * Call the <code>doPost</code> method.
50    *
51    * @param request a <code>HttpServletRequest</code> value
52    * @param response a <code>HttpServletResponse</code> value
53    * @exception IOException if an error occurs
54    * @exception ServletException if an error occurs
55    */

56   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
57   {
58     doPost(request, response);
59   }
60
61   /**
62    * Store the comment to the database and display the resulting message.
63    *
64    * @param request a <code>HttpServletRequest</code> value
65    * @param response a <code>HttpServletResponse</code> value
66    * @exception IOException if an error occurs
67    * @exception ServletException if an error occurs
68    */

69   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
70   {
71     ServletPrinter sp = null;
72     Context JavaDoc initialContext = null;
73
74     Integer JavaDoc toId; // to user id
75
Integer JavaDoc fromId; // from user id
76
Integer JavaDoc itemId; // item id
77
String JavaDoc comment; // user comment
78
Integer JavaDoc rating; // user rating
79

80     sp = new ServletPrinter(response, "StoreComment");
81
82     /* Get and check all parameters */
83
84     String JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc();
129     }
130     catch (Exception JavaDoc e)
131     {
132       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
133       return ;
134     }
135
136     TopicConnectionFactory JavaDoc topicFactory = null;
137     TopicConnection JavaDoc connection = null;
138     TopicSession JavaDoc session = null;
139     Topic JavaDoc topic = null;
140     try
141     {
142       // lookup the connection factory
143
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
144       // create a connection to the JMS provider
145
connection = topicFactory.createTopicConnection();
146       // lookup the destination
147
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicStoreComment");
148       // create a session
149
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
150
}
151     catch (Exception JavaDoc 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       // create a sender
160
TopicPublisher JavaDoc sender = session.createPublisher(topic); // identified publisher
161
// create a message
162
MapMessage JavaDoc message = session.createMapMessage();
163       // set parameters
164
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       // send the message
170
connection.start(); // allows message to be delivered (default is connection stopped)
171
sender.publish(message);
172       connection.stop();
173       // close connection and session
174
sender.close();
175       session.close();
176       connection.close();
177     }
178     catch (Exception JavaDoc 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