// Path: core/src/main/java/org/mvcspec/ozark/util/CdiUtils.java
// @ApplicationScoped
// @SuppressWarnings("unchecked")
// public class CdiUtils {
// 
//     @Inject
//     private BeanManager beanManager;
// 
//     /**
//      * Create a new CDI bean given its class. The bean is created in the context
//      * defined by the scope annotation on the class.
//      *
//      * @param clazz CDI class.
//      * @param <T>   class parameter.
//      * @return newly allocated CDI bean.
//      */
//     @SuppressWarnings("unchecked")
//     public <T> T newBean(Class<T> clazz) {
//         return newBean(beanManager, clazz);
//     }
// 
//     /**
//      * Create a new CDI bean given its class and a bean manager. The bean is created
//      * in the context defined by the scope annotation on the class.
//      *
//      * @param bm    The BeanManager.
//      * @param clazz CDI class.
//      * @param <T>   class parameter.
//      * @return newly allocated CDI bean.
//      */
//     public static <T> T newBean(BeanManager bm, Class<T> clazz) {
//         Set<Bean<?>> beans = bm.getBeans(clazz);
//         final Bean<T> bean = (Bean<T>) bm.resolve(beans);
//         final CreationalContext<T> ctx = bm.createCreationalContext(bean);
//         return (T) bm.getReference(bean, clazz, ctx);
//     }
// 
//     /**
//      * @param beforeBean The BeforeBeanDiscovery.
//      * @param bm         The BeanManager.
//      * @param types      annotated types to register
//      */
//     public static void addAnnotatedTypes(BeforeBeanDiscovery beforeBean, BeanManager bm, Class<?>... types) {
//         for (Class<?> type : types) {
//             beforeBean.addAnnotatedType(bm.createAnnotatedType(type), type.getName());
//         }
//     }
// 
//     /**
//      * Returns a list of CDI beans with the specified bean type and qualifiers.
//      * Please note that this method supports looking up beans deployed with the application
//      * even if Ozark is deployed as a container archive.
//      */
//     public static <T> List<T> getApplicationBeans(Class<T> type, Annotation... qualifiers) {
//         BeanManager manager = getApplicationBeanManager();
//         return manager.getBeans(type, qualifiers).stream()
//                 .map(bean -> (T) manager.getReference(bean, type, manager.createCreationalContext(bean)))
//                 .collect(Collectors.toList());
//     }
// 
//     /**
//      * Returns a single CDI bean with the given type and qualifiers. Will throw an exception if there
//      * is more than one matching bean. Please note that this method supports looking up beans deployed
//      * with the application even if Ozark is deployed as a container archive.
//      */
//     public static <T> Optional<T> getApplicationBean(Class<T> type, Annotation... qualifiers) {
//         List<T> instances = getApplicationBeans(type);
//         if (instances.size() == 0) {
//             return Optional.empty();
//         } else if (instances.size() == 1) {
//             return Optional.of(instances.get(0));
//         } else {
//             throw new IllegalStateException("More than one CDI managed instance found of: " + type.getName());
//         }
//     }
// 
//     /**
//      * This method returns a {@link BeanManager} which can resolve beans defined in the application.
//      * In case of Glassfish the injected {@link BeanManager} doesn't work here as it only resolves
//      * beans in the Ozark archive if Ozark is installed as part of the container.
//      */
//     public static BeanManager getApplicationBeanManager() {
//         return CDI.current().getBeanManager();
//     }
// 
// }


