KickJava   Java API By Example, From Geeks To Geeks.

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


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 displays the list of bids regarding an item.
21  * It must be called this way :
22  * <pre>
23  * http://..../ViewUserInfo?itemId=xx where xx is the id of the item
24  * </pre>
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.0
27  */

28
29 public class ViewBidHistory extends HttpServlet JavaDoc
30 {
31
32   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
33   {
34     sp.printHTMLheader("RUBiS ERROR: View Bid History");
35     sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>");
36     sp.printHTML(errorMsg);
37     sp.printHTMLfooter();
38   }
39
40   /**
41    * Call the <code>doPost</code> method.
42    *
43    * @param request a <code>HttpServletRequest</code> value
44    * @param response a <code>HttpServletResponse</code> value
45    * @exception IOException if an error occurs
46    * @exception ServletException if an error occurs
47    */

48   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
49   {
50     doPost(request, response);
51   }
52
53   /**
54    * Display the bid history of an item
55    *
56    * @param request a <code>HttpServletRequest</code> value
57    * @param response a <code>HttpServletResponse</code> value
58    * @exception IOException if an error occurs
59    * @exception ServletException if an error occurs
60    */

61   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
62   {
63     ServletPrinter sp = null;
64     Context JavaDoc initialContext = null;
65
66     String JavaDoc value = request.getParameter("itemId");
67     Integer JavaDoc itemId;
68     
69     sp = new ServletPrinter(response, "ViewBidHistory");
70
71     if ((value == null) || (value.equals("")))
72     {
73       sp.printHTMLheader("RUBiS ERROR: View bids history");
74       sp.printHTML("<h3>You must provide an item identifier !<br></h3>");
75       sp.printHTMLfooter();
76       return ;
77     }
78     else
79       itemId = new Integer JavaDoc(value);
80
81     sp.printHTMLheader("RUBiS: Bid history");
82
83     try
84     {
85       initialContext = new InitialContext JavaDoc();
86     }
87     catch (Exception JavaDoc e)
88     {
89       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
90       return ;
91     }
92
93     TopicConnectionFactory JavaDoc topicFactory = null;
94     TopicConnection JavaDoc connection = null;
95     TopicSession JavaDoc session = null;
96     Topic JavaDoc topic = null;
97     String JavaDoc html;
98     try
99     {
100       // lookup the connection factory
101
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
102       // create a connection to the JMS provider
103
connection = topicFactory.createTopicConnection();
104       // lookup the destination
105
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicViewBidHistory");
106       // create a session
107
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
108
}
109     catch (Exception JavaDoc e)
110     {
111       sp.printHTML("Cannot connect to message bean MDB_ViewBidHistory : " +e+"<br>");
112       return ;
113     }
114     try
115     {
116        // create a requestor to receive the reply
117
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
118       // create a message
119
MapMessage JavaDoc message = session.createMapMessage();
120       // set parameters
121
message.setInt("itemId", itemId.intValue());
122       message.setJMSCorrelationID("bidHistory");
123       // send the message and receive the reply
124
connection.start(); // allows message to be delivered (default is connection stopped)
125
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
126       connection.stop();
127       // read the reply
128
html = reply.getText();
129       // close connection and session
130
requestor.close(); // also close the session
131
connection.close();
132     }
133     catch (Exception JavaDoc e)
134     {
135       sp.printHTML("Cannot get bids history: " +e+"<br>");
136       return ;
137     }
138     sp.printHTML(html);
139     sp.printHTMLfooter();
140   }
141
142 }
143
Popular Tags