001    // Copyright 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.ioc.annotations.IntermediateType;
018    import org.apache.tapestry5.ioc.annotations.Symbol;
019    import org.apache.tapestry5.ioc.util.TimeInterval;
020    import org.apache.tapestry5.services.Cookies;
021    import org.apache.tapestry5.services.Request;
022    
023    import javax.servlet.http.Cookie;
024    
025    /**
026     * Implementation of the {@link org.apache.tapestry5.services.Cookies} service interface.
027     */
028    public class CookiesImpl implements Cookies
029    {
030        private final Request request;
031    
032        private final CookieSource cookieSource;
033    
034        private final CookieSink cookieSink;
035    
036        private final int defaultMaxAge;
037    
038        /**
039         * @param request
040         * @param cookieSource
041         * @param cookieSink
042         * @param defaultMaxAge default cookie expiration time in milliseconds
043         */
044        public CookiesImpl(Request request,
045    
046                           CookieSource cookieSource,
047    
048                           CookieSink cookieSink,
049    
050                           @Symbol("tapestry.default-cookie-max-age") @IntermediateType(TimeInterval.class)
051                           long defaultMaxAge)
052        {
053            this.request = request;
054            this.cookieSource = cookieSource;
055            this.cookieSink = cookieSink;
056            this.defaultMaxAge = (int) (defaultMaxAge / 1000l);
057        }
058    
059        public String readCookieValue(String name)
060        {
061            Cookie[] cookies = cookieSource.getCookies();
062    
063            if (cookies == null) return null;
064    
065            for (Cookie cooky : cookies)
066            {
067                if (cooky.getName().equals(name)) return cooky.getValue();
068            }
069    
070            return null;
071        }
072    
073        public void writeCookieValue(String name, String value)
074        {
075            writeCookieValue(name, value, defaultMaxAge);
076        }
077    
078        public void writeCookieValue(String name, String value, int maxAge)
079        {
080            Cookie cookie = new Cookie(name, value);
081            cookie.setPath(request.getContextPath() + "/");
082            cookie.setMaxAge(maxAge);
083            cookie.setSecure(request.isSecure());
084    
085            cookieSink.addCookie(cookie);
086        }
087    
088        public void writeCookieValue(String name, String value, String path)
089        {
090            Cookie cookie = new Cookie(name, value);
091            cookie.setPath(path);
092            cookie.setMaxAge(defaultMaxAge);
093            cookie.setSecure(request.isSecure());
094    
095            cookieSink.addCookie(cookie);
096        }
097    
098        public void writeDomainCookieValue(String name, String value, String domain)
099        {
100            writeDomainCookieValue(name, value, domain, defaultMaxAge);
101        }
102    
103        public void writeDomainCookieValue(String name, String value, String domain, int maxAge)
104        {
105            Cookie cookie = new Cookie(name, value);
106            cookie.setPath(request.getContextPath() + "/");
107            cookie.setDomain(domain);
108            cookie.setMaxAge(maxAge);
109            cookie.setSecure(request.isSecure());
110    
111            cookieSink.addCookie(cookie);
112        }
113    
114        public void writeCookieValue(String name, String value, String path, String domain)
115        {
116            Cookie cookie = new Cookie(name, value);
117            cookie.setPath(path);
118            cookie.setDomain(domain);
119            cookie.setMaxAge(defaultMaxAge);
120            cookie.setSecure(request.isSecure());
121    
122            cookieSink.addCookie(cookie);
123        }
124    
125        public void removeCookieValue(String name)
126        {
127            Cookie cookie = new Cookie(name, null);
128            cookie.setPath(request.getContextPath() + "/");
129            cookie.setMaxAge(0);
130            cookie.setSecure(request.isSecure());
131    
132            cookieSink.addCookie(cookie);
133        }
134    }