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