1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 02:39:42 +02:00

Add StreamFeatureProvider

so that AccountManager and the CapsExtension can be moved to
smack-extensions, where they belong.
This commit is contained in:
Florian Schmaus 2014-09-11 23:58:07 +02:00
parent fc51f3df48
commit 46a4402a69
22 changed files with 396 additions and 151 deletions

View file

@ -26,7 +26,6 @@ import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.CapsExtension;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
@ -39,6 +38,7 @@ import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.jivesoftware.smackx.disco.NodeInformationProvider;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;

View file

@ -0,0 +1,84 @@
/**
*
* Copyright © 2009 Jonas Ådahl, 2011-2014 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.caps.packet;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* A XEP-0115 Entity Capabilities extension.
* <p>
* Note that this is currently in smack-core as it's a potential stream feature.
* TODO: In feature versions of Smack, it should be possible to register
* "providers" for stream features too, so that this class can be moved back to
* smack-extensions.
* </p>
*/
public class CapsExtension implements PacketExtension {
public static final String NAMESPACE = "http://jabber.org/protocol/caps";
public static final String ELEMENT = "c";
private final String node, ver, hash;
public CapsExtension(String node, String version, String hash) {
this.node = node;
this.ver = version;
this.hash = hash;
}
public String getElementName() {
return ELEMENT;
}
public String getNamespace() {
return NAMESPACE;
}
public String getNode() {
return node;
}
public String getVer() {
return ver;
}
public String getHash() {
return hash;
}
/**
* <pre>
* <c xmlns='http://jabber.org/protocol/caps'
* hash='sha-1'
* node='http://code.google.com/p/exodus'
* ver='QgayPKawpkPSDYmwT/WM94uAlu0='/>
* </pre>
*
*/
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("hash", hash).attribute("node", node).attribute("ver", ver);
xml.closeEmptyElement();
return xml;
}
public static CapsExtension from(Packet stanza) {
return stanza.getExtension(ELEMENT, NAMESPACE);
}
}

View file

