Search in sources :

Example 26 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthenticationFilterTest method testAuthorizedApiKey.

@Test
public void testAuthorizedApiKey() throws IOException {
    // Define ApiConfig
    FF4jAuthenticationFilter.setApiConfig(new ApiConfig().createApiKey("12", true, true, Util.set("USER")));
    // Given
    FF4jAuthenticationFilter faf = new FF4jAuthenticationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    when(mockRequest.getHeaderString("Authorization")).thenReturn("apiKey=12");
    when(mockRequest.getMethod()).thenReturn("GET");
    UriInfo mockUriInfo = mock(UriInfo.class);
    when(mockUriInfo.getPath()).thenReturn("someURL");
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    // When
    faf.filter(mockRequest);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) FF4jAuthenticationFilter(org.ff4j.web.api.security.FF4jAuthenticationFilter) ApiConfig(org.ff4j.web.ApiConfig) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 27 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthenticationFilterTest method testGETWadl.

@Test
public void testGETWadl() throws IOException {
    // Given
    FF4jAuthenticationFilter faf = new FF4jAuthenticationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    when(mockRequest.getMethod()).thenReturn("GET");
    UriInfo mockUriInfo = mock(UriInfo.class);
    when(mockUriInfo.getPath()).thenReturn("application.wadl");
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    faf.filter(mockRequest);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) FF4jAuthenticationFilter(org.ff4j.web.api.security.FF4jAuthenticationFilter) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 28 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthorizationFilterTest method testRoleNothing.

@Test
public void testRoleNothing() throws IOException {
    // Given
    FF4jAuthorizationFilter faf = new FF4jAuthorizationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    UriInfo mockUriInfo = mock(UriInfo.class);
    ResourceInfo mockResInfo = new ResourceInfo() {

        public Method getResourceMethod() {
            return methodNothing;
        }

        public Class<?> getResourceClass() {
            return targetResource;
        }
    };
    faf.setInfo(mockResInfo);
    when(mockUriInfo.getPath()).thenReturn("localhost");
    when(mockRequest.getSecurityContext()).thenReturn(new FF4jSecurityContext("user", "", Util.set("USER")));
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    // When
    faf.filter(mockRequest);
// OK
}
Also used : FF4jAuthorizationFilter(org.ff4j.web.api.security.FF4jAuthorizationFilter) ResourceInfo(javax.ws.rs.container.ResourceInfo) FF4jSecurityContext(org.ff4j.web.api.security.FF4jSecurityContext) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 29 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthorizationFilterTest method testPermitAll.

@Test
public void testPermitAll() throws IOException {
    // Given
    FF4jAuthorizationFilter faf = new FF4jAuthorizationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    UriInfo mockUriInfo = mock(UriInfo.class);
    ResourceInfo mockResInfo = mock(ResourceInfo.class);
    when(mockResInfo.getResourceMethod()).thenReturn(methodPermit);
    faf.setInfo(mockResInfo);
    when(mockUriInfo.getPath()).thenReturn("localhost");
    when(mockRequest.getSecurityContext()).thenReturn(new FF4jSecurityContext("user", "", Util.set("USER")));
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    // When
    faf.filter(mockRequest);
// Then expecte 403
}
Also used : FF4jAuthorizationFilter(org.ff4j.web.api.security.FF4jAuthorizationFilter) ResourceInfo(javax.ws.rs.container.ResourceInfo) FF4jSecurityContext(org.ff4j.web.api.security.FF4jSecurityContext) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 30 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project narayana by jbosstm.

