KickJava   Java API By Example, From Geeks To Geeks.

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


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 general information about a user.
21  * It must be called this way :
22  * <pre>
23  * http://..../ViewUserInfo?userId=xx where xx is the id of the user
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 ViewUserInfo extends HttpServlet JavaDoc
30 {
31   
32   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
33   {
34     sp.printHTMLheader("RUBiS ERROR: View user info");
35     sp.printHTML("<h2>We cannot process your request due to the following error :</h2><br>");
36     sp.printHTML(errorMsg);
37     sp.printHTMLfooter();
38   }
39
40
41   /**
42    * Call the <code>doPost</code> method.
43    *
44    * @param request a <code>HttpServletRequest</code> value
45    * @param response a <code>HttpServletResponse</code> value
46    * @exception IOException if an error occurs
47    * @exception ServletException if an error occurs
48    */

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

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