Home | History | Annotate | Download | only in cookie
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/cookie/CookieOrigin.java $
      3  * $Revision: 653041 $
      4  * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 package org.apache.http.cookie;
     32 
     33 import java.util.Locale;
     34 
     35 /**
     36  * CookieOrigin class incapsulates details of an origin server that
     37  * are relevant when parsing, validating or matching HTTP cookies.
     38  *
     39  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     40  *
     41  * @since 4.0
     42  */
     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, int port, final String path, boolean secure) {
     51         super();
     52         if (host == null) {
     53             throw new IllegalArgumentException(
     54                     "Host of origin may not be null");
     55         }
     56         if (host.trim().length() == 0) {
     57             throw new IllegalArgumentException(
     58                     "Host of origin may not be blank");
     59         }
     60         if (port < 0) {
     61             throw new IllegalArgumentException("Invalid port: " + port);
     62         }
     63         if (path == null) {
     64             throw new IllegalArgumentException(
     65                     "Path of origin may not be null.");
     66         }
     67         this.host = host.toLowerCase(Locale.ENGLISH);
     68         this.port = port;
     69         if (path.trim().length() != 0) {
     70             this.path = path;
     71         } else {
     72             this.path = "/";
     73         }
     74         this.secure = secure;
     75     }
     76 
     77     public String getHost() {
     78         return this.host;
     79     }
     80 
     81     public String getPath() {
     82         return this.path;
     83     }
     84 
     85     public int getPort() {
     86         return this.port;
     87     }
     88 
     89     public boolean isSecure() {
     90         return this.secure;
     91     }
     92 
     93     @Override
     94     public String toString() {
     95         StringBuilder buffer = new StringBuilder();
     96         buffer.append('[');
     97         if (this.secure) {
     98             buffer.append("(secure)");
     99         }
    100         buffer.append(this.host);
    101         buffer.append(':');
    102         buffer.append(Integer.toString(this.port));
    103         buffer.append(this.path);
    104         buffer.append(']');
    105         return buffer.toString();
    106     }
    107 
    108 }
    109