<commit_before>package org.springframework.security.config;

import org.springframework.security.ui.FilterChainOrder;
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;
import org.springframework.util.Assert;

import org.w3c.dom.Node;
import org.w3c.dom.Element;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

/**
 * Replaces a Spring bean of type "Filter" with a wrapper class which implements the <tt>Ordered</tt>
 * interface. This allows user to add their own filter to the security chain. If the user's filter
 * already implements Ordered, and no "order" attribute is specified, the filter's default order will be used.
 *
 * @author <NAME>
 * @version $Id$
 */
public class OrderedFilterBeanDefinitionDecorator implements BeanDefinitionDecorator {

    public static final String ATT_AFTER = "after";
    public static final String ATT_BEFORE = "before";

    public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
        Element elt = (Element)node;
        String order = getOrder(elt, parserContext);

        BeanDefinition filter = holder.getBeanDefinition();
        BeanDefinition wrapper = new RootBeanDefinition(OrderedFilterDecorator.class);
        wrapper.getConstructorArgumentValues().addIndexedArgumentValue(0, holder.getBeanName());
        wrapper.getConstructorArgumentValues().addIndexedArgumentValue(1, filter);

        if (StringUtils.hasText(order)) {
            wrapper.getPropertyValues().addPropertyValue("order", order);
        }

        return new BeanDefinitionHolder(wrapper, holder.getBeanName());
    }

    /**
     * Attempts to get the order of the filter by parsing the 'before' or 'after' attributes.
     */
    private String getOrder(Element elt, ParserContext pc) {
        String after = elt.getAttribute(ATT_AFTER);
        String before = elt.getAttribute(ATT_BEFORE);

        if (StringUtils.hasText(after)) {
            return Integer.toString(FilterChainOrder.getOrder(after) + 1);
        }

        if (StringUtils.hasText(before)) {
            return Integer.toString(FilterChainOrder.getOrder(before) - 1);
        }

        return null;
    }

    static class OrderedFilterDecorator implements Filter, Ordered {
        private Integer order = null;
        private Filter delegate;
        private String beanName;

        OrderedFilterDecorator(String beanName, Filter delegate) {
            this.delegate = delegate;
            this.beanName = beanName;
        }

        public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            delegate.doFilter(request, response, chain);
        }

        public final void init(FilterConfig filterConfig) throws ServletException {
            delegate.init(filterConfig);
        }

        public final void destroy() {
            delegate.destroy();
        }

        public final int getOrder() {
            if(order == null) {
                Assert.isInstanceOf(Ordered.class, "Filter '"+ beanName +"' must implement the 'Ordered' interface " +
                        " or you must specify one of the attributes '" + ATT_AFTER + "' or '" +
                        ATT_BEFORE + "' in <" + Elements.CUSTOM_FILTER +">");

                return ((Ordered)delegate).getOrder();
            }
            return order.intValue();
        }

        public final void setOrder(int order) {
            this.order = new Integer(order);
        }

        public String getBeanName() {
            return beanName;
        }
    }
}<commit_msg>Corrected check on whether delegate implements Ordered interface.<commit_after>package org.springframework.security.config;

import org.springframework.security.ui.FilterChainOrder;
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;
import org.springframework.util.Assert;

import org.w3c.dom.Node;
import org.w3c.dom.Element;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

/**
 * Replaces a Spring bean of type "Filter" with a wrapper class which implements the <tt>Ordered</tt>
 * interface. This allows user to add their own filter to the security chain. If the user's filter
 * already implements Ordered, and no "order" attribute is specified, the filter's default order will be used.
 *
 * @author <NAME>
 * @version $Id$
 */
public class OrderedFilterBeanDefinitionDecorator implements BeanDefinitionDecorator {

    public static final String ATT_AFTER = "after";
    public static final String ATT_BEFORE = "before";

    public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
        Element elt = (Element)node;
        String order = getOrder(elt, parserContext);

        BeanDefinition filter = holder.getBeanDefinition();
        BeanDefinition wrapper = new RootBeanDefinition(OrderedFilterDecorator.class);
        wrapper.getConstructorArgumentValues().addIndexedArgumentValue(0, holder.getBeanName());
        wrapper.getConstructorArgumentValues().addIndexedArgumentValue(1, filter);

        if (StringUtils.hasText(order)) {
            wrapper.getPropertyValues().addPropertyValue("order", order);
        }

        return new BeanDefinitionHolder(wrapper, holder.getBeanName());
    }

    /**
     * Attempts to get the order of the filter by parsing the 'before' or 'after' attributes.
     */
    private String getOrder(Element elt, ParserContext pc) {
        String after = elt.getAttribute(ATT_AFTER);
        String before = elt.getAttribute(ATT_BEFORE);

        if (StringUtils.hasText(after)) {
            return Integer.toString(FilterChainOrder.getOrder(after) + 1);
        }

        if (StringUtils.hasText(before)) {
            return Integer.toString(FilterChainOrder.getOrder(before) - 1);
        }

        return null;
    }

    static class OrderedFilterDecorator implements Filter, Ordered {
        private Integer order = null;
        private Filter delegate;
        private String beanName;

        OrderedFilterDecorator(String beanName, Filter delegate) {
            this.delegate = delegate;
            this.beanName = beanName;
        }

        public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            delegate.doFilter(request, response, chain);
        }

        public final void init(FilterConfig filterConfig) throws ServletException {
            delegate.init(filterConfig);
        }

        public final void destroy() {
            delegate.destroy();
        }

        public final int getOrder() {
            if(order == null) {
                Assert.isInstanceOf(Ordered.class, delegate, "Filter '"+ beanName +"' must implement the 'Ordered' interface " +
                        " or you must specify one of the attributes '" + ATT_AFTER + "' or '" +
                        ATT_BEFORE + "' in <" + Elements.CUSTOM_FILTER +">");

                return ((Ordered)delegate).getOrder();
            }
            return order.intValue();
        }

        public final void setOrder(int order) {
            this.order = new Integer(order);
        }

        public String getBeanName() {
            return beanName;
        }
    }
}
{'fraction_non_alphanumeric': 0.08891537544696067, 'fraction_numerical': 0.0014302741358760429, 'mean_word_length': 3.0812256809338523, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '145463', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2080, 'n_words': 560}