001/**
002 *
003 * Copyright 2016-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.iot;
018
019import java.util.logging.Logger;
020
021import org.jivesoftware.smack.Manager;
022import org.jivesoftware.smack.XMPPConnection;
023import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
024import org.jivesoftware.smack.packet.IQ;
025
026import org.jivesoftware.smackx.iot.provisioning.IoTProvisioningManager;
027
028import org.jxmpp.jid.Jid;
029
030public abstract class IoTManager extends Manager {
031
032    private static final Logger LOGGER = Logger.getLogger(IoTManager.class.getName());
033
034    private final IoTProvisioningManager ioTProvisioningManager;
035
036    private boolean allowNonFriends;
037
038    private static boolean autoEnable;
039
040    public static void setAutoEnableIoTManagers(boolean autoEnable) {
041        IoTManager.autoEnable = autoEnable;
042    }
043
044    public static boolean isAutoEnableActive() {
045        return autoEnable;
046    }
047
048    protected IoTManager(XMPPConnection connection) {
049        super(connection);
050
051        ioTProvisioningManager = IoTProvisioningManager.getInstanceFor(connection);
052    }
053
054    /**
055     * Set whether or not non friends should be able to use the services provided by this manager. Those non-friend
056     * entities still need to know the full JID for IQ based requests.
057     *
058     * @param allowNonFriends true to allow everyone to use the services.
059     */
060    public void setAllowNonFriends(boolean allowNonFriends) {
061        this.allowNonFriends = allowNonFriends;
062    }
063
064    protected boolean isAllowed(Jid jid) {
065        if (allowNonFriends) return true;
066
067        return ioTProvisioningManager.isMyFriend(jid);
068    }
069
070    protected abstract class IoTIqRequestHandler extends AbstractIqRequestHandler {
071
072        protected IoTIqRequestHandler(String element, String namespace, IQ.Type type, Mode mode) {
073            super(element, namespace, type, mode);
074        }
075
076        @Override
077        public final IQ handleIQRequest(IQ iqRequest) {
078            if (!isAllowed(iqRequest.getFrom())) {
079                LOGGER.warning("Ignoring IQ request " + iqRequest);
080                return null;
081            }
082
083            return handleIoTIqRequest(iqRequest);
084        }
085
086        protected abstract IQ handleIoTIqRequest(IQ iqRequest);
087    }
088}