KickJava   Java API By Example, From Geeks To Geeks.

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


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 buy an item
21  * It must be called this way :
22  * <pre>
23  * http://..../BuyNow?itemId=xx&nickname=yy&password=zz
24  * where xx is the id of the item
25  * yy is the nick name of the user
26  * zz is the user password
27  * </pre>
28  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
29  * @version 1.0
30  */

31
32
33 public class BuyNow extends HttpServlet JavaDoc
34 {
35
36   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
37   {
38     sp.printHTMLheader("RUBiS ERROR: Buy Now");
39     sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>");
40     sp.printHTML(errorMsg);
41     sp.printHTMLfooter();
42   }
43
44
45   /**
46    * Authenticate the user and end the display a buy now form
47    *
48    * @param request a <code>HttpServletRequest</code> value
49    * @param response a <code>HttpServletResponse</code> value
50    * @exception IOException if an error occurs
51    * @exception ServletException if an error occurs
52    */

53   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
54   {
55     ServletPrinter sp = null;
56     String JavaDoc itemStr = request.getParameter("itemId");
57     String JavaDoc name = request.getParameter("nickname");
58     String JavaDoc pass = request.getParameter("password");
59     sp = new ServletPrinter(response, "BuyNow");
60
61     if ((itemStr == null) || (itemStr.equals("")) ||
62         (name == null) || (name.equals(""))||
63         (pass == null) || (pass.equals("")))
64     {
65       printError("Item id, name and password are required - Cannot process the request<br>", sp);
66       return ;
67     }
68     Integer JavaDoc itemId = new Integer JavaDoc(itemStr);
69
70     Context JavaDoc initialContext = null;
71     try
72     {
73       initialContext = new InitialContext JavaDoc();
74     }
75     catch (Exception JavaDoc e)
76     {
77       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
78       return ;
79     }
80
81     TopicConnectionFactory JavaDoc topicFactory = null;
82     TopicConnection JavaDoc connection = null;
83     TopicSession JavaDoc session = null;
84     Topic JavaDoc topic = null;
85     String JavaDoc html;
86     try
87     {
88       // lookup the connection factory
89
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
90       // create a connection to the JMS provider
91
connection = topicFactory.createTopicConnection();
92       // lookup the destination
93
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicBuyNow");
94       // create a session
95
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
96
}
97     catch (Exception JavaDoc e)
98     {
99       sp.printHTML("Cannot connect to message bean MDB_BuyNow : " +e+"<br>");
100       return ;
101     }
102     try
103     {
104       sp.printHTMLheader("RUBiS: Buy now");
105       // create a requestor to receive the reply
106
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
107       // create a message
108
MapMessage JavaDoc message = session.createMapMessage();
109       // set parameters
110
message.setInt("itemId", itemId.intValue());
111       message.setString("username", name);
112       message.setString("password", pass);
113       message.setJMSCorrelationID("buyNow");
114       // send the message and receive the reply
115
connection.start(); // allows message to be delivered (default is connection stopped)
116
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
117       connection.stop();
118       // read the reply
119
html = reply.getText();
120       // close connection and session
121
requestor.close(); // also close the session
122
connection.close();
123     }
124     catch (Exception JavaDoc e)
125     {
126       sp.printHTML("Buy now failed: " +e+"<br>");
127       return ;
128     }
129     sp.printHTML(html);
130     sp.printHTMLfooter();
131   }
132
133   /**
134    * Call the doGet method
135    *
136    * @param request a <code>HttpServletRequest</code> value
137    * @param response a <code>HttpServletResponse</code> value
138    * @exception IOException if an error occurs
139    * @exception ServletException if an error occurs
140    */

141   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
142   {
143     doGet(request, response);
144   }
145 }
146
Popular Tags