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; 029import org.jivesoftware.smackx.commands.AdHocCommandManager; 030import org.jivesoftware.smackx.commands.RemoteCommand; 031import org.jivesoftware.smackx.xdata.Form; 032import org.jivesoftware.smackx.xdata.FormField; 033import org.jxmpp.jid.EntityBareJid; 034import org.jxmpp.jid.Jid; 035import org.jxmpp.jid.util.JidUtil; 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().getServiceName()); 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 Form answerForm = command.getForm().createAnswerForm(); 076 077 FormField accountJidField = answerForm.getField("accountjid"); 078 accountJidField.addValue(userJid.toString()); 079 080 FormField passwordField = answerForm.getField("password"); 081 passwordField.addValue(password); 082 083 FormField passwordVerifyField = answerForm.getField("password-verify"); 084 passwordVerifyField.addValue(password); 085 086 command.next(answerForm); 087 assert(command.isCompleted()); 088 } 089 090 public RemoteCommand deleteUser() { 091 return deleteUser(connection().getServiceName()); 092 } 093 094 public RemoteCommand deleteUser(Jid service) { 095 return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "delete-user"); 096 } 097 098 public void deleteUser(EntityBareJid userJidToDelete) 099 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 100 Set<EntityBareJid> userJidsToDelete = Collections.singleton(userJidToDelete); 101 deleteUser(userJidsToDelete); 102 } 103 104 public void deleteUser(Set<EntityBareJid> jidsToDelete) 105 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 106 RemoteCommand command = deleteUser(); 107 command.execute(); 108 109 Form answerForm = command.getForm().createAnswerForm(); 110 111 FormField accountJids = answerForm.getField("accountjids"); 112 accountJids.addValues(JidUtil.toStringList(jidsToDelete)); 113 114 command.next(answerForm); 115 assert(command.isCompleted()); 116 } 117}