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