001: package com.meterware.httpunit.cookies;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.Iterator;
006:
007: /********************************************************************************************************************
008: * $Id: CookieProperties.java,v 1.2 2004/02/12 13:03:24 russgold Exp $
009: *
010: * Copyright (c) 2003-2004, Russell Gold
011: *
012: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
013: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
015: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
016: *
017: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
018: * of the Software.
019: *
020: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
021: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
023: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: *
026: *******************************************************************************************************************/
027:
028: /**
029: * Controls behavior for cookies.
030: */
031: public class CookieProperties {
032:
033: /** If true, domain matching follows the spec. If false, permits any domain which is a prefix of the host. **/
034: private static boolean _domainMatchingStrict = true;
035:
036: /** If true, path matching follows the spec. If false, permits any path. **/
037: private static boolean _pathMatchingStrict = true;
038:
039: /** A collection of listeners for cookie events. **/
040: private static ArrayList _listeners;
041:
042: public static void reset() {
043: _domainMatchingStrict = true;
044: _pathMatchingStrict = true;
045: _listeners = null;
046: }
047:
048: /**
049: * Returns true (the default) if cookies should be rejected if they specify a domain which is not a suffix
050: * of the host domain or does not contain all of the dots in that host domain name
051: * (see <a href="http://www.faqs.org/rfcs/rfc2965.html">RFC2965</a>).
052: */
053: public static boolean isDomainMatchingStrict() {
054: return _domainMatchingStrict;
055: }
056:
057: /**
058: * Specifies whether strict domain name matching must be followed.
059: */
060: public static void setDomainMatchingStrict(
061: boolean domainMatchingStrict) {
062: _domainMatchingStrict = domainMatchingStrict;
063: }
064:
065: /**
066: * Returns true (the default) if cookies should be rejected if they specify a path which is not a prefix
067: * of the request path (see <a href="http://www.faqs.org/rfcs/rfc2965.html">RFC2965</a>).
068: */
069: public static boolean isPathMatchingStrict() {
070: return _pathMatchingStrict;
071: }
072:
073: /**
074: * Specifies whether strict path name matching must be followed.
075: */
076: public static void setPathMatchingStrict(boolean pathMatchingStrict) {
077: _pathMatchingStrict = pathMatchingStrict;
078: }
079:
080: /**
081: * Adds a listener for cookie events.
082: */
083: public static void addCookieListener(CookieListener listener) {
084: if (_listeners == null)
085: _listeners = new ArrayList();
086: synchronized (_listeners) {
087: _listeners.add(listener);
088: }
089: }
090:
091: public static void reportCookieRejected(int reason,
092: String attribute, String source) {
093: if (_listeners == null)
094: return;
095:
096: List listeners;
097: synchronized (_listeners) {
098: listeners = (List) _listeners.clone();
099: }
100:
101: for (Iterator i = listeners.iterator(); i.hasNext();) {
102: ((CookieListener) i.next()).cookieRejected(source, reason,
103: attribute);
104: }
105: }
106: }
|