KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: XMLPresenceProvider.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/07/22 20:49:46 $
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.user.User;
16 import org.jivesoftware.messenger.user.UserNotFoundException;
17 import org.xmpp.packet.JID;
18 import org.xmpp.packet.PacketError;
19 import org.xmpp.packet.Presence;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * The XMLPresenceProvider provides information about the users presence in XML format.
28  * The returned XML will include the last known presence of the user. If the user is offline
29  * then the unavailable presence will be recreated with the last known presence status.
30  *
31  * @author Gaston Dombiak
32  */

33 class XMLPresenceProvider extends PresenceInfoProvider {
34
35     public void sendInfo(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
36             Presence presence) throws IOException JavaDoc {
37         response.setContentType("text/xml");
38         PrintWriter JavaDoc out = response.getWriter();
39         if (presence == null) {
40             // Recreate the unavailable presence with the last known status
41
JID targetJID = new JID(request.getParameter("jid"));
42             presence = new Presence(Presence.Type.unavailable);
43             XMPPServer server = XMPPServer.getInstance();
44             try {
45                 User user = server.getUserManager().getUser(targetJID.getNode());
46                 String JavaDoc status = server.getPresenceManager().getLastPresenceStatus(user);
47                 if (status != null) {
48                     presence.setStatus(status);
49                 }
50             }
51             catch (UserNotFoundException e) {}
52             presence.setFrom(targetJID);
53         }
54         out.println(presence.toXML());
55         out.flush();
56     }
57
58     public void sendUserNotFound(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
59             throws IOException JavaDoc {
60         response.setContentType("text/xml");
61         PrintWriter JavaDoc out = response.getWriter();
62         // Send a forbidden presence
63
Presence presence = new Presence();
64         presence.setError(PacketError.Condition.forbidden);
65         try {
66             presence.setFrom(new JID(request.getParameter("jid")));
67         }
68         catch (Exception JavaDoc e) {}
69         try {
70             presence.setTo(new JID(request.getParameter("req_jid")));
71         }
72         catch (Exception JavaDoc e) {}
73         out.println(presence.toXML());
74         out.flush();
75     }
76 }
77
Popular Tags