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  package org.apache.hc.core5.net;
28  
29  import java.io.Serializable;
30  import java.net.URISyntaxException;
31  import java.util.Locale;
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  
39  /**
40   * Component that holds all details needed to describe a network connection
41   * to a host. This includes remote host name and port.
42   *
43   * @since 5.0
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE)
46  public final class Host implements NamedEndpoint, Serializable {
47  
48      private static final long serialVersionUID = 1L;
49      private final String name;
50      private final String lcName;
51      private final int port;
52  
53      public Host(final String name, final int port) {
54          super();
55          this.name   = Args.containsNoBlanks(name, "Host name");
56          this.port = Ports.check(port);
57          this.lcName = this.name.toLowerCase(Locale.ROOT);
58      }
59  
60      public static Host create(final String s) throws URISyntaxException {
61          Args.notEmpty(s, "HTTP Host");
62          final int portIdx = s.lastIndexOf(":");
63          final int port;
64          if (portIdx > 0) {
65              try {
66                  port = Integer.parseInt(s.substring(portIdx + 1));
67              } catch (final NumberFormatException ex) {
68                  throw new URISyntaxException(s, "invalid port");
69              }
70              final String hostname = s.substring(0, portIdx);
71              if (TextUtils.containsBlanks(hostname)) {
72                  throw new URISyntaxException(s, "hostname contains blanks");
73              }
74              return new Host(hostname, port);
75          }
76          throw new URISyntaxException(s, "port not found");
77      }
78  
79      @Override
80      public String getHostName() {
81          return name;
82      }
83  
84      @Override
85      public int getPort() {
86          return port;
87      }
88  
89      @Override
90      public boolean equals(final Object o) {
91          if (this == o) {
92              return true;
93          }
94          if (o instanceof Host) {
95              final Hostef="../../../../../org/apache/hc/core5/net/Host.html#Host">Host that = (Host) o;
96              return this.lcName.equals(that.lcName) && this.port == that.port;
97          }
98          return false;
99      }
100 
101     @Override
102     public int hashCode() {
103         int hash = LangUtils.HASH_SEED;
104         hash = LangUtils.hashCode(hash, this.lcName);
105         hash = LangUtils.hashCode(hash, this.port);
106         return hash;
107     }
108 
109     @Override
110     public String toString() {
111         return name + ":" + port;
112     }
113 
114 }