IQTimeHandler.java 3.41 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16
package org.jivesoftware.messenger.handler;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
17 18 19
import org.jivesoftware.messenger.IQHandlerInfo;
import org.jivesoftware.messenger.disco.ServerFeaturesProvider;
import org.jivesoftware.util.FastDateFormat;
Matt Tucker's avatar
Matt Tucker committed
20
import org.xmpp.packet.IQ;
Matt Tucker's avatar
Matt Tucker committed
21

22 23 24 25 26 27
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

Matt Tucker's avatar
Matt Tucker committed
28
/**
29 30 31
 * Implements the TYPE_IQ jabber:iq:time protocol (time info) as
 * as defined by JEP-0090. Allows Jabber entities to query each
 * other's local time.  The server will respond with its local time.
Matt Tucker's avatar
Matt Tucker committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * <p/>
 * <h2>Assumptions</h2>
 * This handler assumes that the time request is addressed to itself.
 * An appropriate TYPE_IQ tag matcher should be placed in front of this
 * one to route TYPE_IQ time requests not addressed to the server to
 * another channel (probably for direct delivery to the recipient).
 * <p/>
 * <h2>Warning</h2>
 * There should be a way of determining whether a session has
 * authorization to access this feature. I'm not sure it is a good
 * idea to do authorization in each handler. It would be nice if
 * the framework could assert authorization policies across channels.
 *
 * @author Iain Shigeoka
 */
public class IQTimeHandler extends IQHandler implements ServerFeaturesProvider {

49 50 51 52
     // todo Make display text match the locale of user (xml:lang support)
    private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
    private static final DateFormat TIME_FORMAT = DateFormat.getTimeInstance(DateFormat.LONG);
    // UTC and not JEP-0082 time format is used as per the JEP-0090 specification.
53 54
    private static final FastDateFormat UTC_FORMAT = FastDateFormat
            .getInstance("yyyyMMdd'T'HH:mm:ss", TimeZone.getTimeZone("GMT+0"));
55

Matt Tucker's avatar
Matt Tucker committed
56 57 58 59 60
    private Element responseElement;
    private IQHandlerInfo info;

    public IQTimeHandler() {
        super("XMPP Server Time Handler");
61
        info = new IQHandlerInfo("query", "jabber:iq:time");
Matt Tucker's avatar
Matt Tucker committed
62 63 64 65 66 67 68 69
        responseElement = DocumentHelper.createElement(QName.get("query", "jabber:iq:time"));
        responseElement.addElement("utc");
        responseElement.addElement("tz").setText(TIME_FORMAT.getTimeZone().getDisplayName());
        responseElement.addElement("display");
    }

    public IQ handleIQ(IQ packet) {
        IQ response = null;
Matt Tucker's avatar
Matt Tucker committed
70
        response = IQ.createResultIQ(packet);
71
        response.setChildElement(buildResponse());
Matt Tucker's avatar
Matt Tucker committed
72 73 74 75 76 77
        return response;
    }

    /**
     * Build the responseElement packet
     */
78 79
    private Element buildResponse() {
        Element response = responseElement.createCopy();
Matt Tucker's avatar
Matt Tucker committed
80
        Date current = new Date();
81
        response.element("utc").setText(UTC_FORMAT.format(current));
82
        StringBuilder display = new StringBuilder(DATE_FORMAT.format(current));
Matt Tucker's avatar
Matt Tucker committed
83 84
        display.append(' ');
        display.append(TIME_FORMAT.format(current));
85 86
        response.element("display").setText(display.toString());
        return response;
Matt Tucker's avatar
Matt Tucker committed
87 88 89 90 91 92
    }

    public IQHandlerInfo getInfo() {
        return info;
    }

93
    public Iterator<String> getFeatures() {
94
        return Collections.singleton("jabber:iq:time").iterator();
Matt Tucker's avatar
Matt Tucker committed
95
    }
96
}