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.admin;
018
019import java.util.Collections;
020import java.util.Map;
021import java.util.Set;
022import java.util.WeakHashMap;
023
024import org.jivesoftware.smack.Manager;
025import org.jivesoftware.smack.SmackException.NoResponseException;
026import org.jivesoftware.smack.SmackException.NotConnectedException;
027import org.jivesoftware.smack.XMPPConnection;
028import org.jivesoftware.smack.XMPPException.XMPPErrorException;
029
030import org.jivesoftware.smackx.commands.AdHocCommandManager;
031import org.jivesoftware.smackx.commands.RemoteCommand;
032import org.jivesoftware.smackx.xdata.Form;
033import org.jivesoftware.smackx.xdata.FormField;
034
035import org.jxmpp.jid.EntityBareJid;
036import org.jxmpp.jid.Jid;
037import org.jxmpp.jid.util.JidUtil;
038
039public class ServiceAdministrationManager extends Manager {
040
041    public static final String COMMAND_NODE = "http://jabber.org/protocol/admin";
042
043    private static final String COMMAND_NODE_HASHSIGN = COMMAND_NODE + '#';
044
045    private static final Map<XMPPConnection, ServiceAdministrationManager> INSTANCES = new WeakHashMap<>();
046
047    public static synchronized ServiceAdministrationManager getInstanceFor(XMPPConnection connection) {
048        ServiceAdministrationManager serviceAdministrationManager = INSTANCES.get(connection);
049        if (serviceAdministrationManager == null) {
050            serviceAdministrationManager = new ServiceAdministrationManager(connection);
051            INSTANCES.put(connection, serviceAdministrationManager);
052        }
053        return serviceAdministrationManager;
054    }
055
056    private final AdHocCommandManager adHocCommandManager;
057
058    public ServiceAdministrationManager(XMPPConnection connection) {
059        super(connection);
060
061        adHocCommandManager = AdHocCommandManager.getAddHocCommandsManager(connection);
062    }
063
064    public RemoteCommand addUser() {
065        return addUser(connection().getXMPPServiceDomain());
066    }
067
068    public RemoteCommand addUser(Jid service) {
069        return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "add-user");
070    }
071
072    public void addUser(final EntityBareJid userJid, final String password)
073                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
074        RemoteCommand command = addUser();
075        command.execute();
076
077        Form answerForm = command.getForm().createAnswerForm();
078
079        FormField accountJidField = answerForm.getField("accountjid");
080        accountJidField.addValue(userJid.toString());
081
082        FormField passwordField = answerForm.getField("password");
083        passwordField.addValue(password);
084
085        FormField passwordVerifyField = answerForm.getField("password-verify");
086        passwordVerifyField.addValue(password);
087
088        command.execute(answerForm);
089        assert (command.isCompleted());
090    }
091
092    public RemoteCommand deleteUser() {
093        return deleteUser(connection().getXMPPServiceDomain());
094    }
095
096    public RemoteCommand deleteUser(Jid service) {
097        return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "delete-user");
098    }
099
100    public void deleteUser(EntityBareJid userJidToDelete)
101                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
102        Set<EntityBareJid> userJidsToDelete = Collections.singleton(userJidToDelete);
103        deleteUser(userJidsToDelete);
104    }
105
106    public void deleteUser(Set<EntityBareJid> jidsToDelete)
107                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
108        RemoteCommand command = deleteUser();
109        command.execute();
110
111        Form answerForm = command.getForm().createAnswerForm();
112
113        FormField accountJids = answerForm.getField("accountjids");
114        accountJids.addValues(JidUtil.toStringList(jidsToDelete));
115
116        command.execute(answerForm);
117        assert (command.isCompleted());
118    }
119}