private void addFilterToChains()

in src/main/java/org/apache/sling/engine/impl/filter/ServletFilterManager.java [263:337]


    private void addFilterToChains(final Filter filter, final ServiceReference<Filter> reference) {
        final Long serviceId = (Long) reference.getProperty(Constants.SERVICE_ID);
        final MBeanReg mbeanReg = mbeanMap.get(serviceId);
        final FilterProcessorMBeanImpl mbean = mbeanReg == null ? null : mbeanReg.mbean;

        // get the order, Integer.MAX_VALUE by default
        final String orderSource;
        Object orderObj = reference.getProperty(Constants.SERVICE_RANKING);
        if (orderObj == null) {
            // filter order is defined as lower value has higher
            // priority while service ranking is the opposite In
            // addition we allow different types than Integer
            orderObj = reference.getProperty(EngineConstants.FILTER_ORDER);
            if (orderObj != null) {
                log.warn(
                        "Filter service {} is using deprecated property {}. Use {} instead.",
                        reference,
                        EngineConstants.FILTER_ORDER,
                        Constants.SERVICE_RANKING);
                // we can use 0 as the default as this will be applied
                // in the next step anyway if this props contains an
                // invalid value
                orderSource = EngineConstants.FILTER_ORDER.concat("=").concat(orderObj.toString());
                orderObj = Integer.valueOf(-1
                        * Converters.standardConverter()
                                .convert(orderObj)
                                .defaultValue(0)
                                .to(Integer.class));
            } else {
                orderSource = "none";
            }
        } else {
            orderSource = Constants.SERVICE_RANKING.concat("=").concat(orderObj.toString());
        }
        final int order = (orderObj instanceof Integer) ? ((Integer) orderObj).intValue() : 0;

        // register by scope
        Object scopeValue = reference.getProperty(EngineConstants.SLING_FILTER_SCOPE);
        if (scopeValue == null) {
            scopeValue = reference.getProperty(EngineConstants.FILTER_SCOPE);
            log.warn(
                    "Filter service {} is using deprecated property {}. Use {} instead.",
                    reference,
                    EngineConstants.FILTER_SCOPE,
                    EngineConstants.SLING_FILTER_SCOPE);
        }
        final String[] scopes =
                Converters.standardConverter().convert(scopeValue).to(String[].class);
        final FilterPredicate predicate = new FilterPredicate(reference);

        boolean used = false;
        for (String scope : scopes) {
            scope = scope.toUpperCase();
            try {
                FilterChainType type = FilterChainType.valueOf(scope.toString());
                getFilterChain(type).addFilter(filter, predicate, serviceId, order, orderSource, mbean);

                if (type == FilterChainType.COMPONENT) {
                    getFilterChain(FilterChainType.INCLUDE)
                            .addFilter(filter, predicate, serviceId, order, orderSource, mbean);
                    getFilterChain(FilterChainType.FORWARD)
                            .addFilter(filter, predicate, serviceId, order, orderSource, mbean);
                }
                used = true;
            } catch (final IllegalArgumentException iae) {
                log.warn("Filter service {} has invalid value {} for scope. Value is ignored", reference, scope);
            }
        }
        if (!used) {
            log.warn(
                    "Filter service {} has been registered without a valid {} property. The filter is ignored and not used.",
                    serviceId,
                    EngineConstants.SLING_FILTER_SCOPE);
        }
    }