001// Copyright 2007, 2008, 2012 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
015package org.apache.tapestry5.internal.services;
016
017import javax.servlet.http.Cookie;
018
019import org.apache.tapestry5.CookieBuilder;
020import org.apache.tapestry5.commons.util.TimeInterval;
021import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
022import org.apache.tapestry5.http.services.Request;
023import org.apache.tapestry5.ioc.annotations.IntermediateType;
024import org.apache.tapestry5.ioc.annotations.Symbol;
025import org.apache.tapestry5.services.Cookies;
026
027/**
028 * Implementation of the {@link org.apache.tapestry5.services.Cookies} service interface.
029 */
030public class CookiesImpl implements Cookies
031{
032    private final Request request;
033
034    private final CookieSource cookieSource;
035
036    private final CookieSink cookieSink;
037
038    private final String defaultCookiePath;
039
040    private final int defaultMaxAge;
041
042    /**
043     * @param request
044     * @param cookieSource
045     * @param cookieSink
046     * @param contextPath
047     * @param defaultMaxAge
048     *         default cookie expiration time in milliseconds
049     */
050    public CookiesImpl(Request request,
051
052                       CookieSource cookieSource,
053
054                       CookieSink cookieSink,
055
056                       @Symbol(TapestryHttpSymbolConstants.CONTEXT_PATH)
057                       String contextPath,
058
059                       @Symbol("tapestry.default-cookie-max-age") @IntermediateType(TimeInterval.class)
060                       long defaultMaxAge)
061    {
062        this.request = request;
063        this.cookieSource = cookieSource;
064        this.cookieSink = cookieSink;
065        this.defaultCookiePath = contextPath + "/";
066        this.defaultMaxAge = (int) (defaultMaxAge / 1000l);
067    }
068
069    public String readCookieValue(String name)
070    {
071        Cookie[] cookies = cookieSource.getCookies();
072
073        if (cookies == null) return null;
074
075        for (Cookie cooky : cookies)
076        {
077            if (cooky.getName().equals(name)) return cooky.getValue();
078        }
079
080        return null;
081    }
082
083    public void writeCookieValue(String name, String value)
084    {
085        getBuilder(name, value).write();
086    }
087
088    public void writeCookieValue(String name, String value, int maxAge)
089    {
090        getBuilder(name, value).setMaxAge(maxAge).write();
091    }
092
093    public void writeCookieValue(String name, String value, String path)
094    {
095        getBuilder(name, value).setPath(path).write();
096    }
097
098    public void writeDomainCookieValue(String name, String value, String domain)
099    {
100        getBuilder(name, value).setDomain(domain).write();
101    }
102
103    public void writeDomainCookieValue(String name, String value, String domain, int maxAge)
104    {
105        getBuilder(name, value).setDomain(domain).setMaxAge(maxAge).write();
106    }
107
108    public void writeCookieValue(String name, String value, String path, String domain)
109    {
110        getBuilder(name, value).setPath(path).setDomain(domain).write();
111    }
112
113    public void removeCookieValue(String name)
114    {
115        getBuilder(name, null).delete();
116    }
117
118    public CookieBuilder getBuilder(String name, String value)
119    {
120        CookieBuilder builder = new CookieBuilder(name, value)
121        {
122            @Override
123            public void write()
124            {
125                Cookie cookie = new Cookie(name, value);
126
127                cookie.setPath(path == null ? defaultCookiePath : path);
128
129                if(domain != null)
130                    cookie.setDomain(domain);
131
132                cookie.setMaxAge(maxAge == null ? defaultMaxAge : maxAge);
133
134                cookie.setSecure(secure == null ? request.isSecure() : secure);
135
136                cookie.setVersion(version);
137
138                if (comment != null)
139                {
140                    cookie.setComment(comment);
141                }
142
143                if (httpOnly != null)
144                {
145                    cookie.setHttpOnly(httpOnly);
146                }
147
148                cookieSink.addCookie(cookie);
149            }
150
151            @Override
152            public void delete()
153            {
154                setMaxAge(0);
155
156                write();
157            }
158        };
159
160        return builder;
161    }
162}