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