KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > presence > PresenceStatusServlet


1 /**
2  * $RCSfile: PresenceStatusServlet.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/07/26 18:56:14 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.plugin.presence;
13
14 import org.jivesoftware.messenger.XMPPServer;
15 import org.jivesoftware.messenger.plugin.PresencePlugin;
16 import org.jivesoftware.messenger.user.UserNotFoundException;
17 import org.jivesoftware.util.Log;
18 import org.jivesoftware.admin.AuthCheckFilter;
19 import org.xmpp.packet.Presence;
20
21 import javax.servlet.ServletConfig JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServlet JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30
31 /**
32  * Servlet that provides information about the presence status of the users in the system.
33  * The information may be provided in XML format or in graphical mode. Use the <b>type</b>
34  * parameter to specify the type of information to get. Possible values are <b>image</b> and
35  * <b>xml</b>. If no type was defined then an image representation is assumed.<p>
36  * <p/>
37  * The request <b>MUST</b> include the <b>jid</b> parameter. This parameter will be used
38  * to locate the local user in the server. If this parameter is missing from the request then
39  * an error will be logged and nothing will be returned.
40  *
41  * @author Gaston Dombiak
42  */

43 public class PresenceStatusServlet extends HttpServlet JavaDoc {
44
45     private PresencePlugin plugin;
46     private XMLPresenceProvider xmlProvider;
47     private ImagePresenceProvider imageProvider;
48
49     byte available[];
50     byte away[];
51     byte chat[];
52     byte dnd[];
53     byte offline[];
54     byte xa[];
55
56     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
57         super.init(servletConfig);
58         plugin =
59                 (PresencePlugin) XMPPServer.getInstance().getPluginManager().getPlugin("presence");
60         xmlProvider = new XMLPresenceProvider();
61         imageProvider = new ImagePresenceProvider(this);
62         available = loadResource("/images/user-green-16x16.gif");
63         away = loadResource("/images/user-yellow-16x16.gif");
64         chat = loadResource("/images/user-green-16x16.gif");
65         dnd = loadResource("/images/user-red-16x16.gif");
66         offline = loadResource("/images/user-clear-16x16.gif");
67         xa = loadResource("/images/user-yellow-16x16.gif");
68         // Exclude this servlet from requering the user to login
69
AuthCheckFilter.addExclude("presence/status");
70     }
71
72     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73             throws ServletException JavaDoc, IOException JavaDoc {
74         String JavaDoc sender = request.getParameter("req_jid");
75         String JavaDoc jid = request.getParameter("jid");
76         String JavaDoc type = request.getParameter("type");
77         type = type == null ? "image" : type;
78
79         try {
80             Presence presence = plugin.getPresence(sender, jid);
81             if ("image".equals(type)) {
82                 imageProvider.sendInfo(request, response, presence);
83             }
84             else if ("xml".equals(type)) {
85                 xmlProvider.sendInfo(request, response, presence);
86             }
87             else {
88                 Log.warn("The presence servlet received an invalid request of type: " + type);
89                 // TODO Do something
90
}
91         }
92         catch (UserNotFoundException e) {
93             if ("image".equals(type)) {
94                 imageProvider.sendUserNotFound(request, response);
95             }
96             else if ("xml".equals(type)) {
97                 xmlProvider.sendUserNotFound(request, response);
98             }
99             else {
100                 Log.warn("The presence servlet received an invalid request of type: " + type);
101                 // TODO Do something
102
}
103         }
104         catch (IllegalArgumentException JavaDoc e) {
105             if ("image".equals(type)) {
106                 imageProvider.sendUserNotFound(request, response);
107             }
108             else if ("xml".equals(type)) {
109                 xmlProvider.sendUserNotFound(request, response);
110             }
111             else {
112                 Log.warn("The presence servlet received an invalid request of type: " + type);
113                 // TODO Do something
114
}
115         }
116     }
117
118     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
119             throws ServletException JavaDoc, IOException JavaDoc {
120         doGet(request, response);
121     }
122
123     public void destroy() {
124         super.destroy();
125         available = null;
126         away = null;
127         chat = null;
128         dnd = null;
129         offline = null;
130         xa = null;
131         // Release the excluded URL
132
AuthCheckFilter.removeExclude("presence/status");
133     }
134
135     private byte[] loadResource(String JavaDoc path) {
136         ServletContext JavaDoc context = getServletContext();
137         InputStream JavaDoc in = context.getResourceAsStream(path);
138         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
139         try {
140             for (int i = in.read(); i > -1; i = in.read()) {
141                 out.write(i);
142             }
143         }
144         catch (IOException JavaDoc e) {
145             Log.error("error loading:" + path);
146         }
147         return out.toByteArray();
148     }
149
150 }
151
Popular Tags