001/** 002 * 003 * Copyright 2003-2007 Jive Software, 2014-2024 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.time.packet; 018 019import java.util.Calendar; 020import java.util.Date; 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.XMPPConnection; 025import org.jivesoftware.smack.packet.IQ; 026import org.jivesoftware.smack.packet.IqData; 027import org.jivesoftware.smack.util.StringUtils; 028 029import org.jxmpp.util.XmppDateTime; 030 031/** 032 * A Time IQ packet, which is used by XMPP clients to exchange their respective local 033 * times. Clients that wish to fully support the entity time protocol should register 034 * a PacketListener for incoming time requests that then respond with the local time. 035 * 036 * @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a> 037 * @author Florian Schmaus 038 */ 039public class Time extends IQ implements TimeView { 040 041 public static final String NAMESPACE = "urn:xmpp:time"; 042 public static final String ELEMENT = "time"; 043 044 private static final Logger LOGGER = Logger.getLogger(Time.class.getName()); 045 046 private final String utc; 047 private final String tzo; 048 049 @SuppressWarnings("this-escape") 050 public Time(TimeBuilder timeBuilder) { 051 super(timeBuilder, ELEMENT, NAMESPACE); 052 utc = timeBuilder.getUtc(); 053 tzo = timeBuilder.getTzo(); 054 055 Type type = getType(); 056 switch (type) { 057 case get: 058 if (utc != null) { 059 throw new IllegalArgumentException("Time requests must not have utc set"); 060 } 061 if (tzo != null) { 062 throw new IllegalArgumentException("Time requests must not have tzo set"); 063 } 064 break; 065 case result: 066 StringUtils.requireNotNullNorEmpty(utc, "Must have set a utc value"); 067 StringUtils.requireNotNullNorEmpty(tzo, "Must have set a tzo value"); 068 break; 069 case error: 070 // Nothing to check. 071 break; 072 case set: 073 throw new IllegalArgumentException("Invalid IQ type"); 074 } 075 } 076 077 /** 078 * Returns the local time or <code>null</code> if the time hasn't been set. 079 * 080 * @return the local time. 081 */ 082 public Date getTime() { 083 if (utc == null) { 084 return null; 085 } 086 Date date = null; 087 try { 088 date = XmppDateTime.parseDate(utc); 089 } 090 catch (Exception e) { 091 LOGGER.log(Level.SEVERE, "Error getting local time", e); 092 } 093 return date; 094 } 095 096 @Override 097 public String getUtc() { 098 return utc; 099 } 100 101 @Override 102 public String getTzo() { 103 return tzo; 104 } 105 106 @Override 107 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 108 if (utc != null) { 109 buf.rightAngleBracket(); 110 buf.element("utc", utc); 111 buf.element("tzo", tzo); 112 } else { 113 buf.setEmptyElement(); 114 } 115 116 return buf; 117 } 118 119 public static TimeBuilder builder(XMPPConnection connection) { 120 return new TimeBuilder(connection); 121 } 122 123 public static TimeBuilder builder(IqData iqData) { 124 return new TimeBuilder(iqData); 125 } 126 127 public static TimeBuilder builder(String stanzaId) { 128 return new TimeBuilder(stanzaId); 129 } 130 131 public static TimeBuilder builder(Time timeRequest, Calendar calendar) { 132 IqData iqData = IqData.createResponseData(timeRequest); 133 return builder(iqData).setTime(calendar); 134 } 135 136 public static TimeBuilder builder(Time timeRequest) { 137 return builder(timeRequest, Calendar.getInstance()); 138 } 139}