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 org.apache.tapestry.services.CookieSource;
018
019 import javax.servlet.http.Cookie;
020 import javax.servlet.http.HttpServletRequest;
021 import javax.servlet.http.HttpServletResponse;
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 writeCookieValue(String name, String value, String path)
068 {
069 Cookie cookie = new Cookie(name, value);
070 cookie.setPath(path);
071 _response.addCookie(cookie);
072 }
073
074 public void writeDomainCookieValue(String name, String value, String domain)
075 {
076 Cookie cookie = new Cookie(name, value);
077 cookie.setPath(_request.getContextPath() + "/");
078 cookie.setDomain(domain);
079 _response.addCookie(cookie);
080 }
081
082 public void writeDomainCookieValue(String name, String value, String domain, int maxAge)
083 {
084 Cookie cookie = new Cookie(name, value);
085 cookie.setPath(_request.getContextPath() + "/");
086 cookie.setDomain(domain);
087 cookie.setMaxAge(maxAge);
088
089 _response.addCookie(cookie);
090 }
091
092 public void writeCookieValue(String name, String value, String path, String domain)
093 {
094 Cookie cookie = new Cookie(name, value);
095 cookie.setPath(path);
096 cookie.setDomain(domain);
097 _response.addCookie(cookie);
098 }
099
100 public void removeCookieValue(String name)
101 {
102 Cookie cookie = new Cookie(name, null);
103 cookie.setPath(_request.getContextPath() + "/");
104 cookie.setMaxAge(0);
105
106 _response.addCookie(cookie);
107 }
108
109 public void setRequest(HttpServletRequest request)
110 {
111 _request = request;
112 }
113
114 public void setResponse(HttpServletResponse response)
115 {
116 _response = response;
117 }
118
119 public void setDefaultMaxAge(int defaultMaxAge)
120 {
121 _defaultMaxAge = defaultMaxAge;
122 }
123 }