All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flowlogix.security.cdi.ShiroSessionScopeContext Maven / Gradle / Ivy

There is a newer version: 9.0.7
Show newest version
/*
 * Copyright 2015 lprimak.
 *
 * 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 com.flowlogix.security.cdi;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.regex.Pattern;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.CDI;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.mgt.WebSecurityManager;

/**
 * If web environment, delegate to SessionScoped,
 * otherwise use Shiro sessions to store session beans
 * 
 * @author lprimak
 */
public class ShiroSessionScopeContext implements Context, Serializable 
{
    @Override
    public Class getScope()
    {
        return ShiroSessionScoped.class;
    }
    

    @Override
    public  T get(Contextual contextual, CreationalContext creationalContext)
    {
        if(isWebContainerSessions())
        {
            Context ctx = CDI.current().getBeanManager().getContext(SessionScoped.class);
            return ctx.get(contextual, creationalContext);            
        }
        else
        {
            Session session = SecurityUtils.getSubject().getSession();
            Bean bean = (Bean)contextual;
            synchronized(session.getId().toString().intern())
            {
                @SuppressWarnings("unchecked")
                ScopeInst scopeInst = (ScopeInst) 
                        session.getAttribute(BEAN_PREFIX + bean.getBeanClass());
                T rv;
                if(scopeInst == null)
                {
                    rv = bean.create(creationalContext);
                    session.setAttribute(BEAN_PREFIX + bean.getBeanClass(), 
                            new ScopeInst<>(bean, rv, creationalContext));
                }
                else
                {
                    rv = scopeInst.instance;
                }
                return rv;
            }
        }
    }

    
    @Override
    public  T get(Contextual contextual)
    {
        if(isWebContainerSessions())
        {
            Context ctx = CDI.current().getBeanManager().getContext(SessionScoped.class);
            return ctx.get(contextual);
        }
        else
        {
            Session session = SecurityUtils.getSubject().getSession(false);
            T rv = null;
            if(session != null)
            {
                Bean bean = (Bean)contextual;
                @SuppressWarnings("unchecked")
                ScopeInst scopeInst = (ScopeInst) 
                        session.getAttribute(BEAN_PREFIX + bean.getBeanClass());        
                if(scopeInst != null)
                {
                    rv = scopeInst.instance;
                }
            }
            return rv;
        }
    }

    
    @Override
    public boolean isActive()
    {
        return true;
    }


    public  void onDestroy(Session session)
    {
        List attrNames = FluentIterable.from(session.getAttributeKeys())
                .transform(new Function()
                {
                    @Override
                    public String apply(Object f)
                    {
                        return f instanceof String ? (String) f : null;
                    }
                })
                .filter(Predicates.and(Predicates.notNull(),
                                Predicates.contains(bpPattern))).toList();
        for (String attrName : attrNames)
        {
            @SuppressWarnings("unchecked")
            ScopeInst scopeInst = (ScopeInst) session.getAttribute(attrName);
            if (scopeInst != null)
            {
                scopeInst.bean.destroy(scopeInst.instance, scopeInst.context);
            }
        }
    }
    
    
    private boolean isWebContainerSessions()
    {
        if(SecurityUtils.getSecurityManager() instanceof WebSecurityManager)
        {
            WebSecurityManager wsm = (WebSecurityManager) SecurityUtils.getSecurityManager();
            return wsm.isHttpSessionMode();
        }
        return false;
    }
    
    
    @RequiredArgsConstructor
    private static class ScopeInst implements Serializable
    {
        private final Bean bean;
        private final T instance;
        private final CreationalContext context;
        private static final long serialVersionUID = 1L;
    }
    
    
    private static final String BEAN_PREFIX = "FL_SSSC_";
    private static final Pattern bpPattern = Pattern.compile(String.format("^%s.*", BEAN_PREFIX));
    private static final long serialVersionUID = 1L;    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy