mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 09:09:38 +02:00
Add support for XEP-0133: Service Administration
also extend Smack's integration test framework to use XEP-0133 as a means for of throw away account creation. Fixes SMACK-742
This commit is contained in:
parent
1f1bc236fd
commit
274e5630c4
12 changed files with 414 additions and 56 deletions
|
@ -0,0 +1,117 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 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.jivesoftware.smackx.xdata.FormField;
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.jid.util.JidUtil;
|
||||
|
||||
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().getServiceName());
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
FormField accountJidField = answerForm.getField("accountjid");
|
||||
accountJidField.addValue(userJid.toString());
|
||||
|
||||
FormField passwordField = answerForm.getField("password");
|
||||
passwordField.addValue(password);
|
||||
|
||||
FormField passwordVerifyField = answerForm.getField("password-verify");
|
||||
passwordVerifyField.addValue(password);
|
||||
|
||||
command.next(answerForm);
|
||||
assert(command.isCompleted());
|
||||
}
|
||||
|
||||
public RemoteCommand deleteUser() {
|
||||
return deleteUser(connection().getServiceName());
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
FormField accountJids = answerForm.getField("accountjids");
|
||||
accountJids.addValues(JidUtil.toStringList(jidsToDelete));
|
||||
|
||||
command.next(answerForm);
|
||||
assert(command.isCompleted());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smack's API for XEP-0133: Service Administration.
|
||||
*/
|
||||
package org.jivesoftware.smackx.admin;
|
|
@ -326,6 +326,16 @@ public abstract class AdHocCommand {
|
|||
return data.getStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this command has been completed successfully.
|
||||
*
|
||||
* @return <code>true</code> if this command is completed.
|
||||
* @since 4.2
|
||||
*/
|
||||
public boolean isCompleted() {
|
||||
return getStatus() == Status.completed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the data of the current stage. This should not used.
|
||||
*
|
||||
|
|
|
@ -143,11 +143,18 @@ public class RemoteCommand extends AdHocCommand {
|
|||
data.setForm(form.getDataFormToSend());
|
||||
}
|
||||
|
||||
AdHocCommandData responseData = (AdHocCommandData) connection.createPacketCollectorAndSend(
|
||||
data).nextResultOrThrow();
|
||||
AdHocCommandData responseData = null;
|
||||
try {
|
||||
responseData = connection.createPacketCollectorAndSend(data).nextResultOrThrow();
|
||||
}
|
||||
finally {
|
||||
// We set the response data in a 'finally' block, so that it also gets set even if an error IQ was returned.
|
||||
if (responseData != null) {
|
||||
this.sessionID = responseData.getSessionID();
|
||||
super.setData(responseData);
|
||||
}
|
||||
}
|
||||
|
||||
this.sessionID = responseData.getSessionID();
|
||||
super.setData(responseData);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -32,8 +32,10 @@ import org.jivesoftware.smack.SmackException.NoResponseException;
|
|||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.filter.StanzaIdFilter;
|
||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.iqregister.packet.Registration;
|
||||
import org.jxmpp.jid.parts.Localpart;
|
||||
|
||||
|
@ -116,6 +118,7 @@ public final class AccountManager extends Manager {
|
|||
*
|
||||
* @param accountCreationSupported true if the server supports In-Band Registration.
|
||||
*/
|
||||
// TODO: Remove this method and the accountCreationSupported boolean.
|
||||
void setSupportsAccountCreation(boolean accountCreationSupported) {
|
||||
this.accountCreationSupported = accountCreationSupported;
|
||||
}
|
||||
|
@ -132,6 +135,8 @@ public final class AccountManager extends Manager {
|
|||
* @throws InterruptedException
|
||||
*/
|
||||
public boolean supportsAccountCreation() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
||||
// TODO: Replace this body with isSupported() and possible deprecate this method.
|
||||
|
||||
// Check if we already know that the server supports creating new accounts
|
||||
if (accountCreationSupported) {
|
||||
return true;
|
||||
|
@ -326,6 +331,18 @@ public final class AccountManager extends Manager {
|
|||
createPacketCollectorAndSend(reg).nextResultOrThrow();
|
||||
}
|
||||
|
||||
public boolean isSupported()
|
||||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
||||
XMPPConnection connection = connection();
|
||||
|
||||
ExtensionElement extensionElement = connection.getFeature(Registration.ELEMENT, Registration.NAMESPACE);
|
||||
if (extensionElement != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection).serverSupportsFeature(Registration.NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the account registration info from the server.
|
||||
* @throws XMPPErrorException
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue