View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Represents authority component of request {@link URI}.
44   *
45   * @since 5.0
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              //Rewind
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       * Constructs a new instance.
93       *
94       * @throws IllegalArgumentException
95       *             If the port parameter is outside the specified range of valid port values, which is between 0 and
96       *             65535, inclusive. {@code -1} indicates the scheme default port.
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      * @since 5.2
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      * @since 5.2
120      */
121     public URIAuthority(final Host host) {
122         this(null, host);
123     }
124 
125     /**
126      * @since 5.2
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      * Creates a {@code URIAuthority} instance from a string. Text may not contain any blanks.
141      *
142      * @param s The value to parse
143      * @return The parsed URIAuthority.
144      * @throws URISyntaxException Thrown if a string could not be parsed as a URIAuthority.
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 }