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 package org.apache.hc.core5.net;
29
30 import java.io.Serializable;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.Objects;
34
35 import org.apache.hc.core5.annotation.Contract;
36 import org.apache.hc.core5.annotation.ThreadingBehavior;
37 import org.apache.hc.core5.util.Args;
38 import org.apache.hc.core5.util.LangUtils;
39 import org.apache.hc.core5.util.TextUtils;
40 import org.apache.hc.core5.util.Tokenizer;
41
42
43
44
45
46
47 @Contract(threading = ThreadingBehavior.IMMUTABLE)
48 public final class URIAuthority implements NamedEndpoint, Serializable {
49
50 private static final long serialVersionUID = 1L;
51 private final String userInfo;
52 private final Host host;
53
54 static URIAuthority parse(final CharSequence s, final Tokenizer.Cursor cursor) throws URISyntaxException {
55 final Tokenizer tokenizer = Tokenizer.INSTANCE;
56 String userInfo = null;
57 final int initPos = cursor.getPos();
58 final String token = tokenizer.parseContent(s, cursor, URISupport.HOST_DELIMITERS);
59 if (!cursor.atEnd() && s.charAt(cursor.getPos()) == '@') {
60 cursor.updatePos(cursor.getPos() + 1);
61 if (!TextUtils.isBlank(token)) {
62 userInfo = token;
63 }
64 } else {
65
66 cursor.updatePos(initPos);
67 }
68 final Host host = Host.parse(s, cursor);
69 return new URIAuthority(userInfo, host);
70 }
71
72 static URIAuthority parse(final CharSequence s) throws URISyntaxException {
73 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
74 return parse(s, cursor);
75 }
76
77 static void format(final StringBuilder buf, final URIAuthority uriAuthority) {
78 if (uriAuthority.getUserInfo() != null) {
79 buf.append(uriAuthority.getUserInfo());
80 buf.append("@");
81 }
82 Host.format(buf, uriAuthority);
83 }
84
85 static String format(final URIAuthority uriAuthority) {
86 final StringBuilder buf = new StringBuilder();
87 format(buf, uriAuthority);
88 return buf.toString();
89 }
90
91
92
93
94
95
96
97
98 public URIAuthority(final String userInfo, final String hostname, final int port) {
99 super();
100 this.userInfo = userInfo;
101 this.host = new Host(hostname, port);
102 }
103
104 public URIAuthority(final String hostname, final int port) {
105 this(null, hostname, port);
106 }
107
108
109
110
111 public URIAuthority(final String userInfo, final Host host) {
112 super();
113 Args.notNull(host, "Host");
114 this.userInfo = userInfo;
115 this.host = host;
116 }
117
118
119
120
121 public URIAuthority(final Host host) {
122 this(null, host);
123 }
124
125
126
127
128 public URIAuthority(final String userInfo, final NamedEndpoint endpoint) {
129 super();
130 Args.notNull(endpoint, "Endpoint");
131 this.userInfo = userInfo;
132 this.host = new Host(endpoint.getHostName(), endpoint.getPort());
133 }
134
135 public URIAuthority(final NamedEndpoint namedEndpoint) {
136 this(null, namedEndpoint);
137 }
138
139
140
141
142
143
144
145
146 public static URIAuthority create(final String s) throws URISyntaxException {
147 if (TextUtils.isBlank(s)) {
148 return null;
149 }
150 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
151 final URIAuthority uriAuthority = parse(s, cursor);
152 if (!cursor.atEnd()) {
153 throw URISupport.createException(s, cursor, "Unexpected content");
154 }
155 return uriAuthority;
156 }
157
158 public URIAuthority(final String hostname) {
159 this(null, hostname, -1);
160 }
161
162 public String getUserInfo() {
163 return userInfo;
164 }
165
166 @Override
167 public String getHostName() {
168 return host.getHostName();
169 }
170
171 @Override
172 public int getPort() {
173 return host.getPort();
174 }
175
176 @Override
177 public String toString() {
178 return format(this);
179 }
180
181 @Override
182 public boolean equals(final Object obj) {
183 if (this == obj) {
184 return true;
185 }
186 if (obj instanceof URIAuthority) {
187 final URIAuthority that = (URIAuthority) obj;
188 return Objects.equals(this.userInfo, that.userInfo) &&
189 Objects.equals(this.host, that.host);
190 }
191 return false;
192 }
193
194 @Override
195 public int hashCode() {
196 int hash = LangUtils.HASH_SEED;
197 hash = LangUtils.hashCode(hash, userInfo);
198 hash = LangUtils.hashCode(hash, host);
199 return hash;
200 }
201
202 }