the class ServerLRAFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // a request is leaving the container so clear any context on the thread and fix up the LRA response header
    Object suspendedLRA = Current.getState("suspendedLRA");
    Object newLRA = Current.getState("newLRA");
    URL current = Current.peek();
    try {
        if (current != null) {
            int status = responseContext.getStatus();
            Response.Status.Family[] cancel0nFamily = (Response.Status.Family[]) requestContext.getProperty(CANCEL_ON_FAMILY_PROP);
            Response.Status[] cancel0n = (Response.Status[]) requestContext.getProperty(CANCEL_ON_PROP);
            Boolean closeCurrent = (Boolean) requestContext.getProperty(TERMINAL_LRA_PROP);
            if (closeCurrent == null)
                closeCurrent = false;
            if (cancel0nFamily != null)
                if (Arrays.stream(cancel0nFamily).anyMatch(f -> Response.Status.Family.familyOf(status) == f))
                    closeCurrent = true;
            if (cancel0n != null && !closeCurrent)
                if (Arrays.stream(cancel0n).anyMatch(f -> status == f.getStatusCode()))
                    closeCurrent = true;
            if (closeCurrent) {
                lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA becasue http status is " + status);
                lraClient.cancelLRA(current);
                if (current.equals(newLRA))
                    // don't try to cancle newKRA twice
                    newLRA = null;
            }
        }
        if (newLRA != null) {
            lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA");
            lraClient.closeLRA((URL) newLRA);
        }
        if (responseContext.getStatus() == Response.Status.OK.getStatusCode() && NarayanaLRAClient.isAsyncCompletion(resourceInfo.getResourceMethod())) {
            LRALogger.i18NLogger.warn_lraParticipantqForAsync(resourceInfo.getResourceMethod().getDeclaringClass().getName(), resourceInfo.getResourceMethod().getName(), Response.Status.ACCEPTED.getStatusCode(), Response.Status.OK.getStatusCode());
        }
    } finally {
        if (suspendedLRA != null)
            Current.push((URL) suspendedLRA);
        Current.updateLRAContext(responseContext.getHeaders());
        Current.popAll();
    }
}
Also used : Response(javax.ws.rs.core.Response) Status(io.narayana.lra.annotation.Status) LEAVE(io.narayana.lra.client.NarayanaLRAClient.LEAVE) Arrays(java.util.Arrays) COMPENSATE(io.narayana.lra.client.NarayanaLRAClient.COMPENSATE) Provider(javax.ws.rs.ext.Provider) URL(java.net.URL) IllegalLRAStateException(io.narayana.lra.client.IllegalLRAStateException) GenericLRAException(io.narayana.lra.client.GenericLRAException) Complete(io.narayana.lra.annotation.Complete) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) LRA_HTTP_HEADER(io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_HEADER) Compensate(io.narayana.lra.annotation.Compensate) Forget(io.narayana.lra.annotation.Forget) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inject(javax.inject.Inject) Leave(io.narayana.lra.annotation.Leave) ResourceInfo(javax.ws.rs.container.ResourceInfo) Current(io.narayana.lra.client.Current) Map(java.util.Map) FORGET(io.narayana.lra.client.NarayanaLRAClient.FORGET) Status(io.narayana.lra.annotation.Status) URI(java.net.URI) LRA(io.narayana.lra.annotation.LRA) Method(java.lang.reflect.Method) STATUS(io.narayana.lra.client.NarayanaLRAClient.STATUS) Context(javax.ws.rs.core.Context) TimeLimit(io.narayana.lra.annotation.TimeLimit) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NestedLRA(io.narayana.lra.annotation.NestedLRA) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) NarayanaLRAClient(io.narayana.lra.client.NarayanaLRAClient) COMPLETE(io.narayana.lra.client.NarayanaLRAClient.COMPLETE) Response(javax.ws.rs.core.Response) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) LRALogger(io.narayana.lra.logging.LRALogger) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) LRA_HTTP_RECOVERY_HEADER(io.narayana.lra.client.NarayanaLRAClient.LRA_HTTP_RECOVERY_HEADER) URL(java.net.URL)

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)81 Test (org.junit.Test)40 Response (javax.ws.rs.core.Response)32 UriInfo (javax.ws.rs.core.UriInfo)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Test (org.junit.jupiter.api.Test)18 Resource (org.glassfish.jersey.server.model.Resource)15 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)14 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 File (java.io.File)8 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)8 ResourceInfo (javax.ws.rs.container.ResourceInfo)8 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)8 IOException (java.io.IOException)7 URI (java.net.URI)6 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)6 FF4jAuthenticationFilter (org.ff4j.web.api.security.FF4jAuthenticationFilter)6 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 ApiKeyValidatorFilter (org.ff4j.web.api.filter.ApiKeyValidatorFilter)5