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