RequestAttributeCondition.java

package com.ziesemer.utils.web.addressFilter.conditions;

import javax.servlet.ServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Mark A. Ziesemer
 * 	<a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
 */
public class RequestAttributeCondition implements ICondition{
	
	protected static final Logger LOGGER = LoggerFactory.getLogger(RequestAttributeCondition.class);
	
	protected final String name;
	protected final IStringMatcher matcher;
	
	public RequestAttributeCondition(final String name, final IStringMatcher matcher){
		this.name = name;
		this.matcher = matcher;
	}
	
	public boolean checkCondition(final ServletRequest sr){
		final Object attr = sr.getAttribute(name);
		if(attr != null){
			if(attr.getClass() == String.class){
				return matcher.matches((String)attr);
			}
			LOGGER.warn("Read request attribute named {}.  Expected type String but got {}.",
				name, attr.getClass());
		}
		return false;
	}
	
	public String getName(){
		return name;
	}
	
	public IStringMatcher getMatcher(){
		return matcher;
	}
	
}