@ -19,14 +19,15 @@ package org.jivesoftware.smackx.caps.provider;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.CapsExtension;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.StreamFeatureProvider;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class CapsExtensionProvider implements PacketExtensionProvider {
public class CapsExtensionProvider implements PacketExtensionProvider, StreamFeatureProvider {
public PacketExtension parseExtension(XmlPullParser parser) throws XmlPullParserException, IOException,
SmackException {
@ -55,4 +56,10 @@ public class CapsExtensionProvider implements PacketExtensionProvider {
throw new SmackException("Caps elment with missing attributes");
}
}
@Override
public PacketExtension parseStreamFeature(XmlPullParser parser) throws XmlPullParserException,
IOException, SmackException {
return parseExtension(parser);
}
}

View file

@ -0,0 +1,297 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.iqregister;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.jxmpp.util.XmppStringUtils;
/**
* Allows creation and management of accounts on an XMPP server.
*
* @author Matt Tucker
*/
public class AccountManager extends Manager {
private static final Map<XMPPConnection, AccountManager> INSTANCES = new WeakHashMap<XMPPConnection, AccountManager>();
/**
* Returns the AccountManager instance associated with a given XMPPConnection.
*
* @param connection the connection used to look for the proper ServiceDiscoveryManager.
* @return the AccountManager associated with a given XMPPConnection.
*/
public static synchronized AccountManager getInstance(XMPPConnection connection) {
AccountManager accountManager = INSTANCES.get(connection);
if (accountManager == null) {
accountManager = new AccountManager(connection);
INSTANCES.put(connection, accountManager);
}
return accountManager;
}
private Registration info = null;
/**
* Flag that indicates whether the server supports In-Band Registration.
* In-Band Registration may be advertised as a stream feature. If no stream feature
* was advertised from the server then try sending an IQ packet to discover if In-Band
* Registration is available.
*/
private boolean accountCreationSupported = false;
/**
* Creates a new AccountManager instance.
*
* @param connection a connection to a XMPP server.
*/
private AccountManager(XMPPConnection connection) {
super(connection);
}
/**
* Sets whether the server supports In-Band Registration. In-Band Registration may be
* advertised as a stream feature. If no stream feature was advertised from the server
* then try sending an IQ packet to discover if In-Band Registration is available.
*
* @param accountCreationSupported true if the server supports In-Band Registration.
*/
void setSupportsAccountCreation(boolean accountCreationSupported) {
this.accountCreationSupported = accountCreationSupported;
}
/**
* Returns true if the server supports creating new accounts. Many servers require
* that you not be currently authenticated when creating new accounts, so the safest
* behavior is to only create new accounts before having logged in to a server.
*
* @return true if the server support creating new accounts.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public boolean supportsAccountCreation() throws NoResponseException, XMPPErrorException, NotConnectedException {
// Check if we already know that the server supports creating new accounts
if (accountCreationSupported) {
return true;
}
// No information is known yet (e.g. no stream feature was received from the server
// indicating that it supports creating new accounts) so send an IQ packet as a way
// to discover if this feature is supported
if (info == null) {
getRegistrationInfo();
accountCreationSupported = info.getType() != IQ.Type.error;
}
return accountCreationSupported;
}
/**
* Returns an unmodifiable collection of the names of the required account attributes.
* All attributes must be set when creating new accounts. The standard set of possible
* attributes are as follows: <ul>
* <li>name -- the user's name.
* <li>first -- the user's first name.
* <li>last -- the user's last name.
* <li>email -- the user's email address.
* <li>city -- the user's city.
* <li>state -- the user's state.
* <li>zip -- the user's ZIP code.
* <li>phone -- the user's phone number.
* <li>url -- the user's website.
* <li>date -- the date the registration took place.
* <li>misc -- other miscellaneous information to associate with the account.
* <li>text -- textual information to associate with the account.
* <li>remove -- empty flag to remove account.
* </ul><p>
*
* Typically, servers require no attributes when creating new accounts, or just
* the user's email address.
*
* @return the required account attributes.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public Collection<String> getAccountAttributes() throws NoResponseException, XMPPErrorException, NotConnectedException {
if (info == null) {
getRegistrationInfo();
}
Map<String, String> attributes = info.getAttributes();
if (attributes != null) {
return Collections.unmodifiableSet(attributes.keySet());
} else {
return Collections.emptySet();
}
}
/**
* Returns the value of a given account attribute or <tt>null</tt> if the account
* attribute wasn't found.
*
* @param name the name of the account attribute to return its value.
* @return the value of the account attribute or <tt>null</tt> if an account
* attribute wasn't found for the requested name.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public String getAccountAttribute(String name) throws NoResponseException, XMPPErrorException, NotConnectedException {
if (info == null) {
getRegistrationInfo();
}
return info.getAttributes().get(name);
}
/**
* Returns the instructions for creating a new account, or <tt>null</tt> if there
* are no instructions. If present, instructions should be displayed to the end-user
* that will complete the registration process.
*
* @return the account creation instructions, or <tt>null</tt> if there are none.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public String getAccountInstructions() throws NoResponseException, XMPPErrorException, NotConnectedException {
if (info == null) {
getRegistrationInfo();
}
return info.getInstructions();
}
/**
* Creates a new account using the specified username and password. The server may
* require a number of extra account attributes such as an email address and phone
* number. In that case, Smack will attempt to automatically set all required
* attributes with blank values, which may or may not be accepted by the server.
* Therefore, it's recommended to check the required account attributes and to let
* the end-user populate them with real values instead.
*
* @param username the username.
* @param password the password.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public void createAccount(String username, String password) throws NoResponseException, XMPPErrorException, NotConnectedException {
// Create a map for all the required attributes, but give them blank values.
Map<String, String> attributes = new HashMap<String, String>();
for (String attributeName : getAccountAttributes()) {
attributes.put(attributeName, "");
}
createAccount(username, password, attributes);
}
/**
* Creates a new account using the specified username, password and account attributes.
* The attributes Map must contain only String name/value pairs and must also have values
* for all required attributes.
*
* @param username the username.
* @param password the password.
* @param attributes the account attributes.
* @throws XMPPErrorException if an error occurs creating the account.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @see #getAccountAttributes()
*/
public void createAccount(String username, String password, Map<String, String> attributes)
throws NoResponseException, XMPPErrorException, NotConnectedException {
attributes.put("username", username);
attributes.put("password", password);
Registration reg = new Registration(attributes);
reg.setType(IQ.Type.set);
reg.setTo(connection().getServiceName());
createPacketCollectorAndSend(reg).nextResultOrThrow();
}
/**
* Changes the password of the currently logged-in account. This operation can only
* be performed after a successful login operation has been completed. Not all servers
* support changing passwords; an XMPPException will be thrown when that is the case.
*
* @throws IllegalStateException if not currently logged-in to the server.
* @throws XMPPErrorException if an error occurs when changing the password.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void changePassword(String newPassword) throws NoResponseException, XMPPErrorException, NotConnectedException {
Map<String, String> map = new HashMap<String, String>();
map.put("username",XmppStringUtils.parseLocalpart(connection().getUser()));
map.put("password",newPassword);
Registration reg = new Registration(map);
reg.setType(IQ.Type.set);
reg.setTo(connection().getServiceName());
createPacketCollectorAndSend(reg).nextResultOrThrow();
}
/**
* Deletes the currently logged-in account from the server. This operation can only
* be performed after a successful login operation has been completed. Not all servers
* support deleting accounts; an XMPPException will be thrown when that is the case.
*
* @throws IllegalStateException if not currently logged-in to the server.
* @throws XMPPErrorException if an error occurs when deleting the account.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void deleteAccount() throws NoResponseException, XMPPErrorException, NotConnectedException {
Map<String, String> attributes = new HashMap<String, String>();
// To delete an account, we add a single attribute, "remove", that is blank.
attributes.put("remove", "");
Registration reg = new Registration(attributes);
reg.setType(IQ.Type.set);
reg.setTo(connection().getServiceName());
createPacketCollectorAndSend(reg).nextResultOrThrow();
}
/**
* Gets the account registration info from the server.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*
* @throws XMPPException if an error occurs.
* @throws SmackException if there was no response from the server.
*/
private synchronized void getRegistrationInfo() throws NoResponseException, XMPPErrorException, NotConnectedException {
Registration reg = new Registration();
reg.setTo(connection().getServiceName());
info = createPacketCollectorAndSend(reg).nextResultOrThrow();
}
private PacketCollector createPacketCollectorAndSend(IQ req) throws NotConnectedException {
PacketCollector collector = connection().createPacketCollector(new PacketIDFilter(req.getPacketID()));
connection().sendPacket(req);
return collector;
}
}

View file

@ -0,0 +1,133 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.iqregister.packet;
import java.util.Map;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents registration packets. An empty GET query will cause the server to return information
* about it's registration support. SET queries can be used to create accounts or update
* existing account information. XMPP servers may require a number of attributes to be set
* when creating a new account. The standard account attributes are as follows:
* <ul>
* <li>name -- the user's name.
* <li>first -- the user's first name.
* <li>last -- the user's last name.
* <li>email -- the user's email address.
* <li>city -- the user's city.
* <li>state -- the user's state.
* <li>zip -- the user's ZIP code.
* <li>phone -- the user's phone number.
* <li>url -- the user's website.
* <li>date -- the date the registration took place.
* <li>misc -- other miscellaneous information to associate with the account.
* <li>text -- textual information to associate with the account.
* <li>remove -- empty flag to remove account.
* </ul>
*
* @author Matt Tucker
*/
public class Registration extends IQ {
public static final String NAMESPACE = "jabber:iq:register";
private final String instructions;
private final Map<String, String> attributes;
public Registration() {
this(null);
}
public Registration(Map<String, String> attributes) {
this(null, attributes);
}
public Registration(String instructions, Map<String, String> attributes) {
this.instructions = instructions;
this.attributes = attributes;
}
/**
* Returns the registration instructions, or <tt>null</tt> if no instructions
* have been set. If present, instructions should be displayed to the end-user
* that will complete the registration process.
*
* @return the registration instructions, or <tt>null</tt> if there are none.
*/
public String getInstructions() {
return instructions;
}
/**
* Returns the map of String key/value pairs of account attributes.
*
* @return the account attributes.
*/
public Map<String, String> getAttributes() {
return attributes;
}
@Override
public XmlStringBuilder getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(QUERY_ELEMENT);
xml.xmlnsAttribute(NAMESPACE);
xml.rightAngleBracket();
xml.optElement("instructions", instructions);
if (attributes != null && attributes.size() > 0) {
for (String name : attributes.keySet()) {
String value = attributes.get(name);
xml.element(name, value);
}
}
// Add packet extensions, if any are defined.
xml.append(getExtensionsXML());
xml.closeElement(QUERY_ELEMENT);
return xml;
}
public static class Feature implements PacketExtension {
public static final String ELEMENT = "register";
public static final String NAMESPACE = "http://jabber.org/features/iq-register";
public static final Feature INSTANCE = new Registration.Feature();
private Feature() {
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public CharSequence toXML() {
return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
}
@Override
public String getNamespace() {
return NAMESPACE;
}
}
}

View file

@ -0,0 +1,77 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.iqregister.provider;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.xmlpull.v1.XmlPullParser;
public class RegistrationProvider implements IQProvider {
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
String instruction = null;
Map<String, String> fields = new HashMap<String, String>();
List<PacketExtension> packetExtensions = new LinkedList<PacketExtension>();
outerloop:
while (true) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
// Any element that's in the jabber:iq:register namespace,
// attempt to parse it if it's in the form <name>value</name>.
if (parser.getNamespace().equals(Registration.NAMESPACE)) {
String name = parser.getName();
String value = "";
if (parser.next() == XmlPullParser.TEXT) {
value = parser.getText();
}
// Ignore instructions, but anything else should be added to the map.
if (!name.equals("instructions")) {
fields.put(name, value);
}
else {
instruction = value;
}
}
// Otherwise, it must be a packet extension.
else {
packetExtensions.add(PacketParserUtils.parsePacketExtension(
parser.getName(), parser.getNamespace(),
parser));
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals(IQ.QUERY_ELEMENT)) {
break outerloop;
}
}
}
Registration registration = new Registration(instruction, fields);
registration.addExtensions(packetExtensions);
return registration;
}
}

