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.util.ArrayList;
020import java.util.Arrays;
021import java.util.LinkedHashMap;
022import java.util.List;
023import java.util.Map;
024
025import javax.servlet.http.Cookie;
026
027import org.apache.wicket.protocol.http.mock.Cookies.Key;
028
029/**
030 * cookie collection utility
031 * @author mosmann
032 */
033public class CookieCollection
034{
035        private final Map<Cookies.Key, Cookie> cookies = new LinkedHashMap<Cookies.Key, Cookie>();
036        private final Map<Cookies.Key, Cookie> expiredCookies = new LinkedHashMap<Cookies.Key, Cookie>();
037
038        /**
039         * add cookie to collection
040         *   if cookie is expired, it will be moved to expired cookie set
041         *   overwrite existing cookie with new value 
042         * @param cookie a cookie
043         */
044        public void add(Cookie cookie)
045        {
046                Key key = Cookies.keyOf(cookie);
047                Cookie copyOfCookie = Cookies.copyOf(cookie);
048                if (Cookies.isExpired(cookie))
049                {
050                        expiredCookies.put(key, copyOfCookie);
051                        cookies.remove(key);
052                }
053                else
054                {
055                        cookies.put(key, copyOfCookie);
056                }
057        }
058
059        /**
060         * calls add on each cookie
061         * @param cookies array of cookies
062         */
063        public void addAll(Cookie[] cookies)
064        {
065                if (cookies != null)
066                {
067                        addAll(Arrays.asList(cookies));
068                }
069        }
070
071        /**
072         * calls add on each cookie
073         * @param cookies list of cookies
074         */
075        public void addAll(List<Cookie> cookies)
076        {
077                for (Cookie cookie : cookies)
078                {
079                        add(cookie);
080                }
081        }
082
083        /**
084         * list of non expired cookies
085         * @return as list
086         */
087        public List<Cookie> asList()
088        {
089                ArrayList<Cookie> ret = new ArrayList<Cookie>();
090                ret.addAll(cookies.values());
091                return ret;
092        }
093
094        /**
095         * list of expired cookies
096         * @return as list
097         */
098        public List<Cookie> expiredAsList()
099        {
100                ArrayList<Cookie> ret = new ArrayList<Cookie>();
101                ret.addAll(expiredCookies.values());
102                return ret;
103        }
104
105        /**
106         * list of all cookies, expired or not
107         * @return as list
108         */
109        public List<Cookie> allAsList()
110        {
111                ArrayList<Cookie> ret = new ArrayList<Cookie>();
112                ret.addAll(cookies.values());
113                ret.addAll(expiredCookies.values());
114                return ret;
115        }
116
117}