| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>IniFilterChainResolverFactory.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Test Coverage</a> > <a href="../index.html" class="el_bundle">shiro-web</a> > <a href="index.source.html" class="el_package">org.apache.shiro.web.config</a> > <span class="el_source">IniFilterChainResolverFactory.java</span></div><h1>IniFilterChainResolverFactory.java</h1><pre class="source lang-java linenums">/* |
| * 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.shiro.web.config; |
| |
| import org.apache.shiro.config.Ini; |
| import org.apache.shiro.config.IniFactorySupport; |
| import org.apache.shiro.config.IniSecurityManagerFactory; |
| import org.apache.shiro.config.ReflectionBuilder; |
| import org.apache.shiro.util.CollectionUtils; |
| import org.apache.shiro.util.Factory; |
| import org.apache.shiro.web.filter.mgt.FilterChainManager; |
| import org.apache.shiro.web.filter.mgt.FilterChainResolver; |
| import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import javax.servlet.Filter; |
| import javax.servlet.FilterConfig; |
| import java.util.LinkedHashMap; |
| import java.util.Map; |
| |
| /** |
| * A {@link Factory} that creates {@link FilterChainResolver} instances based on {@link Ini} configuration. |
| * |
| * @since 1.0 |
| */ |
| public class IniFilterChainResolverFactory extends IniFactorySupport<FilterChainResolver> { |
| |
| public static final String FILTERS = "filters"; |
| public static final String URLS = "urls"; |
| |
| <span class="fc" id="L48"> private static transient final Logger log = LoggerFactory.getLogger(IniFilterChainResolverFactory.class);</span> |
| |
| private FilterConfig filterConfig; |
| |
| public IniFilterChainResolverFactory() { |
| <span class="fc" id="L53"> super();</span> |
| <span class="fc" id="L54"> }</span> |
| |
| public IniFilterChainResolverFactory(Ini ini) { |
| <span class="fc" id="L57"> super(ini);</span> |
| <span class="fc" id="L58"> }</span> |
| |
| public IniFilterChainResolverFactory(Ini ini, Map<String, ?> defaultBeans) { |
| <span class="fc" id="L61"> this(ini);</span> |
| <span class="fc" id="L62"> this.setDefaults(defaultBeans);</span> |
| <span class="fc" id="L63"> }</span> |
| |
| public FilterConfig getFilterConfig() { |
| <span class="fc" id="L66"> return filterConfig;</span> |
| } |
| |
| public void setFilterConfig(FilterConfig filterConfig) { |
| <span class="fc" id="L70"> this.filterConfig = filterConfig;</span> |
| <span class="fc" id="L71"> }</span> |
| |
| protected FilterChainResolver createInstance(Ini ini) { |
| <span class="fc" id="L74"> FilterChainResolver filterChainResolver = createDefaultInstance();</span> |
| <span class="pc bpc" id="L75" title="1 of 2 branches missed."> if (filterChainResolver instanceof PathMatchingFilterChainResolver) {</span> |
| <span class="fc" id="L76"> PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) filterChainResolver;</span> |
| <span class="fc" id="L77"> FilterChainManager manager = resolver.getFilterChainManager();</span> |
| <span class="fc" id="L78"> buildChains(manager, ini);</span> |
| } |
| <span class="fc" id="L80"> return filterChainResolver;</span> |
| } |
| |
| protected FilterChainResolver createDefaultInstance() { |
| <span class="fc" id="L84"> FilterConfig filterConfig = getFilterConfig();</span> |
| <span class="fc bfc" id="L85" title="All 2 branches covered."> if (filterConfig != null) {</span> |
| <span class="fc" id="L86"> return new PathMatchingFilterChainResolver(filterConfig);</span> |
| } else { |
| <span class="fc" id="L88"> return new PathMatchingFilterChainResolver();</span> |
| } |
| } |
| |
| protected void buildChains(FilterChainManager manager, Ini ini) { |
| //filters section: |
| <span class="fc" id="L94"> Ini.Section section = ini.getSection(FILTERS);</span> |
| |
| <span class="fc bfc" id="L96" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(section)) {</span> |
| <span class="fc" id="L97"> String msg = "The [{}] section has been deprecated and will be removed in a future release! Please " +</span> |
| "move all object configuration (filters and all other objects) to the [{}] section."; |
| <span class="fc" id="L99"> log.warn(msg, FILTERS, IniSecurityManagerFactory.MAIN_SECTION_NAME);</span> |
| } |
| |
| <span class="fc" id="L102"> Map<String, Object> defaults = new LinkedHashMap<String, Object>();</span> |
| |
| <span class="fc" id="L104"> Map<String, Filter> defaultFilters = manager.getFilters();</span> |
| |
| //now let's see if there are any object defaults in addition to the filters |
| //these can be used to configure the filters: |
| //create a Map of objects to use as the defaults: |
| <span class="pc bpc" id="L109" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(defaultFilters)) {</span> |
| <span class="fc" id="L110"> defaults.putAll(defaultFilters);</span> |
| } |
| //User-provided objects must come _after_ the default filters - to allow the user-provided |
| //ones to override the default filters if necessary. |
| <span class="fc" id="L114"> Map<String, ?> defaultBeans = getDefaults();</span> |
| <span class="fc bfc" id="L115" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(defaultBeans)) {</span> |
| <span class="fc" id="L116"> defaults.putAll(defaultBeans);</span> |
| } |
| |
| <span class="fc" id="L119"> Map<String, Filter> filters = getFilters(section, defaults);</span> |
| |
| //add the filters to the manager: |
| <span class="fc" id="L122"> registerFilters(filters, manager);</span> |
| |
| //urls section: |
| <span class="fc" id="L125"> section = ini.getSection(URLS);</span> |
| <span class="fc" id="L126"> createChains(section, manager);</span> |
| <span class="fc" id="L127"> }</span> |
| |
| protected void registerFilters(Map<String, Filter> filters, FilterChainManager manager) { |
| <span class="pc bpc" id="L130" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(filters)) {</span> |
| <span class="fc bfc" id="L131" title="All 2 branches covered."> boolean init = getFilterConfig() != null; //only call filter.init if there is a FilterConfig available</span> |
| <span class="fc bfc" id="L132" title="All 2 branches covered."> for (Map.Entry<String, Filter> entry : filters.entrySet()) {</span> |
| <span class="fc" id="L133"> String name = entry.getKey();</span> |
| <span class="fc" id="L134"> Filter filter = entry.getValue();</span> |
| <span class="fc" id="L135"> manager.addFilter(name, filter, init);</span> |
| <span class="fc" id="L136"> }</span> |
| } |
| <span class="fc" id="L138"> }</span> |
| |
| protected Map<String, Filter> getFilters(Map<String, String> section, Map<String, ?> defaults) { |
| |
| <span class="fc" id="L142"> Map<String, Filter> filters = extractFilters(defaults);</span> |
| |
| <span class="fc bfc" id="L144" title="All 2 branches covered."> if (!CollectionUtils.isEmpty(section)) {</span> |
| <span class="fc" id="L145"> ReflectionBuilder builder = new ReflectionBuilder(defaults);</span> |
| <span class="fc" id="L146"> Map<String, ?> built = builder.buildObjects(section);</span> |
| <span class="fc" id="L147"> Map<String,Filter> sectionFilters = extractFilters(built);</span> |
| |
| <span class="fc bfc" id="L149" title="All 2 branches covered."> if (CollectionUtils.isEmpty(filters)) {</span> |
| <span class="fc" id="L150"> filters = sectionFilters;</span> |
| } else { |
| <span class="pc bpc" id="L152" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(sectionFilters)) {</span> |
| <span class="fc" id="L153"> filters.putAll(sectionFilters);</span> |
| } |
| } |
| } |
| |
| <span class="fc" id="L158"> return filters;</span> |
| } |
| |
| private Map<String, Filter> extractFilters(Map<String, ?> objects) { |
| <span class="fc bfc" id="L162" title="All 2 branches covered."> if (CollectionUtils.isEmpty(objects)) {</span> |
| <span class="fc" id="L163"> return null;</span> |
| } |
| <span class="fc" id="L165"> Map<String, Filter> filterMap = new LinkedHashMap<String, Filter>();</span> |
| <span class="fc bfc" id="L166" title="All 2 branches covered."> for (Map.Entry<String, ?> entry : objects.entrySet()) {</span> |
| <span class="fc" id="L167"> String key = entry.getKey();</span> |
| <span class="fc" id="L168"> Object value = entry.getValue();</span> |
| <span class="fc bfc" id="L169" title="All 2 branches covered."> if (value instanceof Filter) {</span> |
| <span class="fc" id="L170"> filterMap.put(key, (Filter) value);</span> |
| } |
| <span class="fc" id="L172"> }</span> |
| <span class="fc" id="L173"> return filterMap;</span> |
| } |
| |
| protected void createChains(Map<String, String> urls, FilterChainManager manager) { |
| <span class="fc bfc" id="L177" title="All 2 branches covered."> if (CollectionUtils.isEmpty(urls)) {</span> |
| <span class="pc bpc" id="L178" title="1 of 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="fc" id="L179"> log.debug("No urls to process.");</span> |
| } |
| <span class="fc" id="L181"> return;</span> |
| } |
| |
| <span class="fc bfc" id="L184" title="All 2 branches covered."> if (log.isTraceEnabled()) {</span> |
| <span class="fc" id="L185"> log.trace("Before url processing.");</span> |
| } |
| |
| <span class="fc bfc" id="L188" title="All 2 branches covered."> for (Map.Entry<String, String> entry : urls.entrySet()) {</span> |
| <span class="fc" id="L189"> String path = entry.getKey();</span> |
| <span class="fc" id="L190"> String value = entry.getValue();</span> |
| <span class="fc" id="L191"> manager.createChain(path, value);</span> |
| <span class="fc" id="L192"> }</span> |
| <span class="fc" id="L193"> }</span> |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |