1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-14 09:01:08 +01:00

Use Jid (and subclasses) from jxmpp-jid

Fixes SMACK-634
This commit is contained in:
Florian Schmaus 2015-02-14 17:15:02 +01:00
parent 0ee2d9ed1e
commit 5bb4727c57
180 changed files with 1510 additions and 1032 deletions

View file

@ -18,6 +18,7 @@
package org.jivesoftware.smackx.address;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import org.jxmpp.jid.Jid;
import java.util.List;
@ -64,7 +65,8 @@ public class MultipleRecipientInfo {
* @return the JID of a MUC room to which responses should be sent or <tt>null</tt> if
* no specific address was provided.
*/
public String getReplyRoom() {
// TODO should return BareJid
public Jid getReplyRoom() {
List<MultipleAddresses.Address> replyRoom = extension.getAddressesOfType(MultipleAddresses.Type.replyroom);
return replyRoom.isEmpty() ? null : ((MultipleAddresses.Address) replyRoom.get(0)).getJid();
}

View file

@ -28,7 +28,10 @@ import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jxmpp.util.XmppStringUtils;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;
import java.util.ArrayList;
import java.util.Collection;
@ -66,7 +69,7 @@ public class MultipleRecipientManager {
* @throws NotConnectedException
* @throws InterruptedException
*/
public static void send(XMPPConnection connection, Stanza packet, Collection<String> to, Collection<String> cc, Collection<String> bcc) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException
public static void send(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException
{
send(connection, packet, to, cc, bcc, null, null, false);
}
@ -96,18 +99,18 @@ public class MultipleRecipientManager {
* @throws NotConnectedException
* @throws InterruptedException
*/
public static void send(XMPPConnection connection, Stanza packet, Collection<String> to, Collection<String> cc, Collection<String> bcc,
String replyTo, String replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException {
public static void send(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc,
Jid replyTo, Jid replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException {
// Check if *only* 'to' is set and contains just *one* entry, in this case extended stanzas addressing is not
// required at all and we can send it just as normal stanza without needing to add the extension element
if (to != null && to.size() == 1 && (cc == null || cc.isEmpty()) && (bcc == null || bcc.isEmpty()) && !noReply
&& StringUtils.isNullOrEmpty(replyTo) && StringUtils.isNullOrEmpty(replyRoom)) {
String toJid = to.iterator().next();
Jid toJid = to.iterator().next();
packet.setTo(toJid);
connection.sendPacket(packet);
return;
}
String serviceAddress = getMultipleRecipienServiceAddress(connection);
DomainBareJid serviceAddress = getMultipleRecipienServiceAddress(connection);
if (serviceAddress != null) {
// Send packet to target users using multiple recipient service provided by the server
sendThroughService(connection, packet, to, cc, bcc, replyTo, replyRoom, noReply,
@ -115,8 +118,8 @@ public class MultipleRecipientManager {
}
else {
// Server does not support XEP-33 so try to send the packet to each recipient
if (noReply || (replyTo != null && replyTo.trim().length() > 0) ||
(replyRoom != null && replyRoom.trim().length() > 0)) {
if (noReply || replyTo != null ||
replyRoom != null) {
// Some specified XEP-33 features were requested so throw an exception alerting
// the user that this features are not available
throw new FeatureNotSupportedException("Extended Stanza Addressing");
@ -162,8 +165,8 @@ public class MultipleRecipientManager {
}
else {
// Send reply to multiple recipients
List<String> to = new ArrayList<String>(info.getTOAddresses().size());
List<String> cc = new ArrayList<String>(info.getCCAddresses().size());
List<Jid> to = new ArrayList<>(info.getTOAddresses().size());
List<Jid> cc = new ArrayList<>(info.getCCAddresses().size());
for (MultipleAddresses.Address jid : info.getTOAddresses()) {
to.add(jid.getJid());
}
@ -175,9 +178,9 @@ public class MultipleRecipientManager {
to.add(original.getFrom());
}
// Remove the sender from the TO/CC list (try with bare JID too)
String from = connection.getUser();
FullJid from = connection.getUser();
if (!to.remove(from) && !cc.remove(from)) {
String bareJID = XmppStringUtils.parseBareJid(from);
BareJid bareJID = from.asBareJid();
to.remove(bareJID);
cc.remove(bareJID);
}
@ -202,44 +205,44 @@ public class MultipleRecipientManager {
}
private static void sendToIndividualRecipients(XMPPConnection connection, Stanza packet,
Collection<String> to, Collection<String> cc, Collection<String> bcc) throws NotConnectedException, InterruptedException {
Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc) throws NotConnectedException, InterruptedException {
if (to != null) {
for (String jid : to) {
for (Jid jid : to) {
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
if (cc != null) {
for (String jid : cc) {
for (Jid jid : cc) {
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
if (bcc != null) {
for (String jid : bcc) {
for (Jid jid : bcc) {
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
}
private static void sendThroughService(XMPPConnection connection, Stanza packet, Collection<String> to,
Collection<String> cc, Collection<String> bcc, String replyTo, String replyRoom, boolean noReply,
String serviceAddress) throws NotConnectedException, InterruptedException {
private static void sendThroughService(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to,
Collection<? extends Jid> cc, Collection<? extends Jid> bcc, Jid replyTo, Jid replyRoom, boolean noReply,
DomainBareJid serviceAddress) throws NotConnectedException, InterruptedException {
// Create multiple recipient extension
MultipleAddresses multipleAddresses = new MultipleAddresses();
if (to != null) {
for (String jid : to) {
for (Jid jid : to) {
multipleAddresses.addAddress(MultipleAddresses.Type.to, jid, null, null, false, null);
}
}
if (cc != null) {
for (String jid : cc) {
for (Jid jid : cc) {
multipleAddresses.addAddress(MultipleAddresses.Type.to, jid, null, null, false, null);
}
}
if (bcc != null) {
for (String jid : bcc) {
for (Jid jid : bcc) {
multipleAddresses.addAddress(MultipleAddresses.Type.bcc, jid, null, null, false, null);
}
}
@ -247,11 +250,11 @@ public class MultipleRecipientManager {
multipleAddresses.setNoReply();
}
else {
if (replyTo != null && replyTo.trim().length() > 0) {
if (replyTo != null) {
multipleAddresses
.addAddress(MultipleAddresses.Type.replyto, replyTo, null, null, false, null);
}
if (replyRoom != null && replyRoom.trim().length() > 0) {
if (replyRoom != null) {
multipleAddresses.addAddress(MultipleAddresses.Type.replyroom, replyRoom, null, null,
false, null);
}
@ -278,9 +281,9 @@ public class MultipleRecipientManager {
* @throws NotConnectedException
* @throws InterruptedException
*/
private static String getMultipleRecipienServiceAddress(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
private static DomainBareJid getMultipleRecipienServiceAddress(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
List<String> services = sdm.findServices(MultipleAddresses.NAMESPACE, true, true);
List<DomainBareJid> services = sdm.findServices(MultipleAddresses.NAMESPACE, true, true);
if (services.size() > 0) {
return services.get(0);
}

View file

@ -20,6 +20,7 @@ package org.jivesoftware.smackx.address.packet;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.Jid;
import java.util.ArrayList;
import java.util.List;
@ -64,7 +65,7 @@ public class MultipleAddresses implements PacketExtension {
* @param delivered true when the packet was already delivered to this address.
* @param uri used to specify an external system address, such as a sip:, sips:, or im: URI.
*/
public void addAddress(Type type, String jid, String node, String desc, boolean delivered,
public void addAddress(Type type, Jid jid, String node, String desc, boolean delivered,
String uri) {
// Create a new address with the specificed configuration
Address address = new Address(type);
@ -132,7 +133,7 @@ public class MultipleAddresses implements PacketExtension {
public static final String ELEMENT = "address";
private final Type type;
private String jid;
private Jid jid;
private String node;
private String description;
private boolean delivered;
@ -146,11 +147,11 @@ public class MultipleAddresses implements PacketExtension {
return type;
}
public String getJid() {
public Jid getJid() {
return jid;
}
private void setJid(String jid) {
private void setJid(Jid jid) {
this.jid = jid;
}

View file

@ -20,8 +20,10 @@ package org.jivesoftware.smackx.address.provider;
import java.io.IOException;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import org.jivesoftware.smackx.address.packet.MultipleAddresses.Type;
import org.jxmpp.jid.Jid;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -46,7 +48,7 @@ public class MultipleAddressesProvider extends PacketExtensionProvider<MultipleA
case MultipleAddresses.Address.ELEMENT:
String typeString = parser.getAttributeValue("", "type");
Type type = Type.valueOf(typeString);
String jid = parser.getAttributeValue("", "jid");
Jid jid = ParserUtils.getJidAttribute(parser, "jid");
String node = parser.getAttributeValue("", "node");
String desc = parser.getAttributeValue("", "desc");
boolean delivered = "true".equals(parser.getAttributeValue("", "delivered"));