EntityTimeManager.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.time;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.ConnectionCreationListener;
  21. import org.jivesoftware.smack.Manager;
  22. import org.jivesoftware.smack.SmackException.NoResponseException;
  23. import org.jivesoftware.smack.SmackException.NotConnectedException;
  24. import org.jivesoftware.smack.XMPPConnection;
  25. import org.jivesoftware.smack.XMPPConnectionRegistry;
  26. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  27. import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
  28. import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
  29. import org.jivesoftware.smack.packet.IQ;
  30. import org.jivesoftware.smack.packet.IQ.Type;
  31. import org.jivesoftware.smack.packet.StanzaError.Condition;

  32. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  33. import org.jivesoftware.smackx.time.packet.Time;

  34. import org.jxmpp.jid.Jid;

  35. public final class EntityTimeManager extends Manager {

  36.     private static final Map<XMPPConnection, EntityTimeManager> INSTANCES = new WeakHashMap<>();

  37.     private static boolean autoEnable = true;

  38.     static {
  39.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  40.             @Override
  41.             public void connectionCreated(XMPPConnection connection) {
  42.                 getInstanceFor(connection);
  43.             }
  44.         });
  45.     }

  46.     public static void setAutoEnable(boolean autoEnable) {
  47.         EntityTimeManager.autoEnable = autoEnable;
  48.     }

  49.     public static synchronized EntityTimeManager getInstanceFor(XMPPConnection connection) {
  50.         EntityTimeManager entityTimeManager = INSTANCES.get(connection);
  51.         if (entityTimeManager == null) {
  52.             entityTimeManager = new EntityTimeManager(connection);
  53.             INSTANCES.put(connection, entityTimeManager);
  54.         }
  55.         return entityTimeManager;
  56.     }

  57.     private boolean enabled = false;

  58.     private EntityTimeManager(XMPPConnection connection) {
  59.         super(connection);
  60.         if (autoEnable)
  61.             enable();

  62.         connection.registerIQRequestHandler(new AbstractIqRequestHandler(Time.ELEMENT, Time.NAMESPACE, Type.get,
  63.                         Mode.async) {
  64.             @Override
  65.             public IQ handleIQRequest(IQ iqRequest) {
  66.                 if (enabled) {
  67.                     return Time.createResponse(iqRequest);
  68.                 }
  69.                 else {
  70.                     return IQ.createErrorResponse(iqRequest, Condition.not_acceptable);
  71.                 }
  72.             }
  73.         });
  74.     }

  75.     public synchronized void enable() {
  76.         if (enabled)
  77.             return;
  78.         ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
  79.         sdm.addFeature(Time.NAMESPACE);
  80.         enabled = true;
  81.     }

  82.     public synchronized void disable() {
  83.         if (!enabled)
  84.             return;
  85.         ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
  86.         sdm.removeFeature(Time.NAMESPACE);
  87.         enabled = false;
  88.     }

  89.     public boolean isTimeSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  90.         return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Time.NAMESPACE);
  91.     }

  92.     public Time getTime(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  93.         if (!isTimeSupported(jid))
  94.             return null;

  95.         Time request = new Time();
  96.         // TODO Add Time(Jid) constructor and use this constructor instead
  97.         request.setTo(jid);
  98.         return connection().createStanzaCollectorAndSend(request).nextResultOrThrow();
  99.     }
  100. }