001/**
002 *
003 * Copyright 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;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.Manager;
024import org.jivesoftware.smack.SmackException;
025import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
026import org.jivesoftware.smack.SmackException.NoResponseException;
027import org.jivesoftware.smack.SmackException.NotConnectedException;
028import org.jivesoftware.smack.XMPPConnection;
029import org.jivesoftware.smack.XMPPConnectionRegistry;
030import org.jivesoftware.smack.XMPPException.XMPPErrorException;
031import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
032import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
033import org.jivesoftware.smack.packet.IQ;
034import org.jivesoftware.smack.packet.StanzaError.Condition;
035
036import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
037import org.jivesoftware.smackx.time.packet.Time;
038
039import org.jxmpp.jid.Jid;
040
041public final class EntityTimeManager extends Manager {
042
043    private static final Map<XMPPConnection, EntityTimeManager> INSTANCES = new WeakHashMap<>();
044
045    private static boolean autoEnable = true;
046
047    static {
048        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
049            @Override
050            public void connectionCreated(XMPPConnection connection) {
051                getInstanceFor(connection);
052            }
053        });
054    }
055
056    public static void setAutoEnable(boolean autoEnable) {
057        EntityTimeManager.autoEnable = autoEnable;
058    }
059
060    public static synchronized EntityTimeManager getInstanceFor(XMPPConnection connection) {
061        EntityTimeManager entityTimeManager = INSTANCES.get(connection);
062        if (entityTimeManager == null) {
063            entityTimeManager = new EntityTimeManager(connection);
064            INSTANCES.put(connection, entityTimeManager);
065        }
066        return entityTimeManager;
067    }
068
069    private boolean enabled = false;
070
071    private EntityTimeManager(XMPPConnection connection) {
072        super(connection);
073        if (autoEnable)
074            enable();
075
076        connection.registerIQRequestHandler(new AbstractIqRequestHandler(Time.ELEMENT, Time.NAMESPACE, IQ.Type.get,
077                        Mode.async) {
078            @Override
079            public IQ handleIQRequest(IQ iqRequest) {
080                if (!enabled) {
081                    return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
082                }
083
084                Time timeRequest = (Time) iqRequest;
085                Time timeResponse = Time.builder(timeRequest).build();
086                return timeResponse;
087            }
088        });
089    }
090
091    public synchronized void enable() {
092        if (enabled)
093            return;
094        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
095        sdm.addFeature(Time.NAMESPACE);
096        enabled = true;
097    }
098
099    public synchronized void disable() {
100        if (!enabled)
101            return;
102        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
103        sdm.removeFeature(Time.NAMESPACE);
104        enabled = false;
105    }
106
107    public boolean isTimeSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
108        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Time.NAMESPACE);
109    }
110
111    public Time getTime(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException,
112                    InterruptedException, FeatureNotSupportedException {
113        if (!isTimeSupported(jid)) {
114            throw new SmackException.FeatureNotSupportedException(Time.NAMESPACE);
115        }
116
117        XMPPConnection connection = connection();
118        Time request = Time.builder(connection)
119                        .to(jid)
120                        .build();
121        return connection.sendIqRequestAndWaitForResponse(request);
122    }
123}