/
HostAndPort.java
87 lines (76 loc) · 2.35 KB
/
HostAndPort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package io.vertx.core.net;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.impl.HostAndPortImpl;
/**
* A combination of host and port.
*/
@DataObject
public interface HostAndPort {
static HostAndPort fromJson(JsonObject json) {
int port = json.getInteger("port", -1);
String host = json.getString("host");
if (host != null) {
return HostAndPort.create(host, port);
}
return null;
}
/**
* Create an arbitrary instance.
*
* @param host the host value
* @param port the port value
* @return the instance.
*/
static HostAndPort create(String host, int port) {
return new HostAndPortImpl(host, port);
}
/**
* Parse an authority HTTP header, that is <i>host [':' port]</i>, according to
* <a href="https://datatracker.ietf.org/doc/html/rfc3986#appendix-A">rfc3986</a>.
*
* @param string the string to parse
* @param schemePort the scheme port used when the optional port is not specified
* @return the parsed authority or {@code null} when the {@code string} does not represent a valid authority.
*/
static HostAndPort parseAuthority(String string, int schemePort) {
return HostAndPortImpl.parseAuthority(string, schemePort);
}
/**
* Create an instance with a valid {@code host} for a valid authority: the {@code host} must
* match the <i>host</i> rule of <a href="https://datatracker.ietf.org/doc/html/rfc3986#appendix-A">rfc3986</a>.
*
* @param host the host portion
* @param port the port
* @return the instance
*/
static HostAndPort authority(String host, int port) {
if (!HttpUtils.isValidHostAuthority(host)) {
throw new IllegalArgumentException("Invalid authority host portion: " + host);
}
return new HostAndPortImpl(host, port);
}
/**
* Like {@link #authority(String, int)} without a port, {@code -1} is used instead.
*/
static HostAndPort authority(String host) {
return authority(host, -1);
}
/**
* @return the host value
*/
String host();
/**
* @return the port value or {@code -1} when not specified
*/
int port();
default JsonObject toJson() {
JsonObject json = new JsonObject().put("host", host());
int port = port();
if (port > 0) {
json.put("port", port);
}
return json;
}
}