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 public String readCookieValue(String name) 036 { 037 Cookie[] cookies = _request.getCookies(); 038 039 if (cookies == null) 040 return null; 041 042 for (int i = 0; i < cookies.length; i++) 043 { 044 if (cookies[i].getName().equals(name)) 045 return cookies[i].getValue(); 046 } 047 048 return null; 049 } 050 051 public void writeCookieValue(String name, String value) 052 { 053 Cookie cookie = new Cookie(name, value); 054 cookie.setPath(_request.getContextPath() + "/"); 055 056 _response.addCookie(cookie); 057 } 058 059 public void removeCookieValue(String name) 060 { 061 Cookie cookie = new Cookie(name, null); 062 cookie.setPath(_request.getContextPath() + "/"); 063 cookie.setMaxAge(0); 064 065 _response.addCookie(cookie); 066 } 067 068 public void setRequest(HttpServletRequest request) 069 { 070 _request = request; 071 } 072 073 public void setResponse(HttpServletResponse response) 074 { 075 _response = response; 076 } 077 }