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.http.cookie;
28  
29  import java.util.Locale;
30  
31  import org.apache.http.annotation.Immutable;
32  import org.apache.http.util.Args;
33  
34  /**
35   * CookieOrigin class encapsulates details of an origin server that
36   * are relevant when parsing, validating or matching HTTP cookies.
37   *
38   * @since 4.0
39   */
40  @Immutable
41  public final class CookieOrigin {
42  
43      private final String host;
44      private final int port;
45      private final String path;
46      private final boolean secure;
47  
48      public CookieOrigin(final String host, final int port, final String path, final boolean secure) {
49          super();
50          Args.notBlank(host, "Host");
51          Args.notNegative(port, "Port");
52          Args.notNull(path, "Path");
53          this.host = host.toLowerCase(Locale.ROOT);
54          this.port = port;
55          if (!path.trim().isEmpty()) {
56              this.path = path;
57          } else {
58              this.path = "/";
59          }
60          this.secure = secure;
61      }
62  
63      public String getHost() {
64          return this.host;
65      }
66  
67      public String getPath() {
68          return this.path;
69      }
70  
71      public int getPort() {
72          return this.port;
73      }
74  
75      public boolean isSecure() {
76          return this.secure;
77      }
78  
79      @Override
80      public String toString() {
81          final StringBuilder buffer = new StringBuilder();
82          buffer.append('[');
83          if (this.secure) {
84              buffer.append("(secure)");
85          }
86          buffer.append(this.host);
87          buffer.append(':');
88          buffer.append(Integer.toString(this.port));
89          buffer.append(this.path);
90          buffer.append(']');
91          return buffer.toString();
92      }
93  
94  }