Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContainerRequestFilter get invoked twice with one request #3994

Closed
scrmtrey91 opened this issue May 31, 2019 · 7 comments · Fixed by #4045
Closed

ContainerRequestFilter get invoked twice with one request #3994

scrmtrey91 opened this issue May 31, 2019 · 7 comments · Fixed by #4045

Comments

@scrmtrey91
Copy link

scrmtrey91 commented May 31, 2019

Description


ContainerRequestFilter get invoked twice with one request, when using roles allowed annotation.

Code:

@Provider
@Priority(AUTHENTICATION)
public class WSFilter implements ContainerRequestFilter {

        requestContext.getHeaders().add("Authorization", "test ticket");

        requestContext.setSecurityContext(new SecurityContext() {
            @Override
            public Principal getUserPrincipal() {
                return () -> "test user";
            }

            @Override
            public boolean isUserInRole(String role) {
                return true;
            }

            @Override
            public boolean isSecure() {
                return true;
            }

            @Override
            public String getAuthenticationScheme() {
                return null;
            }
        });
}

@ApplicationPath("ws")
public class JAXRSConfiguration extends ResourceConfig {

    public JAXRSConfiguration() {
        packages("com.test.backend");
        register(JacksonFeature.class);
        register(RolesAllowedDynamicFeature.class);
    }
}

@Path("data")
@RolesAllowed({"test"})
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
public class TestResource {}

payara-web.xml:

<!DOCTYPE payara-web-app PUBLIC "-//Payara.fish//DTD Payara Server 4 Servlet 3.0//EN"
        "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-web-app_4.dtd">
<payara-web-app>
    <context-root>P192</context-root>
    <jaxrs-roles-allowed-enabled>false</jaxrs-roles-allowed-enabled>
</payara-web-app>

Expected Outcome

ContainerRequestFilter should only get invoked once (like in payara 5.191). Header string get added only once.

Current Outcome

ContainerRequestFilter get invoked twice with one request. Because of that it adds the same header string twice which cause problems.

Environment

  • Payara Version: 5.192
  • Edition: Full
  • JDK Version: 8.201 oracle
  • Operating System: Windows / Linux (docker)
@mikael2
Copy link

mikael2 commented Jun 1, 2019

Filter is instantiated twice in 192. Same problem with ContainerResponseFilter

@smillidge
Copy link
Contributor

Can you create a test case on GitHub we could use?

@smillidge smillidge added the PR: TESTS REQUIRED PR Requires Tests to be merged label Jun 1, 2019
@mikael2
Copy link

mikael2 commented Jun 1, 2019

https://github.com/mikael2/Payara192DoubleFilter
Double instantiation occurs if beans.xml bean-discovery-mode is set to all

Example from Payara output below:

INFO: ResponseFilter() payara.doublefilter192.ResponseFilter@cd0095
INFO: ResponseFilter() payara.doublefilter192.ResponseFilter@7e1571
INFO: Loading application [Payara192DoubleFilter] at [/Payara192DoubleFilter]
INFO: Data Grid Status
Payara Data Grid State: DG Version: 35 DG Name: development DG Size: 1
Instances: {
DataGrid: development Instance Group: MicroShoal Name: server Lite: false This: true UUID: 52f2b792-37a2-4f8d-9f23-46a5d75df0d8 Address: /192.168.1.105:4900
}
INFO: Payara Server 5.192 #badassfish (115) startup time : Felix (1,046ms), startup services(4,768ms), total(5,814ms)
INFO: Payara Notification Service bootstrapped with configuration: NotificationExecutionOptions{enabled=true, notifierConfigurationExecutionOptionsList={LOG=LogNotifierConfigurationExecutionOptions{useSeparateLogFile=false} NotifierConfigurationExecutionOptions{notifierType=LOG, enabled=true, noisy=true}}}
INFO: Network Listener JMS_PROXY_default_JMS_host started in: 1ms - bound to [/0.0.0.0:7676]
INFO: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://0.0.0.0:8686/jndi/rmi://0.0.0.0:8686/jmxrmi
INFO: Skipping registration of inhabitant for service reference [org.osgi.service.metatype.MetaTypeProvider] as the service object could not be obtained.
INFO: ResponseFilter.filter(...) payara.doublefilter192.ResponseFilter@cd0095
INFO: ResponseFilter.filter(...) payara.doublefilter192.ResponseFilter@7e1571

@scrmtrey91
Copy link
Author

URL :http://localhost:8080/Backend/ws/data/test

Test case attached:
v10demo.zip

@smillidge smillidge added try to reproduce and removed PR: TESTS REQUIRED PR Requires Tests to be merged labels Jun 3, 2019
@maxencelaurent
Copy link
Contributor

Double instantiation occurs if beans.xml bean-discovery-mode is set to all

Double instantiation occurs with all modes but "none".

Another workaround is to override equals and hashCode methods so that all filter instances are equal:

import javax.ws.rs.ext.Provider;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;

@Provider
public class MyDummyFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext cr) {
       /* ... */
    }

    @Override
    public boolean equals(Object o){
        return o instanceof MyDummyFilter;
    }

    @Override
    public int hashCode() {
        return this.getClass().hashCode();
    }
}

Duplicates are evicted by org.glassfish.jersey.internal.inject.Providers#getAllRankedProviders, line 188 (org.glassfish.jersey.core:jersey-common:2.29.payara-p2)

@pdudits
Copy link
Contributor

pdudits commented Jun 11, 2019

A little more natural workaround is to annotate the filter with @RequestScoped, causing it to be instantiated once even though runtime executes two lookups of it.

I managed to reproduce the issue against Jersey upstream, we'll work on the fix there. (Caused by fix for eclipse-ee4j/jersey#3670)

@pdudits
Copy link
Contributor

pdudits commented Jun 14, 2019

Internally tracked as PAYARA-3908

maxencelaurent added a commit to Heigvd/Wegas that referenced this issue Jun 18, 2019
maxencelaurent added a commit to Heigvd/Wegas that referenced this issue Jun 18, 2019
AlanRoth pushed a commit to AlanRoth/Payara that referenced this issue Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants