<commit_before>/**
 * Created on 2008-8-6 12:16:34
 */
package cn.net.openid.jos.web.filter;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.net.openid.jos.domain.Domain;

/**
 * Domain filter. Parse domain information from request URL, and put into
 * session and request.
 * 
 * @author <NAME>
 * 
 */
public class DomainFilter extends OncePerRequestServiceFilter {
	private static final Log log = LogFactory.getLog(DomainFilter.class);
	private static final String DOMAIN_ATTRIBUTE_NAME = "domain";

	private Pattern skipPattern;

	/**
	 * @param skipPattern
	 *            the skipPattern to set
	 */
	public void setSkipPattern(String skipPattern) {
		log.debug("skipPattern setted: " + skipPattern.trim());
		this.skipPattern = Pattern.compile(skipPattern.trim());
	}

	/**
	 * Get domain from <code>request.getSession(false)</code>.
	 * 
	 * @param request
	 *            the HTTP request
	 * @return the domain in the session, null if session is null or not found.
	 * @see DomainFilter#getDomain(HttpSession)
	 */
	public static Domain getDomain(HttpServletRequest request) {
		return getDomain(request.getSession(false));
	}

	/**
	 * Get domain from the session/request.
	 * 
	 * @param session
	 *            the HTTP session
	 * @return the domain in the session, null if session is null or not found.
	 */
	public static Domain getDomain(HttpSession session) {
		return session != null ? checkRuntime((Domain) session
				.getAttribute(DOMAIN_ATTRIBUTE_NAME)) : null;
	}

	/**
	 * Check the domain runtime.
	 * <p>
	 * As {@link Domain#getRuntime()} is transient, we should check if it is
	 * null before using. Otherwise, if the <code>domain</code> is restored from
	 * a stream, the {@link Domain#getRuntime()} will be null. For example, <a
	 * href="http://tomcat.apache.org/">Apache Tomcat</a> usually write the
	 * objects in HTTP sessions while stopping though serialization mechanism,
	 * and restore them while re-starting.
	 * </p>
	 * 
	 * @param domain
	 *            the domain to check runtime
	 * @return null if runtime is null, else itself.
	 * @see java.io.Serializable
	 */
	private static Domain checkRuntime(Domain domain) {
		return domain.getRuntime() != null ? domain : null;
	}

	/*<commit_msg>Null check for checkRuntime(Domain).<commit_after>/**
 * Created on 2008-8-6 12:16:34
 */
package cn.net.openid.jos.web.filter;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.net.openid.jos.domain.Domain;

/**
 * Domain filter. Parse domain information from request URL, and put into
 * session and request.
 * 
 * @author <NAME>
 * 
 */
public class DomainFilter extends OncePerRequestServiceFilter {
	private static final Log log = LogFactory.getLog(DomainFilter.class);
	private static final String DOMAIN_ATTRIBUTE_NAME = "domain";

	private Pattern skipPattern;

	/**
	 * @param skipPattern
	 *            the skipPattern to set
	 */
	public void setSkipPattern(String skipPattern) {
		log.debug("skipPattern setted: " + skipPattern.trim());
		this.skipPattern = Pattern.compile(skipPattern.trim());
	}

	/**
	 * Get domain from <code>request.getSession(false)</code>.
	 * 
	 * @param request
	 *            the HTTP request
	 * @return the domain in the session, null if session is null or not found.
	 * @see DomainFilter#getDomain(HttpSession)
	 */
	public static Domain getDomain(HttpServletRequest request) {
		return getDomain(request.getSession(false));
	}

	/**
	 * Get domain from the session/request.
	 * 
	 * @param session
	 *            the HTTP session
	 * @return the domain in the session, null if session is null or not found.
	 */
	public static Domain getDomain(HttpSession session) {
		return session != null ? checkRuntime((Domain) session
				.getAttribute(DOMAIN_ATTRIBUTE_NAME)) : null;
	}

	/**
	 * Check the domain runtime.
	 * <p>
	 * As {@link Domain#getRuntime()} is transient, we should check if it is
	 * null before using. Otherwise, if the <code>domain</code> is restored from
	 * a stream, the {@link Domain#getRuntime()} will be null. For example, <a
	 * href="http://tomcat.apache.org/">Apache Tomcat</a> usually write the
	 * objects in HTTP sessions while stopping though serialization mechanism,
	 * and restore them while re-starting.
	 * </p>
	 * 
	 * @param domain
	 *            the domain to check runtime
	 * @return null if runtime is null, else itself.
	 * @see java.io.Serializable
	 */
	private static Domain checkRuntime(Domain domain) {
		return (domain != null && domain.getRuntime() != null) ? domain : null;
	}

	/*
{'fraction_non_alphanumeric': 0.1091652072387624, 'fraction_numerical': 0.004670169293636894, 'mean_word_length': 3.719926538108356, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741103', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1437, 'n_words': 465}