
com.flowlogix.security.cdi.ShiroSessionScopeContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flowlogix-jee Show documentation
Show all versions of flowlogix-jee Show documentation
Flow Logix Components for Jakarta EE
/*
* 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 extends Annotation> 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy