LocalAddrCondition.java

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

import java.net.InetAddress;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletRequest;

/**
 * @author Mark A. Ziesemer
 * 	<a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
 */
public class LocalAddrCondition implements ICondition{
	
	protected final List<IAddressChecker> checkers;
	
	public LocalAddrCondition(final List<IAddressChecker> checkers){
		this.checkers = checkers;
	}
	
	public boolean checkCondition(final ServletRequest sr) throws Exception{
		final InetAddress ip = InetAddress.getByName(sr.getLocalAddr());
		final String hostName = sr.getLocalName();
		for(final IAddressChecker checker : checkers){
			if(checker.matches(ip, hostName)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * <p>
	 * 	Returns a read-only view of the checkers checked against, primarily for unit testing.
	 * </p>
	 */
	public List<IAddressChecker> getCheckers(){
		return Collections.unmodifiableList(checkers);
	}
	
}