001: // Copyright 2007 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.internal.services;
016:
017: import javax.servlet.http.Cookie;
018:
019: import org.apache.tapestry.ioc.annotations.Symbol;
020: import org.apache.tapestry.services.Cookies;
021: import org.apache.tapestry.services.Request;
022:
023: /**
024: * Implementation of the {@link org.apache.tapestry.services.Cookies} service interface.
025: */
026: public class CookiesImpl implements Cookies {
027: private Request _request;
028:
029: private CookieSource _cookieSource;
030:
031: private CookieSink _cookieSink;
032:
033: private int _defaultMaxAge;
034:
035: public CookiesImpl(Request request,
036:
037: CookieSource cookieSource,
038:
039: CookieSink cookieSink,
040:
041: @Symbol("tapestry.default-cookie-max-age")
042: int defaultMaxAge) {
043: _request = request;
044: _cookieSource = cookieSource;
045: _cookieSink = cookieSink;
046: _defaultMaxAge = defaultMaxAge;
047: }
048:
049: public String readCookieValue(String name) {
050: Cookie[] cookies = _cookieSource.getCookies();
051:
052: if (cookies == null)
053: return null;
054:
055: for (int i = 0; i < cookies.length; i++) {
056: if (cookies[i].getName().equals(name))
057: return cookies[i].getValue();
058: }
059:
060: return null;
061: }
062:
063: public void writeCookieValue(String name, String value) {
064: writeCookieValue(name, value, _defaultMaxAge);
065: }
066:
067: public void writeCookieValue(String name, String value, int maxAge) {
068: Cookie cookie = new Cookie(name, value);
069: cookie.setPath(_request.getContextPath() + "/");
070: cookie.setMaxAge(maxAge);
071:
072: _cookieSink.addCookie(cookie);
073: }
074:
075: public void writeCookieValue(String name, String value, String path) {
076: Cookie cookie = new Cookie(name, value);
077: cookie.setPath(path);
078:
079: _cookieSink.addCookie(cookie);
080: }
081:
082: public void writeDomainCookieValue(String name, String value,
083: String domain) {
084: Cookie cookie = new Cookie(name, value);
085: cookie.setPath(_request.getContextPath() + "/");
086: cookie.setDomain(domain);
087:
088: _cookieSink.addCookie(cookie);
089: }
090:
091: public void writeDomainCookieValue(String name, String value,
092: String domain, int maxAge) {
093: Cookie cookie = new Cookie(name, value);
094: cookie.setPath(_request.getContextPath() + "/");
095: cookie.setDomain(domain);
096: cookie.setMaxAge(maxAge);
097:
098: _cookieSink.addCookie(cookie);
099: }
100:
101: public void writeCookieValue(String name, String value,
102: String path, String domain) {
103: Cookie cookie = new Cookie(name, value);
104: cookie.setPath(path);
105: cookie.setDomain(domain);
106:
107: _cookieSink.addCookie(cookie);
108: }
109:
110: public void removeCookieValue(String name) {
111: Cookie cookie = new Cookie(name, null);
112: cookie.setPath(_request.getContextPath() + "/");
113: cookie.setMaxAge(0);
114:
115: _cookieSink.addCookie(cookie);
116: }
117:
118: }
|