Java Code Examples for javax.ws.rs.container.ContainerRequestFilter#filter()

The following examples show how to use javax.ws.rs.container.ContainerRequestFilter#filter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Test
public void customPrincipalFunctionUserInfo() throws Exception {
    final Key<String> userInfoKey = newKey("basicPrincipal");
    final ContainerRequestFilter filter = newFilterBuilder(userInfoKey)
            .principalFunction((__, userInfo) -> TEST_USER_INFO.equals(userInfo) ? TEST_PRINCIPAL : null)
            .build();

    filter.filter(requestCtx);
    verifyZeroInteractions(requestCtx);

    AsyncContext.put(userInfoKey, TEST_USER_INFO);
    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue().getUserPrincipal(), is(sameInstance(TEST_PRINCIPAL)));
}
 
Example 2
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Test
public void customSecurityContextFunctionUserInfo() throws Exception {
    final SecurityContext securityContext = mock(SecurityContext.class);

    final Key<String> userInfoKey = newKey("basicPrincipal");
    final ContainerRequestFilter filter = newFilterBuilder(userInfoKey)
            .securityContextFunction((__, userInfo) -> TEST_USER_INFO.equals(userInfo) ? securityContext : null)
            .build();

    filter.filter(requestCtx);
    verifyZeroInteractions(requestCtx);

    AsyncContext.put(userInfoKey, TEST_USER_INFO);
    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue(), is(sameInstance(securityContext)));
}
 
Example 3
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void principalNoUserInfo() throws Exception {
    final ContainerRequestFilter filter = newFilterBuilder().build();

    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue().getUserPrincipal(), is(sameInstance(ANONYMOUS_PRINCIPAL)));
}
 
Example 4
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void principalUserInfo() throws Exception {
    final Key<Principal> userInfoKey = newKey("basicPrincipal");
    final ContainerRequestFilter filter = newFilterBuilder(userInfoKey).build();

    filter.filter(requestCtx);
    verifyZeroInteractions(requestCtx);

    AsyncContext.put(userInfoKey, TEST_PRINCIPAL);
    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue().getUserPrincipal(), is(sameInstance(TEST_PRINCIPAL)));
}
 
Example 5
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void customPrincipalFunctionNoUserInfo() throws Exception {
    final ContainerRequestFilter filter = newFilterBuilder()
            .principalFunction(__ -> TEST_PRINCIPAL)
            .build();

    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue().getUserPrincipal(), is(sameInstance(TEST_PRINCIPAL)));
}
 
Example 6
Source File: BasicAuthSecurityContextFiltersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void customSecurityContextFunctionNoUserInfo() throws Exception {
    final SecurityContext securityContext = mock(SecurityContext.class);

    final ContainerRequestFilter filter = newFilterBuilder()
            .securityContextFunction(__ -> securityContext)
            .build();

    final ArgumentCaptor<SecurityContext> securityCtxCaptor = ArgumentCaptor.forClass(SecurityContext.class);
    filter.filter(requestCtx);

    verify(requestCtx).setSecurityContext(securityCtxCaptor.capture());
    assertThat(securityCtxCaptor.getValue(), is(sameInstance(securityContext)));
}