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