001 /* 002 * Copyright 1999-2002,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.latka.validators; 018 019 import org.apache.commons.latka.ValidationException; 020 021 import org.apache.commons.latka.http.Session; 022 import org.apache.commons.latka.http.Response; 023 024 /** 025 * CookieValidator validates cookie existence and/or value in an HTTP session. 026 * 027 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a> 028 * @author dIon Gillard 029 * @version $Id: CookieValidator.java 155424 2005-02-26 13:09:29Z dirkv $ 030 */ 031 public class CookieValidator extends BaseConditionalValidator { 032 033 // --------------------------------------------------------------- Attributes 034 035 protected String _cookieValue = null; 036 protected String _cookieName = null; 037 038 protected static final String MESSAGE_INVALID_TEST = "INVALID TEST: COOKIE NAME NOT SET"; 039 protected static final String BARE_MESSAGE_EXISTENT_COOKIE = " TO FIND COOKIE IN SESSION"; 040 protected static final String BARE_MESSAGE_EQUAL_VALUES = " THAT COOKIE VALUES EQUAL:"; 041 042 // needed to generate the correct exception 043 protected String _lastTestedCookieValue = null; 044 045 // ------------------------------------------------------------- Constructors 046 047 public CookieValidator() { 048 this(null,null,null,true); 049 } 050 051 public CookieValidator(String label) { 052 this(label,null,null,true); 053 } 054 055 public CookieValidator(String name, String value) { 056 this(null,name,value,true); 057 } 058 059 public CookieValidator(String label, String name, String value, boolean condition) { 060 super(label, condition); 061 _cookieName = name; 062 _cookieValue = value; 063 } 064 065 // ------------------------------------------------------------------ Methods 066 067 /** 068 * If cookie value is set, this <code>Validator</code> tests for cookie value equivalency, in addition to cookie existence 069 * 070 * @param value the value of the cookie 071 */ 072 public void setCookieValue(String value) { 073 _cookieValue = value; 074 } 075 076 /** 077 * The cookie name must be set for this <code>Validator</code> to function properly. 078 * 079 * @param name the name of the cookie 080 */ 081 public void setCookieName(String name) { 082 _cookieName = name; 083 } 084 085 /** 086 * @throws org.apache.commons.latka.ValidationException 087 * if cookie name hasn't been set, the cookie doesn't exist in the session, or if cookie value has been set and the cookie values don't match 088 */ 089 public boolean assertTrue(Response response) 090 throws ValidationException { 091 092 if (_cookieName != null) { 093 Session session = response.getRequest().getSession(); 094 095 _lastTestedCookieValue = session.getCookieValue(_cookieName); 096 097 // existence test 098 if (_lastTestedCookieValue == null) { 099 return false; 100 } 101 // value test 102 else if (_cookieValue != null) { 103 if (!_cookieValue.equals(_lastTestedCookieValue)) { 104 return false; 105 } 106 } 107 } 108 // invalid test 109 else { 110 fail(MESSAGE_INVALID_TEST); 111 } 112 113 return true; 114 } 115 116 public String generateBareExceptionMessage() { 117 118 if (_lastTestedCookieValue == null) { 119 return BARE_MESSAGE_EXISTENT_COOKIE; 120 } 121 // value test 122 else { 123 StringBuffer buffer = new StringBuffer(BARE_MESSAGE_EQUAL_VALUES); 124 buffer.append(" EXPECTED: "); 125 buffer.append(_cookieValue); 126 buffer.append(" RECEIVED: "); 127 buffer.append(_lastTestedCookieValue); 128 return buffer.toString(); 129 } 130 } 131 132 }