import org.mvcspec.ozark.Properties;
import org.mvcspec.ozark.event.ControllerRedirectEventImpl;
import org.mvcspec.ozark.jaxrs.JaxRsContext;
import org.mvcspec.ozark.util.CdiUtils;
import org.mvcspec.ozark.util.PropertyUtils;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.inject.Inject;
import javax.mvc.MvcContext;
import javax.mvc.event.AfterProcessViewEvent;
import javax.mvc.event.BeforeControllerEvent;
import javax.mvc.event.ControllerRedirectEvent;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
                    if (cookie.getName().equals(COOKIE_NAME)) {
                        request.setAttribute(SCOPE_ID, cookie.getValue());
                        return;     // we're done
                    }
                }
            }
        } else {
            final String scopeId = event.getUriInfo().getQueryParameters().getFirst(SCOPE_ID);
            if (scopeId != null) {
                request.setAttribute(SCOPE_ID, scopeId);
            }
        }
    }

    /**
     * Perform the work we need to do at AfterProcessViewEvent time.
     *
     * @param event the event.
     */
    public void afterProcessViewEvent(@Observes AfterProcessViewEvent event) {
        if (request.getAttribute(SCOPE_ID) != null) {
            String scopeId = (String) request.getAttribute(SCOPE_ID);
            HttpSession session = request.getSession();
            final String sessionKey = SCOPE_ID + "-" + scopeId;
            Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
            if (null != scopeMap) {
                scopeMap.entrySet().stream().forEach((entrySet) -> {
                    String key = entrySet.getKey();
                    Object value = entrySet.getValue();
                    if (key.startsWith(INSTANCE)) {
// Path: core/src/main/java/org/mvcspec/ozark/util/CdiUtils.java
// @ApplicationScoped
// @SuppressWarnings("unchecked")
// public class CdiUtils {
// 
//     @Inject
//     private BeanManager beanManager;
// 
//     /**
//      * Create a new CDI bean given its class. The bean is created in the context
//      * defined by the scope annotation on the class.
//      *
//      * @param clazz CDI class.
//      * @param <T>   class parameter.
//      * @return newly allocated CDI bean.
//      */
//     @SuppressWarnings("unchecked")
//     public <T> T newBean(Class<T> clazz) {
//         return newBean(beanManager, clazz);
//     }
// 
//     /**
//      * Create a new CDI bean given its class and a bean manager. The bean is created
//      * in the context defined by the scope annotation on the class.
//      *
//      * @param bm    The BeanManager.
//      * @param clazz CDI class.
//      * @param <T>   class parameter.
//      * @return newly allocated CDI bean.
//      */
//     public static <T> T newBean(BeanManager bm, Class<T> clazz) {
//         Set<Bean<?>> beans = bm.getBeans(clazz);
//         final Bean<T> bean = (Bean<T>) bm.resolve(beans);
//         final CreationalContext<T> ctx = bm.createCreationalContext(bean);
//         return (T) bm.getReference(bean, clazz, ctx);
//     }
// 
//     /**
//      * @param beforeBean The BeforeBeanDiscovery.
//      * @param bm         The BeanManager.
//      * @param types      annotated types to register
//      */
//     public static void addAnnotatedTypes(BeforeBeanDiscovery beforeBean, BeanManager bm, Class<?>... types) {
//         for (Class<?> type : types) {
//             beforeBean.addAnnotatedType(bm.createAnnotatedType(type), type.getName());
//         }
//     }
// 
//     /**
//      * Returns a list of CDI beans with the specified bean type and qualifiers.
//      * Please note that this method supports looking up beans deployed with the application
//      * even if Ozark is deployed as a container archive.
//      */
//     public static <T> List<T> getApplicationBeans(Class<T> type, Annotation... qualifiers) {
//         BeanManager manager = getApplicationBeanManager();
//         return manager.getBeans(type, qualifiers).stream()
//                 .map(bean -> (T) manager.getReference(bean, type, manager.createCreationalContext(bean)))
//                 .collect(Collectors.toList());
//     }
// 
//     /**
//      * Returns a single CDI bean with the given type and qualifiers. Will throw an exception if there
//      * is more than one matching bean. Please note that this method supports looking up beans deployed
//      * with the application even if Ozark is deployed as a container archive.
//      */
//     public static <T> Optional<T> getApplicationBean(Class<T> type, Annotation... qualifiers) {
//         List<T> instances = getApplicationBeans(type);
//         if (instances.size() == 0) {
//             return Optional.empty();
//         } else if (instances.size() == 1) {
//             return Optional.of(instances.get(0));
//         } else {
//             throw new IllegalStateException("More than one CDI managed instance found of: " + type.getName());
//         }
//     }
// 
//     /**
//      * This method returns a {@link BeanManager} which can resolve beans defined in the application.
//      * In case of Glassfish the injected {@link BeanManager} doesn't work here as it only resolves
//      * beans in the Ozark archive if Ozark is installed as part of the container.
//      */
//     public static BeanManager getApplicationBeanManager() {
//         return CDI.current().getBeanManager();
//     }
// 
// }


// Path: core/src/main/java/org/mvcspec/ozark/cdi/RedirectScopeManager.java
import org.mvcspec.ozark.Properties;
import org.mvcspec.ozark.event.ControllerRedirectEventImpl;
import org.mvcspec.ozark.jaxrs.JaxRsContext;
import org.mvcspec.ozark.util.CdiUtils;
import org.mvcspec.ozark.util.PropertyUtils;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.inject.Inject;
import javax.mvc.MvcContext;
import javax.mvc.event.AfterProcessViewEvent;
import javax.mvc.event.BeforeControllerEvent;
import javax.mvc.event.ControllerRedirectEvent;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

                    if (cookie.getName().equals(COOKIE_NAME)) {
                        request.setAttribute(SCOPE_ID, cookie.getValue());
                        return;     // we're done
                    }
                }
            }
        } else {
            final String scopeId = event.getUriInfo().getQueryParameters().getFirst(SCOPE_ID);
            if (scopeId != null) {
                request.setAttribute(SCOPE_ID, scopeId);
            }
        }
    }

    /**
     * Perform the work we need to do at AfterProcessViewEvent time.
     *
     * @param event the event.
     */
    public void afterProcessViewEvent(@Observes AfterProcessViewEvent event) {
        if (request.getAttribute(SCOPE_ID) != null) {
            String scopeId = (String) request.getAttribute(SCOPE_ID);
            HttpSession session = request.getSession();
            final String sessionKey = SCOPE_ID + "-" + scopeId;
            Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
            if (null != scopeMap) {
                scopeMap.entrySet().stream().forEach((entrySet) -> {
                    String key = entrySet.getKey();
                    Object value = entrySet.getValue();
                    if (key.startsWith(INSTANCE)) {
                        BeanManager beanManager = CdiUtils.getApplicationBeanManager();