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.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   * Represents authority component of request {@link java.net.URI}.
42   *
43   * @since 5.0
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              //Rewind
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       * @throws IllegalArgumentException
91       *             If the port parameter is outside the specified range of valid port values, which is between 0 and
92       *             65535, inclusive. {@code -1} indicates the scheme default port.
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      * @since 5.2
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      * @since 5.2
116      */
117     public URIAuthority(final Host host) {
118         this(null, host);
119     }
120 
121     /**
122      * @since 5.2
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      * Creates {@code URIHost} instance from string. Text may not contain any blanks.
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 }