001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014-2021 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    public Time(TimeBuilder timeBuilder) {
050        super(timeBuilder, ELEMENT, NAMESPACE);
051        utc = timeBuilder.getUtc();
052        tzo = timeBuilder.getTzo();
053
054        Type type = getType();
055        switch (type) {
056        case get:
057            if (utc != null) {
058                throw new IllegalArgumentException("Time requests must not have utc set");
059            }
060            if (tzo != null) {
061                throw new IllegalArgumentException("Time requests must not have tzo set");
062            }
063            break;
064        case result:
065            StringUtils.requireNotNullNorEmpty(utc, "Must have set a utc value");
066            StringUtils.requireNotNullNorEmpty(tzo, "Must have set a tzo value");
067            break;
068        case error:
069            // Nothing to check.
070            break;
071        case set:
072            throw new IllegalArgumentException("Invalid IQ type");
073        }
074    }
075
076    /**
077     * Returns the local time or <code>null</code> if the time hasn't been set.
078     *
079     * @return the local time.
080     */
081    public Date getTime() {
082        if (utc == null) {
083            return null;
084        }
085        Date date = null;
086        try {
087            date = XmppDateTime.parseDate(utc);
088        }
089        catch (Exception e) {
090            LOGGER.log(Level.SEVERE, "Error getting local time", e);
091        }
092        return date;
093    }
094
095    @Override
096    public String getUtc() {
097        return utc;
098    }
099
100    @Override
101    public String getTzo() {
102        return tzo;
103    }
104
105    @Override
106    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
107        if (utc != null) {
108            buf.rightAngleBracket();
109            buf.element("utc", utc);
110            buf.element("tzo", tzo);
111        } else {
112            buf.setEmptyElement();
113        }
114
115        return buf;
116    }
117
118    public static TimeBuilder builder(XMPPConnection connection) {
119        return new TimeBuilder(connection);
120    }
121
122    public static TimeBuilder builder(IqData iqData) {
123        return new TimeBuilder(iqData);
124    }
125
126    public static TimeBuilder builder(String stanzaId) {
127        return new TimeBuilder(stanzaId);
128    }
129
130    public static TimeBuilder builder(Time timeRequest, Calendar calendar) {
131        IqData iqData = IqData.createResponseData(timeRequest);
132        return builder(iqData).setTime(calendar);
133    }
134
135    public static TimeBuilder builder(Time timeRequest) {
136        return builder(timeRequest, Calendar.getInstance());
137    }
138}