001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.cookie; 028 029import java.util.Locale; 030 031import org.apache.http.annotation.Contract; 032import org.apache.http.annotation.ThreadingBehavior; 033import org.apache.http.util.Args; 034import org.apache.http.util.TextUtils; 035 036/** 037 * CookieOrigin class encapsulates details of an origin server that 038 * are relevant when parsing, validating or matching HTTP cookies. 039 * 040 * @since 4.0 041 */ 042@Contract(threading = ThreadingBehavior.IMMUTABLE) 043public final class CookieOrigin { 044 045 private final String host; 046 private final int port; 047 private final String path; 048 private final boolean secure; 049 050 public CookieOrigin(final String host, final int port, final String path, final boolean secure) { 051 super(); 052 Args.notBlank(host, "Host"); 053 Args.notNegative(port, "Port"); 054 Args.notNull(path, "Path"); 055 this.host = host.toLowerCase(Locale.ROOT); 056 this.port = port; 057 if (!TextUtils.isBlank(path)) { 058 this.path = path; 059 } else { 060 this.path = "/"; 061 } 062 this.secure = secure; 063 } 064 065 public String getHost() { 066 return this.host; 067 } 068 069 public String getPath() { 070 return this.path; 071 } 072 073 public int getPort() { 074 return this.port; 075 } 076 077 public boolean isSecure() { 078 return this.secure; 079 } 080 081 @Override 082 public String toString() { 083 final StringBuilder buffer = new StringBuilder(); 084 buffer.append('['); 085 if (this.secure) { 086 buffer.append("(secure)"); 087 } 088 buffer.append(this.host); 089 buffer.append(':'); 090 buffer.append(Integer.toString(this.port)); 091 buffer.append(this.path); 092 buffer.append(']'); 093 return buffer.toString(); 094 } 095 096}