001/** 002 * 003 * Copyright 2016-2020 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.FillableForm; 033 034import org.jxmpp.jid.EntityBareJid; 035import org.jxmpp.jid.Jid; 036 037public class ServiceAdministrationManager extends Manager { 038 039 public static final String COMMAND_NODE = "http://jabber.org/protocol/admin"; 040 041 private static final String COMMAND_NODE_HASHSIGN = COMMAND_NODE + '#'; 042 043 private static final Map<XMPPConnection, ServiceAdministrationManager> INSTANCES = new WeakHashMap<>(); 044 045 public static synchronized ServiceAdministrationManager getInstanceFor(XMPPConnection connection) { 046 ServiceAdministrationManager serviceAdministrationManager = INSTANCES.get(connection); 047 if (serviceAdministrationManager == null) { 048 serviceAdministrationManager = new ServiceAdministrationManager(connection); 049 INSTANCES.put(connection, serviceAdministrationManager); 050 } 051 return serviceAdministrationManager; 052 } 053 054 private final AdHocCommandManager adHocCommandManager; 055 056 public ServiceAdministrationManager(XMPPConnection connection) { 057 super(connection); 058 059 adHocCommandManager = AdHocCommandManager.getAddHocCommandsManager(connection); 060 } 061 062 public RemoteCommand addUser() { 063 return addUser(connection().getXMPPServiceDomain()); 064 } 065 066 public RemoteCommand addUser(Jid service) { 067 return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "add-user"); 068 } 069 070 public void addUser(final EntityBareJid userJid, final String password) 071 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 072 RemoteCommand command = addUser(); 073 command.execute(); 074 075 FillableForm answerForm = new FillableForm(command.getForm()); 076 077 answerForm.setAnswer("accountjid", userJid); 078 answerForm.setAnswer("password", password); 079 answerForm.setAnswer("password-verify", password); 080 081 command.execute(answerForm); 082 assert command.isCompleted(); 083 } 084 085 public RemoteCommand deleteUser() { 086 return deleteUser(connection().getXMPPServiceDomain()); 087 } 088 089 public RemoteCommand deleteUser(Jid service) { 090 return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "delete-user"); 091 } 092 093 public void deleteUser(EntityBareJid userJidToDelete) 094 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 095 Set<EntityBareJid> userJidsToDelete = Collections.singleton(userJidToDelete); 096 deleteUser(userJidsToDelete); 097 } 098 099 public void deleteUser(Set<EntityBareJid> jidsToDelete) 100 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 101 RemoteCommand command = deleteUser(); 102 command.execute(); 103 104 FillableForm answerForm = new FillableForm(command.getForm()); 105 106 answerForm.setAnswer("accountjids", jidsToDelete); 107 108 command.execute(answerForm); 109 assert command.isCompleted(); 110 } 111}