View file

@ -0,0 +1,31 @@
/**
*
* Copyright © 2014 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.iqregister.provider;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.StreamFeatureProvider;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.xmlpull.v1.XmlPullParser;
public class RegistrationStreamFeatureProvider implements StreamFeatureProvider {
@Override
public PacketExtension parseStreamFeature(XmlPullParser parser) {
return Registration.Feature.INSTANCE;
}
}

View file

@ -59,12 +59,12 @@ import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.NodeInformationProvider;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.iqregister.packet.Registration;
import org.jivesoftware.smackx.muc.packet.Destroy;
import org.jivesoftware.smackx.muc.packet.MUCAdmin;
import org.jivesoftware.smackx.muc.packet.MUCInitialPresence;

View file

@ -421,6 +421,12 @@
<className>org.jivesoftware.smackx.caps.provider.CapsExtensionProvider</className>
</extensionProvider>
<streamFeatureProvider>
<elementName>c</elementName>
<namespace>http://jabber.org/protocol/caps</namespace>
<className>org.jivesoftware.smackx.caps.provider.CapsExtensionProvider</className>
</streamFeatureProvider>
<!-- XEP-0297 Stanza Forwarding -->
<extensionProvider>
<elementName>forwarded</elementName>
@ -455,4 +461,17 @@
<namespace>http://www.jivesoftware.com/xmlns/xmpp/properties</namespace>
<className>org.jivesoftware.smackx.jiveproperties.provider.JivePropertiesExtensionProvider</className>
</extensionProvider>
<!-- XEP-0077: In-Band Registration -->
<iqProvider>
<elementName>query</elementName>
<namespace>jabber:iq:register</namespace>
<className>org.jivesoftware.smackx.iqregister.provider.RegistrationProvider</className>
</iqProvider>
<streamFeatureProvider>
<elementName>register</elementName>
<namespace>http://jabber.org/features/iq-register</namespace>
<className>org.jivesoftware.smackx.iqregister.provider.RegistrationStreamFeatureProvider</className>
</streamFeatureProvider>
</smackProviders>