You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2018/12/10 14:47:27 UTC

[24/38] tomee git commit: WIP

WIP


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/8eb2ff30
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/8eb2ff30
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/8eb2ff30

Branch: refs/heads/master
Commit: 8eb2ff30495fe98679c260e2497e60e38c33a682
Parents: 1f1b6b3
Author: Jonathan Gallimore <jo...@jrg.me.uk>
Authored: Mon Oct 29 20:37:57 2018 +0000
Committer: Roberto Cortez <ra...@yahoo.com>
Committed: Fri Dec 7 18:13:04 2018 +0000

----------------------------------------------------------------------
 .../microprofile/jwt/cdi/MPJWTCDIExtension.java | 37 ++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/8eb2ff30/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/cdi/MPJWTCDIExtension.java
----------------------------------------------------------------------
diff --git a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/cdi/MPJWTCDIExtension.java b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/cdi/MPJWTCDIExtension.java
index b7305be..6724bab 100644
--- a/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/cdi/MPJWTCDIExtension.java
+++ b/mp-jwt/src/main/java/org/apache/tomee/microprofile/jwt/cdi/MPJWTCDIExtension.java
@@ -22,10 +22,16 @@ import org.apache.tomee.microprofile.jwt.MPJWTInitializer;
 import org.apache.tomee.microprofile.jwt.config.ConfigurableJWTAuthContextInfo;
 import org.apache.tomee.microprofile.jwt.jaxrs.MPJWPProviderRegistration;
 import org.eclipse.microprofile.jwt.Claim;
+import org.eclipse.microprofile.jwt.JsonWebToken;
 
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Default;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
@@ -34,6 +40,7 @@ import javax.enterprise.inject.spi.ProcessInjectionPoint;
 import javax.inject.Provider;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.security.Principal;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -58,7 +65,7 @@ public class MPJWTCDIExtension implements Extension {
 
     private Set<InjectionPoint> injectionPoints = new HashSet<>();
 
-    public void collectConfigProducer(@Observes final ProcessInjectionPoint<?, ?> pip) {
+    public void collectConfigProducer(@Observes final ProcessInjectionPoint<?, ?> pip, final BeanManager bm) {
         final Claim claim = pip.getInjectionPoint().getAnnotated().getAnnotation(Claim.class);
         if (claim != null) {
             injectionPoints.add(pip.getInjectionPoint());
@@ -94,6 +101,21 @@ public class MPJWTCDIExtension implements Extension {
                         abd.addBean(claimBean);
                     }
                 });
+
+        abd.addBean()
+                .id(MPJWTCDIExtension.class.getName() + "#" + JsonWebToken.class.getName())
+                .beanClass(JsonWebToken.class)
+                .types(JsonWebToken.class, Object.class)
+                .qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE)
+                .scope(ApplicationScoped.class)
+                .createWith(ctx -> {
+                    final Principal principal = getContextualReference(Principal.class, bm);
+                    if (JsonWebToken.class.isInstance(principal)) {
+                        return JsonWebToken.class.cast(principal);
+                    }
+
+                    return null;
+                });
     }
 
     public void observeBeforeBeanDiscovery(@Observes final BeforeBeanDiscovery bbd, final BeanManager beanManager) {
@@ -101,7 +123,18 @@ public class MPJWTCDIExtension implements Extension {
         bbd.addAnnotatedType(beanManager.createAnnotatedType(JsonbProducer.class));
         bbd.addAnnotatedType(beanManager.createAnnotatedType(MPJWTFilter.class));
         bbd.addAnnotatedType(beanManager.createAnnotatedType(MPJWTInitializer.class));
-//        bbd.addAnnotatedType(beanManager.createAnnotatedType(MPJWTProducer.class));
+    }
+
+    public static <T> T getContextualReference(Class<T> type, final BeanManager beanManager) {
+        final Set<Bean<?>> beans = beanManager.getBeans(type);
+
+        if (beans == null || beans.isEmpty()) {
+            throw new IllegalStateException("Could not find beans for Type=" + type);
+        }
+
+        final Bean<?> bean = beanManager.resolve(beans);
+        final CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
+        return (T) beanManager.getReference(bean, type, creationalContext);
     }
 
     static {