jersey icon indicating copy to clipboard operation
jersey copied to clipboard

Fix Dynamic proxy

Open kalinchan opened this issue 1 year ago • 2 comments

Credit to @breakponchito

This is a fix to enable the creation of Proxy classes for context Field injection instead of concrete classes.

kalinchan avatar Sep 23 '24 11:09 kalinchan

@kalinchan Do I understand this PR correctly that the injectee class is annotated by both RequestScoped and EJB Singleton? A test would be much explanatory here...

jansupol avatar Oct 02 '24 12:10 jansupol

I have attempted to create the test for this, but was unsuccessful.

If a class is annotated with jakarta.ejb.Singleton:

@Singleton
@Path("singleton")
public class RSSingletonTestService {
    ...
}

When injecting:

@Context
private HttpHeaders httpHeaders;

@Context
private UriInfo uriInfo;

And calling two different endpoints:

    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("/test1")
    public String test1(@HeaderParam("TESTHEADER") final String testHeaderParam, @Context final UriInfo uriInfoParam) {
        final long msstart = System.currentTimeMillis();
        final String ret = getHeader("test1");

        return getTestBody(testHeaderParam, uriInfoParam.getRequestUri().toString(), msstart, ret);

    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("/test2")
    public String test2(@HeaderParam("TESTHEADER") final String testHeaderParam, @Context final UriInfo uriInfoParam) {
        final long msstart = System.currentTimeMillis();
        final String ret = getHeader("test2");

        return getTestBody(testHeaderParam, uriInfoParam.getRequestUri().toString(), msstart, ret);

    }

The Header and URI incorrectly returns the first requested Header and URI respectively.

private String getTestBody(final String testHeaderParam, final String uriParam, final long msstart, String ret) {
        ret += "URI member: " + uriInfo.getRequestUri().toString() + "\n"; // prints first requested uri // WRONG !!
        ret += "URI param: " + uriParam + "\n"; // works as expected; prints uri per request

        ret += "TESTHEADER member:" + httpHeaders.getHeaderString("TESTHEADER") + "\n"; // prints header sent by first request // WRONG !!
        ret += "TESTHEADER param:" + testHeaderParam + "\n"; // works as expected; prints header per request

        return ret + getFooter(msstart);
    }

Expected Result:

...
URI member: http://localhost:8080/servlet/singleton/test2
URI param: http://localhost:8080/servlet/singleton/test2
TESTHEADER member:TEST2
TESTHEADER param:TEST2
...

Actual Result:

...
URI member: http://localhost:8080/servlet/singleton/test1
URI param: http://localhost:8080/servlet/singleton/test2
TESTHEADER member:TEST1
TESTHEADER param:TEST2
...

kalinchan avatar Oct 03 '24 11:10 kalinchan