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;
034import org.jivesoftware.smack.packet.XMPPError.Condition;
035import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
036import org.jivesoftware.smackx.time.packet.Time;
037
038public 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            public void connectionCreated(XMPPConnection connection) {
047                getInstanceFor(connection);
048            }
049        });
050    }
051
052    public static void setAutoEnable(boolean autoEnable) {
053        EntityTimeManager.autoEnable = autoEnable;
054    }
055
056    public synchronized static EntityTimeManager getInstanceFor(XMPPConnection connection) {
057        EntityTimeManager entityTimeManager = INSTANCES.get(connection);
058        if (entityTimeManager == null) {
059            entityTimeManager = new EntityTimeManager(connection);
060            INSTANCES.put(connection, entityTimeManager);
061        }
062        return entityTimeManager;
063    }
064
065    private boolean enabled = false;
066
067    private EntityTimeManager(XMPPConnection connection) {
068        super(connection);
069        if (autoEnable)
070            enable();
071
072        connection.registerIQRequestHandler(new AbstractIqRequestHandler(Time.ELEMENT, Time.NAMESPACE, Type.get,
073                        Mode.async) {
074            @Override
075            public IQ handleIQRequest(IQ iqRequest) {
076                if (enabled) {
077                    return Time.createResponse(iqRequest);
078                }
079                else {
080                    return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
081                }
082            }
083        });
084    }
085
086    public synchronized void enable() {
087        if (enabled)
088            return;
089        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
090        sdm.addFeature(Time.NAMESPACE);
091        enabled = true;
092    }
093
094    public synchronized void disable() {
095        if (!enabled)
096            return;
097        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
098        sdm.removeFeature(Time.NAMESPACE);
099        enabled = false;
100    }
101
102    public boolean isTimeSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException  {
103        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Time.NAMESPACE);
104    }
105
106    public Time getTime(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException {
107        if (!isTimeSupported(jid))
108            return null;
109
110        Time request = new Time();
111        Time response = (Time) connection().createPacketCollectorAndSend(request).nextResultOrThrow();
112        return response;
113    }
114}