1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-25 12:08:00 +01:00

[core] Add InternetAddress.fromIgnoringZoneId(String)

This commit is contained in:
Florian Schmaus 2023-02-12 16:06:53 +01:00
parent 5295a856e4
commit dee34b5efc
3 changed files with 89 additions and 11 deletions

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2019 Florian Schmaus * Copyright 2019-2023 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,12 +65,32 @@ public abstract class InternetAddress implements CharSequence {
return originalString.subSequence(start, end); return originalString.subSequence(start, end);
} }
public String getRaw() {
return originalString;
}
public static InternetAddress fromIgnoringZoneId(String address) {
return from(address, true);
}
public static InternetAddress from(String address) { public static InternetAddress from(String address) {
return from(address, false);
}
private static InternetAddress from(String address, boolean ignoreZoneId) {
String raw = address;
if (ignoreZoneId) {
int percentPosition = address.indexOf('%');
if (percentPosition > 1) {
address = address.substring(0, percentPosition);
}
}
final InternetAddress internetAddress; final InternetAddress internetAddress;
if (InetAddressUtil.isIpV4Address(address)) { if (InetAddressUtil.isIpV4Address(address)) {
internetAddress = new InternetAddress.Ipv4(address); internetAddress = new InternetAddress.Ipv4(address, raw);
} else if (InetAddressUtil.isIpV6Address(address)) { } else if (InetAddressUtil.isIpV6Address(address)) {
internetAddress = new InternetAddress.Ipv6(address); internetAddress = new InternetAddress.Ipv6(address, raw);
} else if (address.contains(".")) { } else if (address.contains(".")) {
InternetAddress domainNameInternetAddress; InternetAddress domainNameInternetAddress;
try { try {
@ -99,9 +119,11 @@ public abstract class InternetAddress implements CharSequence {
private static class InetAddressInternetAddress extends InternetAddress { private static class InetAddressInternetAddress extends InternetAddress {
private final InetAddress inetAddress; private final InetAddress inetAddress;
private final String raw;
protected InetAddressInternetAddress(String originalString, InetAddress inetAddress) { protected InetAddressInternetAddress(String originalString, String raw, InetAddress inetAddress) {
super(originalString); super(originalString);
this.raw = raw;
this.inetAddress = inetAddress; this.inetAddress = inetAddress;
} }
@ -109,18 +131,27 @@ public abstract class InternetAddress implements CharSequence {
public InetAddress asInetAddress() { public InetAddress asInetAddress() {
return inetAddress; return inetAddress;
} }
@Override
public final String getRaw() {
return raw;
}
} }
public static final class Ipv4 extends InetAddressInternetAddress { public static final class Ipv4 extends InetAddressInternetAddress {
private final Inet4Address inet4Address; private final Inet4Address inet4Address;
private Ipv4(String originalString) { private Ipv4(String originalString, String raw) {
this(originalString, InetAddressUtil.ipv4From(originalString)); this(originalString, raw, InetAddressUtil.ipv4From(originalString));
} }
private Ipv4(String originalString, Inet4Address inet4Address) { private Ipv4(String originalString, Inet4Address inet4Address) {
super(originalString, inet4Address); this(originalString, originalString, inet4Address);
}
private Ipv4(String originalString, String raw, Inet4Address inet4Address) {
super(originalString, raw, inet4Address);
this.inet4Address = inet4Address; this.inet4Address = inet4Address;
} }
@ -133,12 +164,16 @@ public abstract class InternetAddress implements CharSequence {
private Inet6Address inet6Address; private Inet6Address inet6Address;
private Ipv6(String originalString) { private Ipv6(String originalString, String raw) {
this(originalString, InetAddressUtil.ipv6From(originalString)); this(originalString, raw, InetAddressUtil.ipv6From(originalString));
} }
private Ipv6(String originalString, Inet6Address inet6Address) { private Ipv6(String originalString, Inet6Address inet6Address) {
super(originalString, inet6Address); this(originalString, originalString, inet6Address);
}
private Ipv6(String originalString, String raw, Inet6Address inet6Address) {
super(originalString, raw, inet6Address);
this.inet6Address = inet6Address; this.inet6Address = inet6Address;
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2014-2019 Florian Schmaus * Copyright © 2014-2023 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -387,4 +387,12 @@ public class ParserUtils {
public static QName getQName(XmlPullParser parser) { public static QName getQName(XmlPullParser parser) {
return parser.getQName(); return parser.getQName();
} }
public static InternetAddress getInternetAddressIngoringZoneIdAttribute(XmlPullParser parser, String attribute) {
String inetAddressString = parser.getAttributeValue(attribute);
if (inetAddressString == null) {
return null;
}
return InternetAddress.fromIgnoringZoneId(inetAddressString);
}
} }

View file

@ -0,0 +1,35 @@
/**
*
* Copyright 2023 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.smack.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class InternetAddressTest {
@Test
public void testFromIgnoringZoneId() {
assertInternetAddressEqualsIgnoringZoneId("fe80::641a:cdff:febd:d665", "fe80::641a:cdff:febd:d665%dummy0");
}
private static void assertInternetAddressEqualsIgnoringZoneId(String expected, String input) {
InternetAddress internetAddress = InternetAddress.fromIgnoringZoneId(input);
assertEquals(expected, internetAddress.toString());
assertEquals(input, internetAddress.getRaw());
}
}