001    // Copyright 2004, 2005 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.tapestry.services.impl;
016    
017    import javax.servlet.http.Cookie;
018    import javax.servlet.http.HttpServletRequest;
019    import javax.servlet.http.HttpServletResponse;
020    
021    import org.apache.tapestry.services.CookieSource;
022    
023    /**
024     * Implementation of the {@link org.apache.tapestry.services.CookieSource}service interface.
025     * 
026     * @author Howard Lewis Ship
027     * @since 4.0
028     */
029    public class CookieSourceImpl implements CookieSource
030    {
031        private HttpServletRequest _request;
032    
033        private HttpServletResponse _response;
034    
035        private int _defaultMaxAge;
036    
037        public String readCookieValue(String name)
038        {
039            Cookie[] cookies = _request.getCookies();
040    
041            if (cookies == null)
042                return null;
043    
044            for (int i = 0; i < cookies.length; i++)
045            {
046                if (cookies[i].getName().equals(name))
047                    return cookies[i].getValue();
048            }
049    
050            return null;
051        }
052    
053        public void writeCookieValue(String name, String value)
054        {
055            writeCookieValue(name, value, _defaultMaxAge);
056        }
057    
058        public void writeCookieValue(String name, String value, int maxAge)
059        {
060            Cookie cookie = new Cookie(name, value);
061            cookie.setPath(_request.getContextPath() + "/");
062            cookie.setMaxAge(maxAge);
063    
064            _response.addCookie(cookie);
065        }
066    
067        public void removeCookieValue(String name)
068        {
069            Cookie cookie = new Cookie(name, null);
070            cookie.setPath(_request.getContextPath() + "/");
071            cookie.setMaxAge(0);
072    
073            _response.addCookie(cookie);
074        }
075    
076        public void setRequest(HttpServletRequest request)
077        {
078            _request = request;
079        }
080    
081        public void setResponse(HttpServletResponse response)
082        {
083            _response = response;
084        }
085    
086        public void setDefaultMaxAge(int defaultMaxAge)
087        {
088            _defaultMaxAge = defaultMaxAge;
089        }
090    }