• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

igniterealtime / Smack / #2856

pending completion
#2856

push

github-actions

web-flow
Merge pull request #561 from Flowdalic/github-ci

[github ci] Java 15 → 17

16274 of 41793 relevant lines covered (38.94%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.68
/smack-core/src/main/java/org/jivesoftware/smack/util/InternetAddress.java
1
/**
2
 *
3
 * Copyright 2019-2023 Florian Schmaus
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.jivesoftware.smack.util;
18

19
import java.net.Inet4Address;
20
import java.net.Inet6Address;
21
import java.net.InetAddress;
22
import java.net.UnknownHostException;
23

24
import org.minidns.dnslabel.DnsLabel;
25
import org.minidns.dnsname.DnsName;
26
import org.minidns.dnsname.InvalidDnsNameException;
27
import org.minidns.util.InetAddressUtil;
28

29
/**
30
 * An internet address, can be given as IP or as DNS name.
31
 * <p>
32
 * This type is meant for strings that hold an internet address. The original string used to construct this type is
33
 * stored and returning in the {@link #toString()} method.
34
 * </p>
35
 *
36
 * @since 4.4.0
37
 */
38
public abstract class InternetAddress implements CharSequence {
39

40
    protected final String originalString;
41

42
    protected InternetAddress(String originalString) {
1✔
43
        this.originalString = Objects.requireNonNull(originalString, "The 'originalString' argument must not be null");
1✔
44
    }
1✔
45

46
    public abstract InetAddress asInetAddress() throws UnknownHostException;
47

48
    @Override
49
    public String toString() {
50
        return originalString;
1✔
51
    }
52

53
    @Override
54
    public int length() {
55
        return originalString.length();
×
56
    }
57

58
    @Override
59
    public char charAt(int index) {
60
        return originalString.charAt(index);
×
61
    }
62

63
    @Override
64
    public CharSequence subSequence(int start, int end) {
65
        return originalString.subSequence(start, end);
×
66
    }
67

68
    public String getRaw() {
69
        return originalString;
×
70
    }
71

72
    public static InternetAddress fromIgnoringZoneId(String address) {
73
        return from(address, true);
1✔
74
    }
75

76
    public static InternetAddress from(String address) {
77
        return from(address, false);
1✔
78
    }
79

80
    private static InternetAddress from(String address, boolean ignoreZoneId) {
81
        String raw = address;
1✔
82
        if (ignoreZoneId) {
1✔
83
            int percentPosition = address.indexOf('%');
1✔
84
            if (percentPosition > 1) {
1✔
85
                address = address.substring(0, percentPosition);
1✔
86
            }
87
        }
88

89
        final InternetAddress internetAddress;
90
        if (InetAddressUtil.isIpV4Address(address)) {
1✔
91
            internetAddress = new InternetAddress.Ipv4(address, raw);
1✔
92
        } else if (InetAddressUtil.isIpV6Address(address)) {
1✔
93
            internetAddress = new InternetAddress.Ipv6(address, raw);
1✔
94
        } else if (address.contains(".")) {
1✔
95
            InternetAddress domainNameInternetAddress;
96
            try {
97
                DnsName dnsName = DnsName.from(address);
1✔
98
                domainNameInternetAddress = new InternetAddress.DomainName(address, dnsName);
1✔
99
            } catch (InvalidDnsNameException e) {
×
100
                domainNameInternetAddress = new InternetAddress.InvalidDomainName(address, e);
×
101
            }
1✔
102
            internetAddress = domainNameInternetAddress;
1✔
103
        } else {
1✔
104
            DnsLabel dnsLabel = DnsLabel.from(address);
1✔
105
            internetAddress = new InternetAddress.DomainNameLabel(address, dnsLabel);
1✔
106
        }
107
        return internetAddress;
1✔
108
    }
109

110
    public static InternetAddress from(InetAddress inetAddress) {
111
        if (inetAddress instanceof Inet4Address) {
1✔
112
            return new InternetAddress.Ipv4(inetAddress.getHostAddress(), (Inet4Address) inetAddress);
1✔
113
        } else if (inetAddress instanceof Inet6Address) {
1✔
114
            return new InternetAddress.Ipv6(inetAddress.getHostAddress(), (Inet6Address) inetAddress);
1✔
115
        } else {
116
            throw new IllegalArgumentException("Unknown type " + inetAddress.getClass() + " of " + inetAddress);
×
117
        }
118
    }
119

120
    private static class InetAddressInternetAddress extends InternetAddress {
121
        private final InetAddress inetAddress;
122
        private final String raw;
123

124
        protected InetAddressInternetAddress(String originalString, String raw, InetAddress inetAddress) {
125
            super(originalString);
1✔
126
            this.raw = raw;
1✔
127
            this.inetAddress = inetAddress;
1✔
128
        }
1✔
129

130
        @Override
131
        public InetAddress asInetAddress() {
132
            return inetAddress;
1✔
133
        }
134

135
        @Override
136
        public final String getRaw() {
137
            return raw;
1✔
138
        }
139
    }
140

141
    public static final class Ipv4 extends InetAddressInternetAddress {
142

143
        private final Inet4Address inet4Address;
144

145
        private Ipv4(String originalString, String raw) {
146
            this(originalString, raw, InetAddressUtil.ipv4From(originalString));
1✔
147
        }
1✔
148

149
        private Ipv4(String originalString, Inet4Address inet4Address) {
150
            this(originalString, originalString, inet4Address);
1✔
151
        }
1✔
152

153
        private Ipv4(String originalString, String raw, Inet4Address inet4Address) {
154
            super(originalString, raw, inet4Address);
1✔
155
            this.inet4Address = inet4Address;
1✔
156
        }
1✔
157

158
        public Inet4Address getInet4Address() {
159
            return inet4Address;
×
160
        }
161
    }
162

163
    public static final class Ipv6 extends InetAddressInternetAddress {
164

165
        private Inet6Address inet6Address;
166

167
        private Ipv6(String originalString, String raw) {
168
            this(originalString, raw, InetAddressUtil.ipv6From(originalString));
1✔
169
        }
1✔
170

171
        private Ipv6(String originalString, Inet6Address inet6Address) {
172
            this(originalString, originalString, inet6Address);
1✔
173
        }
1✔
174

175
        private Ipv6(String originalString, String raw, Inet6Address inet6Address) {
176
            super(originalString, raw, inet6Address);
1✔
177
            this.inet6Address = inet6Address;
1✔
178
        }
1✔
179

180
        public Inet6Address getInet6Address() {
181
            return inet6Address;
×
182
        }
183
    }
184

185
    private static class NonNumericInternetAddress extends InternetAddress {
186
        private boolean attemptedToResolveInetAddress;
187
        private InetAddress inetAddress;
188

189
        protected NonNumericInternetAddress(String originalString) {
190
            super(originalString);
1✔
191
        }
1✔
192

193
        @Override
194
        public InetAddress asInetAddress() throws UnknownHostException {
195
            if (inetAddress != null || attemptedToResolveInetAddress) {
×
196
                return inetAddress;
×
197
            }
198

199
            attemptedToResolveInetAddress = true;
×
200
            inetAddress = InetAddress.getByName(originalString);
×
201

202
            return inetAddress;
×
203
        }
204
    }
205

206
    public static final class DomainName extends NonNumericInternetAddress {
207

208
        private final DnsName dnsName;
209

210
        private DomainName(String originalString, DnsName dnsName) {
211
            super(originalString);
1✔
212
            this.dnsName = dnsName;
1✔
213
        }
1✔
214

215
        public DnsName getDnsName() {
216
            return dnsName;
×
217
        }
218

219
    }
220

221
    public static final class DomainNameLabel extends NonNumericInternetAddress {
222

223
        private final DnsLabel dnsLabel;
224

225
        private DomainNameLabel(String originalString, DnsLabel dnsLabel) {
226
            super(originalString);
1✔
227
            this.dnsLabel = dnsLabel;
1✔
228
        }
1✔
229

230
        public DnsLabel getDnsLabel() {
231
            return dnsLabel;
×
232
        }
233
    }
234

235
    public static final class InvalidDomainName extends NonNumericInternetAddress {
236

237
        private final InvalidDnsNameException invalidDnsNameException;
238

239
        private InvalidDomainName(String originalString, InvalidDnsNameException invalidDnsNameException) {
240
            super(originalString);
×
241
            this.invalidDnsNameException = invalidDnsNameException;
×
242
        }
×
243

244
        public InvalidDnsNameException getInvalidDnsNameException() {
245
            return invalidDnsNameException;
×
246
        }
247
    }
248
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc