001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *      http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.apache.shiro.ee.cdi;
015
016import java.io.Serializable;
017import java.util.Collection;
018import java.util.List;
019import java.util.Set;
020import java.util.stream.Collectors;
021import java.util.stream.Stream;
022import javax.enterprise.context.SessionScoped;
023import javax.enterprise.event.Observes;
024import javax.enterprise.inject.spi.AfterBeanDiscovery;
025import javax.enterprise.inject.spi.BeforeBeanDiscovery;
026import javax.enterprise.inject.spi.Extension;
027import javax.enterprise.inject.spi.ProcessAnnotatedType;
028import javax.enterprise.inject.spi.WithAnnotations;
029
030import org.apache.shiro.cdi.AnnotatedTypeWrapper;
031import org.apache.shiro.cdi.ShiroSecurityExtension.ShiroSecureAnnotated;
032import org.apache.shiro.mgt.SecurityManager;
033import org.apache.shiro.session.Session;
034import org.apache.shiro.session.SessionListener;
035
036/**
037 * Entry point for Shiro Session scope CDI extension
038 */
039public class ShiroSessionScopeExtension implements Extension, Serializable {
040    private static final long serialVersionUID = 1L;
041    @SuppressWarnings("ConstantName")
042    private static final List<ShiroScopeContext> contexts = Stream.of(
043                    new ShiroScopeContext(ShiroSessionScoped.class, SessionScoped.class),
044                    new ShiroScopeContext(ShiroFacesViewScoped.class, javax.faces.view.ViewScoped.class),
045                    new ShiroScopeContext(ShiroOmniViewScoped.class, org.omnifaces.cdi.ViewScoped.class))
046            .collect(Collectors.toList());
047
048    @SessionScoped
049    @SuppressWarnings("serial")
050    private static final class SessionScopedAnnotated implements Serializable {
051    }
052
053    @javax.faces.view.ViewScoped
054    @SuppressWarnings("serial")
055    private static final class FacesViewScopedAnnotated implements Serializable {
056    }
057
058    @org.omnifaces.cdi.ViewScoped
059    @SuppressWarnings("serial")
060    private static final class OmniViewScopedAnnotated implements Serializable {
061    }
062
063    @ShiroSessionScoped
064    @SuppressWarnings("serial")
065    private static final class ShiroSessionScopedAnnotated implements Serializable {
066    }
067
068    @ShiroFacesViewScoped
069    @SuppressWarnings("serial")
070    private static final class ShiroFacesViewScopedAnnotated implements Serializable {
071    }
072
073    @ShiroOmniViewScoped
074    @SuppressWarnings("serial")
075    private static final class ShiroOmniViewScopedAnnotated implements Serializable {
076    }
077
078
079    /**
080     * intercept session destroy session listeners and destroy the beans
081     *
082     * @param sessionListeners
083     * @param sm
084     */
085    public void addSessionListeners(Collection<SessionListener> sessionListeners, SecurityManager sm) {
086        sessionListeners.add(new SessionListener() {
087            @Override
088            public void onStart(Session session) {
089                contexts.forEach(ctx -> ctx.onCreate(session));
090            }
091
092            @Override
093            public void onStop(Session session) {
094                contexts.forEach(ctx -> ctx.onDestroy(session));
095            }
096
097            @Override
098            public void onExpiration(Session session) {
099                onStop(session);
100            }
101        });
102    }
103
104    <T> void addSessionScoped(@Observes @WithAnnotations(SessionScoped.class) ProcessAnnotatedType<T> pat) {
105        pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
106                Set.of(ShiroSessionScopedAnnotated.class.getDeclaredAnnotations()[0],
107                        ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
108                Set.of(SessionScopedAnnotated.class.getDeclaredAnnotations()[0])));
109    }
110
111    <T> void addFacesViewScoped(@Observes @WithAnnotations(javax.faces.view.ViewScoped.class) ProcessAnnotatedType<T> pat) {
112        pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
113                Set.of(ShiroFacesViewScopedAnnotated.class.getDeclaredAnnotations()[0],
114                        ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
115                Set.of(FacesViewScopedAnnotated.class.getDeclaredAnnotations()[0])));
116    }
117
118    <T> void addOmniViewScoped(@Observes @WithAnnotations(org.omnifaces.cdi.ViewScoped.class) ProcessAnnotatedType<T> pat) {
119        pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
120                Set.of(ShiroOmniViewScopedAnnotated.class.getDeclaredAnnotations()[0],
121                        ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
122                Set.of(OmniViewScopedAnnotated.class.getDeclaredAnnotations()[0])));
123    }
124
125    void addScope(@Observes final BeforeBeanDiscovery event) {
126        contexts.forEach(ctx -> event.addScope(ctx.getScope(), true, true));
127    }
128
129
130    void registerContext(@Observes final AfterBeanDiscovery event) {
131        contexts.forEach(event::addContext);
132    }
133}