1 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 ; 17 import java.text.SimpleDateFormat ; 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 44 public class IQTimeHandler extends IQHandler implements ServerFeaturesProvider { 45 46 private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM); 48 private static final DateFormat TIME_FORMAT = DateFormat.getTimeInstance(DateFormat.LONG); 49 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat ("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 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 display = new StringBuilder (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 |