Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.sling.engine.impl.filter;
 
 
 import  org.osgi.framework.BundleContext;
 import  org.osgi.framework.Constants;
 import  org.osgi.framework.ServiceReference;
 
 public class ServletFilterManager extends ServiceTracker {
 
     public static enum FilterChainType {
        
Indicates request level filters.

See also:
EngineConstants.FILTER_SCOPE_REQUEST
 
         REQUEST("Request"),

        
Indicates error level filters.

See also:
EngineConstants.FILTER_SCOPE_ERROR
 
         ERROR("Error"),

        
Indicates include level filters.

See also:
EngineConstants.FILTER_SCOPE_INCLUDE
 
         INCLUDE("Include"),

        
Indicates forward level filters.

See also:
EngineConstants.FILTER_SCOPE_FORWARD
 
         FORWARD("Forward"),

        
Indicates component level filters.

See also:
EngineConstants.FILTER_SCOPE_COMPONENT
 
         COMPONENT("Component");
 
         private final String message;
 
         private FilterChainType(final String message) {
             this. = message;
         }
 
         @Override
         public String toString() {
             return ;
         }
     }

    
The service property used by Felix's HttpService whiteboard implementation.
 
     private static String FELIX_WHITEBOARD_PATTERN_PROPERTY = "pattern";
 
     // TODO: use filter (&(objectclass=javax.servlet.Filter)(filter.scope=*))
     private static final String FILTER_SERVICE_NAME = Filter.class.getName();

    
default log
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     private final SlingServletContext servletContext;
 
    private final SlingFilterChainHelper[] filterChains;
    private final boolean compatMode;
    public ServletFilterManager(final BundleContext context,
            final SlingServletContext servletContext,
            final boolean compatMode) {
        super(contextnull);
        this. = servletContext;
        this. = new SlingFilterChainHelper[FilterChainType.values().length];
        this. = compatMode;
    }
    public SlingFilterChainHelper getFilterChain(final FilterChainType chain) {
        return [chain.ordinal()];
    }
    public Filter[] getFilters(final FilterChainType chain) {
        return getFilterChain(chain).getFilters();
    }
    @Override
    public Object addingService(ServiceReference reference) {
        Object service = super.addingService(reference);
        if (service instanceof Filter) {
            initFilter(reference, (Filterservice);
        }
        return service;
    }
    @Override
    public void modifiedService(ServiceReference referenceObject service) {
        if (service instanceof Filter) {
            destroyFilter(reference, (Filterservice);
            initFilter(reference, (Filterservice);
        }
        super.modifiedService(referenceservice);
    }
    @Override
    public void removedService(ServiceReference referenceObject service) {
        if (service instanceof Filter) {
            destroyFilter(reference, (Filterservice);
        }
        super.removedService(referenceservice);
    }

    
Check if the filter should be excluded.
    private boolean excludeFilter(final ServiceReference reference) {
        // if the service has a filter scope property, we include it
        if ( reference.getProperty(.) != null
             || reference.getProperty(.) != null ) {
            return false;
        }
        // in compat mode we allow all filters not having the felix pattern prop!
        if ( this. ) {
            // Check if filter will be registered by Felix HttpService Whiteboard
            if (reference.getProperty() != null) {
                return true;
            }
            return false;
        }
        return true;
    }
    private void initFilter(final ServiceReference reference,
            final Filter filter) {
        if ( this.excludeFilter(reference) ) {
            return;
        }
        final String filterName = SlingFilterConfig.getName(reference);
        if (filterName == null) {
            .error("initFilter: Missing name for filter {}"reference);
        } else {
            // initialize the filter first
            try {
                final FilterConfig config = new SlingFilterConfig(
                    referencefilterName);
                filter.init(config);
                // service id
                Long serviceId = (Longreference.getProperty(Constants.SERVICE_ID);
                // get the order, Integer.MAX_VALUE by default
                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(.);
                    if ( orderObj != null ) {
                        // we can use 0 as the default as this will be applied in
                        // the next step anyway if this props contains an invalid
                        // value
                        Integer order = OsgiUtil.toInteger(orderObj, 0);
                        order = order * -1;
                    }
                }
                final int order = (orderObj instanceof Integer)
                        ? ((IntegerorderObj).intValue()
                        : 0;
                // register by scope
                String[] scopes = OsgiUtil.toStringArray(
                        reference.getProperty(.), null);
                if ( scopes == null ) {
                    scopes = OsgiUtil.toStringArray(
                        reference.getProperty(.), null);
                }
                if (scopes != null && scopes.length > 0) {
                    for (String scope : scopes) {
                        scope = scope.toUpperCase();
                        try {
                            FilterChainType type = FilterChainType.valueOf(scope.toString());
                            getFilterChain(type).addFilter(filterserviceId,
                                order);
                            if (type == .) {
                                getFilterChain(.).addFilter(
                                    filterserviceIdorder);
                                getFilterChain(.).addFilter(
                                    filterserviceIdorder);
                            }
                        } catch (IllegalArgumentException iae) {
                            // TODO: log ...
                        }
                    }
                } else {
                    .warn(String.format(
                        "A Filter (Service ID %s) has been registered without a filter.scope property.",
                        reference.getProperty(Constants.SERVICE_ID)));
                    getFilterChain(.).addFilter(filter,
                        serviceIdorder);
                }
            } catch (ServletException ce) {
                .error("Filter " + filterName + " failed to initialize"ce);
            } catch (Throwable t) {
                .error("Unexpected Problem initializing ComponentFilter "
                    + ""t);
            }
        }
    }
    private void destroyFilter(final ServiceReference reference,
            final Filter filter) {
        // service id
        Object serviceId = reference.getProperty(Constants.SERVICE_ID);
        boolean removed = false;
        for (SlingFilterChainHelper filterChain : ) {
            removed |= filterChain.removeFilterById(serviceId);
        }
        // destroy it
        if (removed) {
            try {
                filter.destroy();
            } catch (Throwable t) {
                .error("Unexpected problem destroying Filter {}"filtert);
            }
        }
    }
New to GrepCode? Check out our FAQ X