org.springside.modules.security.springsecurity.DefinitionSourceFactoryBean.java Source code

Java tutorial

Introduction

Here is the source code for org.springside.modules.security.springsecurity.DefinitionSourceFactoryBean.java

Source

/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: DefinitionSourceFactoryBean.java 619 2009-11-03 16:38:04Z calvinxiu $
 */
package org.springside.modules.security.springsecurity;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ConfigAttributeEditor;
import org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.FilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.RequestKey;
import org.springframework.security.util.AntUrlPathMatcher;
import org.springframework.security.util.UrlMatcher;

/**
 * DefinitionSource.
 * 
 * resourceDetailService??URL-?,
 * ??LinkedHashMap<String, String>?URL??
 * SpringSecurityLinkedHashMap<RequestKey, ConfigAttributeDefinition>?.
 * 
 * @see org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource
 * @see ResourceDetailsService
 * 
 * @author calvin
 */
public class DefinitionSourceFactoryBean implements FactoryBean {

    private ResourceDetailsService resourceDetailsService;

    public void setResourceDetailsService(ResourceDetailsService resourceDetailsService) {
        this.resourceDetailsService = resourceDetailsService;
    }

    /**
     * Ant StyleURLMatcherResourceDetailService??RequestMapDefaultFilterInvocationDefinitionSource.
     */
    public Object getObject() throws Exception {
        LinkedHashMap<RequestKey, ConfigAttributeDefinition> requestMap = buildRequestMap();
        UrlMatcher matcher = getUrlMatcher();
        DefaultFilterInvocationDefinitionSource definitionSource = new DefaultFilterInvocationDefinitionSource(
                matcher, requestMap);
        return definitionSource;
    }

    /**
     * @see FactoryBean#getObjectType()
     */
    @SuppressWarnings("unchecked")
    public Class getObjectType() {
        return FilterInvocationDefinitionSource.class;
    }

    /**
     * @see FactoryBean#isSingleton()
     */
    public boolean isSingleton() {
        return true;
    }

    /**
     * ??Ant StyleURLMatcher.
     */
    protected UrlMatcher getUrlMatcher() {
        return new AntUrlPathMatcher();
    }

    /**
     * resourceDetailService??LinkedHashMap<String, String>?URL??
     * DefaultFilterInvocationDefinitionSource?LinkedHashMap<RequestKey, ConfigAttributeDefinition>?.
     */
    protected LinkedHashMap<RequestKey, ConfigAttributeDefinition> buildRequestMap() throws Exception {
        LinkedHashMap<String, String> srcMap = resourceDetailsService.getRequestMap();
        LinkedHashMap<RequestKey, ConfigAttributeDefinition> distMap = new LinkedHashMap<RequestKey, ConfigAttributeDefinition>();
        ConfigAttributeEditor editor = new ConfigAttributeEditor();

        for (Map.Entry<String, String> entry : srcMap.entrySet()) {
            RequestKey key = new RequestKey(entry.getKey(), null);
            if (StringUtils.isNotBlank(entry.getValue())) {
                editor.setAsText(entry.getValue());
                distMap.put(key, (ConfigAttributeDefinition) editor.getValue());
            } else {
                distMap.put(key, ConfigAttributeDefinition.NO_ATTRIBUTES);
            }
        }

        return distMap;
    }
}