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