Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/admin/ServiceAdministrationManage...

112 lines
4.1 KiB
Java
Raw Normal View History

/**
*
* Copyright 2016-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.admin;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.commands.AdHocCommandManager;
import org.jivesoftware.smackx.commands.RemoteCommand;
import org.jivesoftware.smackx.xdata.Form;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.Jid;
public class ServiceAdministrationManager extends Manager {
public static final String COMMAND_NODE = "http://jabber.org/protocol/admin";
private static final String COMMAND_NODE_HASHSIGN = COMMAND_NODE + '#';
private static final Map<XMPPConnection, ServiceAdministrationManager> INSTANCES = new WeakHashMap<>();
public static synchronized ServiceAdministrationManager getInstanceFor(XMPPConnection connection) {
ServiceAdministrationManager serviceAdministrationManager = INSTANCES.get(connection);
if (serviceAdministrationManager == null) {
serviceAdministrationManager = new ServiceAdministrationManager(connection);
INSTANCES.put(connection, serviceAdministrationManager);
}
return serviceAdministrationManager;
}
private final AdHocCommandManager adHocCommandManager;
public ServiceAdministrationManager(XMPPConnection connection) {
super(connection);
adHocCommandManager = AdHocCommandManager.getAddHocCommandsManager(connection);
}
public RemoteCommand addUser() {
return addUser(connection().getXMPPServiceDomain());
}
public RemoteCommand addUser(Jid service) {
return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "add-user");
}
public void addUser(final EntityBareJid userJid, final String password)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
RemoteCommand command = addUser();
command.execute();
Form answerForm = command.getForm().createAnswerForm();
answerForm.setAnswer("accountjid", userJid);
answerForm.setAnswer("password", password);
answerForm.setAnswer("password-verify", password);
Use the correct ad-hoc action in ServiceAdministrationManager Fixes SMACK-804. Thanks to Holger Weiß for his help with the issue. Smack would always use the 'next' action only 'execute' (which is always allowed) and 'complete' where candidates. Example XMPP trace: SENT: <iq to='salem.geekplace.eu' id='f0JiQ-45' type='set'><command xmlns='http://jabber.org/protocol/commands' node='http://jabber.org/protocol/admin#add-user' action='execute'></command></iq><r xmlns='urn:xmpp:sm:3'/> RECV: <iq xml:lang='en' to='sinttest-admin@salem.geekplace.eu/one-phx8g' from='salem.geekplace.eu' type='result' id='f0JiQ-45'> <command status='executing' sessionid='2018-02-21T14:48:13.508812Z' node='http://jabber.org/protocol/admin#add-user' xmlns='http://jabber.org/protocol/commands'> <actions execute='complete'> <complete/> </actions> <x type='form' xmlns='jabber:x:data'> <title>Add User</title> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single' label='Jabber ID'><required/></field> <field var='password' type='text-private' label='Password'><required/></field> <field var='password-verify' type='text-private' label='Password Verification'><required/></field> </x> </command> </iq> SENT: <iq to='salem.geekplace.eu' id='f0JiQ-49' type='set'> <command xmlns='http://jabber.org/protocol/commands' node='http://jabber.org/protocol/admin#add-user' sessionid='2018-02-21T14:48:13.508812Z' action='next'> <x xmlns='jabber:x:data' type='submit'> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single'><value>smack-inttest-one-phx8g@salem.geekplace.eu</value></field> <field var='password' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> <field var='password-verify' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> </x> </command> </iq> RECV: <iq xml:lang='en' to='sinttest-admin@salem.geekplace.eu/one-phx8g' from='salem.geekplace.eu' type='error' id='f0JiQ-49'> <command action='next' sessionid='2018-02-21T14:48:13.508812Z' node='http://jabber.org/protocol/admin#add-user' xmlns='http://jabber.org/protocol/commands'> <x type='submit' xmlns='jabber:x:data'> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single'><value>smack-inttest-one-phx8g@salem.geekplace.eu</value></field> <field var='password' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> <field var='password-verify' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> </x> </command> <error code='400' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Unexpected action</text> </error> </iq>
2018-02-21 16:34:03 +01:00
command.execute(answerForm);
assert command.isCompleted();
}
public RemoteCommand deleteUser() {
return deleteUser(connection().getXMPPServiceDomain());
}
public RemoteCommand deleteUser(Jid service) {
return adHocCommandManager.getRemoteCommand(service, COMMAND_NODE_HASHSIGN + "delete-user");
}
public void deleteUser(EntityBareJid userJidToDelete)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
Set<EntityBareJid> userJidsToDelete = Collections.singleton(userJidToDelete);
deleteUser(userJidsToDelete);
}
public void deleteUser(Set<EntityBareJid> jidsToDelete)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
RemoteCommand command = deleteUser();
command.execute();
Form answerForm = command.getForm().createAnswerForm();
answerForm.setAnswer("accountjids", jidsToDelete);
Use the correct ad-hoc action in ServiceAdministrationManager Fixes SMACK-804. Thanks to Holger Weiß for his help with the issue. Smack would always use the 'next' action only 'execute' (which is always allowed) and 'complete' where candidates. Example XMPP trace: SENT: <iq to='salem.geekplace.eu' id='f0JiQ-45' type='set'><command xmlns='http://jabber.org/protocol/commands' node='http://jabber.org/protocol/admin#add-user' action='execute'></command></iq><r xmlns='urn:xmpp:sm:3'/> RECV: <iq xml:lang='en' to='sinttest-admin@salem.geekplace.eu/one-phx8g' from='salem.geekplace.eu' type='result' id='f0JiQ-45'> <command status='executing' sessionid='2018-02-21T14:48:13.508812Z' node='http://jabber.org/protocol/admin#add-user' xmlns='http://jabber.org/protocol/commands'> <actions execute='complete'> <complete/> </actions> <x type='form' xmlns='jabber:x:data'> <title>Add User</title> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single' label='Jabber ID'><required/></field> <field var='password' type='text-private' label='Password'><required/></field> <field var='password-verify' type='text-private' label='Password Verification'><required/></field> </x> </command> </iq> SENT: <iq to='salem.geekplace.eu' id='f0JiQ-49' type='set'> <command xmlns='http://jabber.org/protocol/commands' node='http://jabber.org/protocol/admin#add-user' sessionid='2018-02-21T14:48:13.508812Z' action='next'> <x xmlns='jabber:x:data' type='submit'> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single'><value>smack-inttest-one-phx8g@salem.geekplace.eu</value></field> <field var='password' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> <field var='password-verify' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> </x> </command> </iq> RECV: <iq xml:lang='en' to='sinttest-admin@salem.geekplace.eu/one-phx8g' from='salem.geekplace.eu' type='error' id='f0JiQ-49'> <command action='next' sessionid='2018-02-21T14:48:13.508812Z' node='http://jabber.org/protocol/admin#add-user' xmlns='http://jabber.org/protocol/commands'> <x type='submit' xmlns='jabber:x:data'> <field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/admin</value></field> <field var='accountjid' type='jid-single'><value>smack-inttest-one-phx8g@salem.geekplace.eu</value></field> <field var='password' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> <field var='password-verify' type='text-private'><value>L2Cz2gKeVsBAEBRJ</value></field> </x> </command> <error code='400' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Unexpected action</text> </error> </iq>
2018-02-21 16:34:03 +01:00
command.execute(answerForm);
assert command.isCompleted();
}
}