KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > handler > IQTimeHandler


1 /**
2  * $RCSfile: IQTimeHandler.java,v $
3  * $Revision: 1.7 $
4  * $Date: 2005/07/27 00:05:38 $
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.handler;
13
14 import org.jivesoftware.messenger.disco.ServerFeaturesProvider;
15 import org.jivesoftware.messenger.IQHandlerInfo;
16 import java.text.DateFormat JavaDoc;
17 import java.text.SimpleDateFormat JavaDoc;
18 import java.util.*;
19
20 import org.dom4j.DocumentHelper;
21 import org.dom4j.Element;
22 import org.dom4j.QName;
23 import org.xmpp.packet.IQ;
24
25 /**
26  * Implements the TYPE_IQ jabber:iq:time protocol (time info) as
27  * as defined by JEP-0090. Allows Jabber entities to query each
28  * other's local time. The server will respond with its local time.
29  * <p/>
30  * <h2>Assumptions</h2>
31  * This handler assumes that the time request is addressed to itself.
32  * An appropriate TYPE_IQ tag matcher should be placed in front of this
33  * one to route TYPE_IQ time requests not addressed to the server to
34  * another channel (probably for direct delivery to the recipient).
35  * <p/>
36  * <h2>Warning</h2>
37  * There should be a way of determining whether a session has
38  * authorization to access this feature. I'm not sure it is a good
39  * idea to do authorization in each handler. It would be nice if
40  * the framework could assert authorization policies across channels.
41  *
42  * @author Iain Shigeoka
43  */

44 public class IQTimeHandler extends IQHandler implements ServerFeaturesProvider {
45
46      // todo Make display text match the locale of user (xml:lang support)
47
private static final DateFormat JavaDoc DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
48     private static final DateFormat JavaDoc TIME_FORMAT = DateFormat.getTimeInstance(DateFormat.LONG);
49     // UTC and not JEP-0082 time format is used as per the JEP-0090 specification.
50
private static final SimpleDateFormat JavaDoc UTC_FORMAT = new SimpleDateFormat JavaDoc("yyyyMMdd'T'HH:mm:ss");
51
52     static {
53         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
54     }
55
56     private Element responseElement;
57     private IQHandlerInfo info;
58
59     public IQTimeHandler() {
60         super("XMPP Server Time Handler");
61         info = new IQHandlerInfo("query", "jabber:iq:time");
62         responseElement = DocumentHelper.createElement(QName.get("query", "jabber:iq:time"));
63         responseElement.addElement("utc");
64         responseElement.addElement("tz").setText(TIME_FORMAT.getTimeZone().getDisplayName());
65         responseElement.addElement("display");
66     }
67
68     public IQ handleIQ(IQ packet) {
69         IQ response = null;
70         response = IQ.createResultIQ(packet);
71         response.setChildElement(buildResponse());
72         return response;
73     }
74
75     /**
76      * Build the responseElement packet
77      */

78     private Element buildResponse() {
79         Element response = responseElement.createCopy();
80         Date current = new Date();
81         response.element("utc").setText(UTC_FORMAT.format(current));
82         StringBuilder JavaDoc display = new StringBuilder JavaDoc(DATE_FORMAT.format(current));
83         display.append(' ');
84         display.append(TIME_FORMAT.format(current));
85         response.element("display").setText(display.toString());
86         return response;
87     }
88
89     public IQHandlerInfo getInfo() {
90         return info;
91     }
92
93     public Iterator getFeatures() {
94         return Collections.singleton("jabber:iq:time").iterator();
95     }
96 }
Popular Tags