KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
21  * Builds the html page with the list of all categories and provides links to browse all
22  * items in a category or items in a category for a given region
23  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
24  * @version 1.0
25  */

26 public class BrowseCategories extends HttpServlet JavaDoc
27 {
28
29   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
30   {
31     sp.printHTMLheader("RUBiS ERROR: Browse Categories");
32     sp.printHTML("<h3>Your request has not been processed due to the following error :</h3><br>");
33     sp.printHTML(errorMsg);
34     sp.printHTMLfooter();
35   }
36
37   /**
38    * Build the html page for the response
39    * @param request a <code>HttpServletRequest</code> value
40    * @param response a <code>HttpServletResponse</code> value
41    * @exception IOException if an error occurs
42    * @exception ServletException if an error occurs
43    */

44   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
45   {
46     ServletPrinter sp = null;
47     String JavaDoc region=null;
48     String JavaDoc username=null, password=null;
49     Context JavaDoc initialContext = null;
50
51     sp = new ServletPrinter(response, "BrowseCategories");
52     sp.printHTMLheader("RUBiS available categories");
53     sp.printHTML("<h2>Currently available categories</h2><br>");
54     try
55     {
56       initialContext = new InitialContext JavaDoc();
57     }
58     catch (Exception JavaDoc e)
59     {
60       printError("Cannot get initial context for JNDI: " +e+"<br>", sp);
61       return ;
62     }
63
64     region = request.getParameter("region");
65     username = request.getParameter("nickname");
66     password = request.getParameter("password");
67
68     TopicConnectionFactory JavaDoc topicFactory = null;
69     TopicConnection JavaDoc connection = null;
70     TopicSession JavaDoc session = null;
71     Topic JavaDoc topic = null;
72     String JavaDoc html;
73     try
74     {
75       // lookup the connection factory
76
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
77        // create a connection to the JMS provider
78
connection = topicFactory.createTopicConnection();
79        // lookup the destination
80
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicBrowseCategories");
81        // create a session
82
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
83
}
84     catch (Exception JavaDoc e)
85     {
86       sp.printHTML("Cannot connect to message bean MDB_BrowseCategories : " +e+"<br>");
87       return ;
88     }
89     try
90     {
91       // create a requestor to receive the reply
92
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
93        // create a message
94
MapMessage JavaDoc message = session.createMapMessage();
95        // set parameters
96
if (region != null)
97         message.setString("region", region);
98       if (username != null)
99         message.setString("nickname", username);
100        if (password != null)
101         message.setString("password", password);
102        message.setJMSCorrelationID("category");
103       // send the message and receive the reply
104
connection.start(); // allows message to be delivered (default is connection stopped)
105
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
106        connection.stop();
107       // read the reply
108
html = reply.getText();
109        // close connection and session
110
requestor.close(); // also close the session
111
connection.close();
112      }
113     catch (Exception JavaDoc e)
114     {
115       sp.printHTML("Cannot get the list of categories: " +e+"<br>");
116       return ;
117     }
118     sp.printHTML(html);
119     sp.printHTMLfooter();
120   }
121
122   /**
123    * Same as <code>doGet</code>.
124    *
125    * @param request a <code>HttpServletRequest</code> value
126    * @param response a <code>HttpServletResponse</code> value
127    * @exception IOException if an error occurs
128    * @exception ServletException if an error occurs
129    */

130   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
131   {
132     doGet(request, response);
133   }
134
135 }
136
Popular Tags