it.scoppelletti.programmerpower.web.security.CompositeChannelDecisionManager.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.web.security.CompositeChannelDecisionManager.java

Source

/*
 * Copyright (C) 2011 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * Licensed 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 it.scoppelletti.programmerpower.web.security;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.*;
import org.springframework.security.access.*;
import org.springframework.security.web.*;
import org.springframework.security.web.access.channel.*;
import org.springframework.security.web.access.intercept.*;
import it.scoppelletti.programmerpower.reflect.*;

/**
 * Controllore dei protocolli.
 * 
 * <P>Il componente {@code CompositeChannelDecisionManager} estende le
 * funzionalit&agrave; del componente Spring {@code ChannelDecisionManagerImpl}
 * con la possibilit&agrave; di integrare la configurazione dei protocolli con i
 * componenti {@code CompositeDecisionManagerContributor} registrati come bean
 * nel contesto dell&rsquo;applicazione.</P>
 *  
 * @see   it.scoppelletti.programmerpower.web.security.CompositeDecisionManagerContributor
 * @since 1.0.0
 */
@Final
public class CompositeChannelDecisionManager extends ChannelDecisionManagerImpl implements BeanNameAware {

    /**
     * Nome della propriet&agrave; di ambiente sulla quale pu&ograve; essere
     * impostato il numero di porta per il protocollo HTTP. Il valore della
     * costante &egrave; <CODE>{@value}</CODE>.
     * 
     * @it.scoppelletti.tag.default {@code 8080}
     * @see <A HREF="{@docRoot}/../reference/setup/envprops.html"
     *      TARGET="_top">Propriet&agrave; di ambiente</A> 
     */
    public static final String PROP_PORT_HTTP = "it.scoppelletti.programmerpower.web.port.http";

    /**
     * Nome della propriet&agrave; di ambiente sulla quale pu&ograve; essere
     * impostato il numero di porta per il protocollo HTTPS. Il valore della
     * costante &egrave; <CODE>{@value}</CODE>. 
     * 
     * @it.scoppelletti.tag.default {@code 8443}
     * @see <A HREF="{@docRoot}/../reference/setup/envprops.html"
     *      TARGET="_top">Propriet&agrave; di ambiente</A> 
     */
    public static final String PROP_PORT_HTTPS = "it.scoppelletti.programmerpower.web.port.https";

    private final Object mySyncRoot = new Object();
    private String myBeanName;
    private List<FilterInvocationSecurityMetadataSource> mySecurityMetadataSource;

    @Autowired
    private ApplicationContext myApplCtx;

    /**
     * Costruttore.
     */
    public CompositeChannelDecisionManager() {
    }

    /**
     * Imposta il nome del bean.
     * 
     * @param value Valore.
     */
    public void setBeanName(String value) {
        myBeanName = value;
    }

    /**
     * Applica il controllo del protocollo.
     * 
     * @param invocation Componente chiamante.
     * @param config     Configurazione dei protocolli da applicare. 
     */
    @Override
    public void decide(FilterInvocation invocation, Collection<ConfigAttribute> config)
            throws IOException, ServletException {
        Collection<ConfigAttribute> attrs;
        List<FilterInvocationSecurityMetadataSource> list;

        synchronized (mySyncRoot) {
            if (mySecurityMetadataSource == null) {
                mySecurityMetadataSource = CompositeDecisionManagerContributor
                        .listSecurityMetadataSource(myBeanName, myApplCtx);
            }

            // Uso una copia della collezione per rilasciare il lock appena
            // possibile
            list = new ArrayList<FilterInvocationSecurityMetadataSource>(mySecurityMetadataSource);
        }

        for (FilterInvocationSecurityMetadataSource securityMds : list) {
            attrs = securityMds.getAttributes(invocation);
            if (attrs != null && !attrs.isEmpty()) {
                super.decide(invocation, attrs);
                return;
            }
        }

        super.decide(invocation, config);
    }
}