001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.spring;
023
024import org.springframework.beans.factory.config.BeanDefinition;
025import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
027import org.springframework.beans.factory.xml.ParserContext;
028import org.springframework.util.StringUtils;
029import org.w3c.dom.Element;
030
031/**
032 * @author William Drai
033 */
034public class SecurityServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
035
036    @Override
037    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
038        if (!SpringGraniteConfig.isSpringSecurity3Present())
039                throw new RuntimeException("Cannot use graniteds:security-service with Spring 2.x");
040        
041        // Set the default ID if necessary
042        if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE)))
043            element.setAttribute(ID_ATTRIBUTE, "org.granite.spring.security.SpringSecurity3Service");
044        
045        builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
046        
047        String authenticationManager = element.getAttribute("authentication-manager");
048        if (authenticationManager != null && authenticationManager.trim().length() > 0)
049                builder.addPropertyReference("authenticationManager", authenticationManager);
050        
051        boolean allowAnonymousAccess = Boolean.valueOf(element.hasAttribute("allow-anonymous-access"));
052                builder.addPropertyValue("allowAnonymousAccess", allowAnonymousAccess);
053        
054        String securityContextRepository = element.getAttribute("security-context-repository");
055        if (securityContextRepository != null && securityContextRepository.trim().length() > 0)
056                builder.addPropertyReference("securityContextRepository", securityContextRepository);
057        
058        String securityMetadataSource = element.getAttribute("security-metadata-source");
059        if (securityMetadataSource != null && securityMetadataSource.trim().length() > 0)
060                builder.addPropertyReference("securityMetadataSource", securityMetadataSource);
061        
062        String securityInterceptor = element.getAttribute("security-interceptor");
063        if (securityInterceptor != null && securityInterceptor.trim().length() > 0)
064                builder.addPropertyReference("securityInterceptor", securityInterceptor);
065        
066        String authenticationTrustResolver = element.getAttribute("authentication-trust-resolver");
067        if (authenticationTrustResolver != null && authenticationTrustResolver.trim().length() > 0)
068            builder.addPropertyReference("authenticationTrustResolver", authenticationTrustResolver);
069        
070        String sessionAuthenticationStrategy = element.getAttribute("session-authentication-strategy");
071        if (sessionAuthenticationStrategy != null && sessionAuthenticationStrategy.trim().length() > 0)
072            builder.addPropertyReference("sessionAuthenticationStrategy", sessionAuthenticationStrategy);
073        
074        String passwordEncoder = element.getAttribute("password-encoder");
075        if (passwordEncoder != null && passwordEncoder.trim().length() > 0)
076            builder.addPropertyReference("passwordEncoder", passwordEncoder);
077    }
078
079    @Override
080    protected String getBeanClassName(Element element) {
081        return "org.granite.spring.security.SpringSecurity3Service";
082    }
083}