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