Skip to content

Commit

Permalink
Issue 4208 - Fails to inject SecurityContext into Helloworld-CDI2-SE …
Browse files Browse the repository at this point in the history
…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<T> 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 <[email protected]>
  • Loading branch information
pa314159 authored and jansupol committed Aug 29, 2019
1 parent 38c1957 commit 8c1d64f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -143,4 +143,11 @@ private static Supplier<?> getSupplier(BeanManager beanManager, ParameterizedTyp
public Class<? extends Annotation> 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();
}
}

0 comments on commit 8c1d64f

Please sign in to comment.