// 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.util.CdiUtils;
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
/*
 * Copyright © 2017 Ivar Grimstad (ivar.grimstad@gmail.com)
 *
 * 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 org.mvcspec.ozark.jaxrs;



/**
 * This filter is used to get the JAX-RS context objects and feed them to the corresponding
 * CDI producer.
 * <p>
 * This class must not be a CDI bean, because CXF/OWB fails to process <code>@Context</code> in this case.
 *
 * @author Christian Kaltepoth
 */
@PreMatching
@Priority(0) // very early
public class JaxRsContextFilter implements ContainerRequestFilter {

    @Context
    private Configuration configuration;

    @Context
    private HttpServletRequest request;

    @Context
    private HttpServletResponse response;

    @Context
    private Application application;

    @Context
    private UriInfo uriInfo;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        /*
         * Please note that we CANNOT inject JaxRsContextProducer here, because this will
         * fail on TomEE/CXF/OWB because processing @Context fails for some reason.
         */
// 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/jaxrs/JaxRsContextFilter.java
import org.mvcspec.ozark.util.CdiUtils;
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;

/*
 * Copyright © 2017 Ivar Grimstad (ivar.grimstad@gmail.com)
 *
 * 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 org.mvcspec.ozark.jaxrs;



/**
 * This filter is used to get the JAX-RS context objects and feed them to the corresponding
 * CDI producer.
 * <p>
 * This class must not be a CDI bean, because CXF/OWB fails to process <code>@Context</code> in this case.
 *
 * @author Christian Kaltepoth
 */
@PreMatching
@Priority(0) // very early
public class JaxRsContextFilter implements ContainerRequestFilter {

    @Context
    private Configuration configuration;

    @Context
    private HttpServletRequest request;

    @Context
    private HttpServletResponse response;

    @Context
    private Application application;

    @Context
    private UriInfo uriInfo;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        /*
         * Please note that we CANNOT inject JaxRsContextProducer here, because this will
         * fail on TomEE/CXF/OWB because processing @Context fails for some reason.
         */
        CdiUtils.getApplicationBean(JaxRsContextProducer.class)