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     */
022    package org.granite.spring;
023    
024    import org.springframework.beans.factory.config.BeanDefinition;
025    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
027    import org.springframework.beans.factory.xml.ParserContext;
028    import org.springframework.util.StringUtils;
029    import org.w3c.dom.Element;
030    
031    /**
032     * @author William Drai
033     */
034    public 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 authenticationExtension = element.getAttribute("authentication-extension");
048            if (authenticationExtension != null && authenticationExtension.trim().length() > 0)
049                builder.addPropertyReference("authenticationExtension", authenticationExtension);
050    
051            String authenticationManager = element.getAttribute("authentication-manager");
052            if (authenticationManager != null && authenticationManager.trim().length() > 0)
053                    builder.addPropertyReference("authenticationManager", authenticationManager);
054            
055            boolean allowAnonymousAccess = Boolean.valueOf(element.hasAttribute("allow-anonymous-access"));
056                    builder.addPropertyValue("allowAnonymousAccess", allowAnonymousAccess);
057            
058            String securityContextRepository = element.getAttribute("security-context-repository");
059            if (securityContextRepository != null && securityContextRepository.trim().length() > 0)
060                    builder.addPropertyReference("securityContextRepository", securityContextRepository);
061            
062            String securityMetadataSource = element.getAttribute("security-metadata-source");
063            if (securityMetadataSource != null && securityMetadataSource.trim().length() > 0)
064                    builder.addPropertyReference("securityMetadataSource", securityMetadataSource);
065            
066            String securityInterceptor = element.getAttribute("security-interceptor");
067            if (securityInterceptor != null && securityInterceptor.trim().length() > 0)
068                    builder.addPropertyReference("securityInterceptor", securityInterceptor);
069            
070            String authenticationTrustResolver = element.getAttribute("authentication-trust-resolver");
071            if (authenticationTrustResolver != null && authenticationTrustResolver.trim().length() > 0)
072                builder.addPropertyReference("authenticationTrustResolver", authenticationTrustResolver);
073            
074            String sessionAuthenticationStrategy = element.getAttribute("session-authentication-strategy");
075            if (sessionAuthenticationStrategy != null && sessionAuthenticationStrategy.trim().length() > 0)
076                builder.addPropertyReference("sessionAuthenticationStrategy", sessionAuthenticationStrategy);
077            
078            String passwordEncoder = element.getAttribute("password-encoder");
079            if (passwordEncoder != null && passwordEncoder.trim().length() > 0)
080                builder.addPropertyReference("passwordEncoder", passwordEncoder);
081        }
082    
083        @Override
084        protected String getBeanClassName(Element element) {
085            return "org.granite.spring.security.SpringSecurity3Service";
086        }
087    }