From 8c1d64f2a03e7e4aa148b7cb646403462a01113a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pappy=20=28R=C4=83zvan=20ST=C4=82NESCU=29?= Date: Thu, 29 Aug 2019 20:31:24 +0200 Subject: [PATCH] Issue 4208 - Fails to inject SecurityContext into Helloworld-CDI2-SE example (#4236) * #4208: inject SecurityContext into HelloWorldResource & Implemented getBeanClass in SupplierBeanBridge. The method JersetBean.getBeanClass is used to compute the bean identifier. The default id for a JerseyBean is "java.lang.Object#jersey", causing collisions of bridge instances. In particular, for HelloWorldResourceTest, Weld's MapBeanStore returned a SupplierInstanceBeanBridge instead of the expected SupplierBeanBridge, because both shared the same bean identifier. Signed-off-by: pappy --- .../helloworld/cdi2se/HelloWorldResource.java | 26 +++++++++++++++---- .../cdi/se/bean/SupplierBeanBridge.java | 9 ++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java index 7799da393d..ce9cba1fa3 100644 --- a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -10,13 +10,19 @@ package org.glassfish.jersey.examples.helloworld.cdi2se; +import java.security.Principal; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.validation.constraints.NotNull; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.SecurityContext; -import javax.inject.Inject; -import javax.inject.Singleton; +import static java.util.Optional.ofNullable; /** * Singleton-scoped resource. @@ -33,7 +39,17 @@ public class HelloWorldResource { @GET @Path("{name}") @Produces("text/plain") - public String getHello(@PathParam("name") String name) { - return helloBean.hello(name); + public String getHello(@PathParam("name") String name, @Context SecurityContext sc) { + final StringBuilder sb = new StringBuilder(this.helloBean.hello(name)); + + ofNullable(sc.getUserPrincipal()) + .map(Principal::getName) + .ifPresent(p -> { + sb.append("("); + sb.append(p); + sb.append(")"); + }); + + return sb.toString(); } } diff --git a/inject/cdi2-se/src/main/java/org/glassfish/jersey/inject/cdi/se/bean/SupplierBeanBridge.java b/inject/cdi2-se/src/main/java/org/glassfish/jersey/inject/cdi/se/bean/SupplierBeanBridge.java index 320847868c..281f489045 100644 --- a/inject/cdi2-se/src/main/java/org/glassfish/jersey/inject/cdi/se/bean/SupplierBeanBridge.java +++ b/inject/cdi2-se/src/main/java/org/glassfish/jersey/inject/cdi/se/bean/SupplierBeanBridge.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -143,4 +143,11 @@ private static Supplier getSupplier(BeanManager beanManager, ParameterizedTyp public Class getScope() { return binding.getScope() == null ? Dependent.class : transformScope(binding.getScope()); } + + @Override + public Class getBeanClass() { + return this.binding.getContracts().isEmpty() + ? super.getBeanClass() + : (Class) this.binding.getContracts().iterator().next(); + } }