001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.protocol.http.mock; 018 019import java.io.Serializable; 020import java.util.Objects; 021 022import javax.servlet.http.Cookie; 023 024import org.apache.wicket.util.lang.Args; 025 026/** 027 * A helper class for dealing with cookies 028 */ 029public final class Cookies 030{ 031 /** 032 * Constructor. 033 */ 034 private Cookies() 035 { 036 } 037 038 /** 039 * Make a copy of the passed cookie. 040 * 041 * @param cookie 042 * The cookie to copy 043 * @return A copy of the passed cookie. May be {@code null} if the argument is {@code null}. 044 */ 045 public static Cookie copyOf(Cookie cookie) 046 { 047 return cookie != null ? (Cookie) cookie.clone() : null; 048 } 049 050 /** 051 * creates a key based on the property for cookie equality 052 * 053 * @param cookie 054 * cookie 055 * @return key 056 */ 057 public static Key keyOf(Cookie cookie) 058 { 059 return new Key(cookie); 060 } 061 062 /** 063 * Checks whether two cookies are equal. 064 * See http://www.ietf.org/rfc/rfc2109.txt, p.4.3.3 065 * 066 * @param c1 067 * the first cookie 068 * @param c2 069 * the second cookie 070 * @return {@code true} only if the cookies have the same name, path and domain 071 */ 072 public static boolean isEqual(Cookie c1, Cookie c2) 073 { 074 Args.notNull(c1, "c1"); 075 Args.notNull(c2, "c2"); 076 077 return new Key(c1).equals(new Key(c2)); 078 } 079 080 /** 081 * detect if this cookie is expired 082 * 083 * @param cookie 084 * @return 085 */ 086 public static boolean isExpired(Cookie cookie) 087 { 088 return cookie.getMaxAge() == 0; 089 } 090 091 public static class Key implements Serializable 092 { 093 094 private final String name; 095 private final String path; 096 private final String domain; 097 098 protected Key(Cookie cookie) 099 { 100 Args.notNull(cookie, "cookie"); 101 name = cookie.getName(); 102 path = cookie.getPath(); 103 domain = cookie.getDomain(); 104 } 105 106 @Override 107 public int hashCode() 108 { 109 final int prime = 31; 110 int result = 1; 111 result = prime * result + ((domain == null) ? 0 : domain.hashCode()); 112 result = prime * result + ((name == null) ? 0 : name.hashCode()); 113 result = prime * result + ((path == null) ? 0 : path.hashCode()); 114 return result; 115 } 116 117 @Override 118 public boolean equals(Object obj) 119 { 120 if (this == obj) 121 return true; 122 if (obj == null) 123 return false; 124 if (getClass() != obj.getClass()) 125 return false; 126 Key other = (Key)obj; 127 128 return Objects.equals(domain, other.domain) 129 && Objects.equals(name, other.name) 130 && Objects.equals(path, other.path); 131 } 132 133 @Override 134 public String toString() 135 { 136 return name + ";" + domain + "/" + path; 137 } 138 139 } 140 141}