Search in sources :

Example 56 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project com-liferay-apio-architect by liferay.

the class FailureFilter method filter.

@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    Try<Object> objectTry = Try.fromFallible(containerResponseContext::getEntity);
    objectTry.map(Failure.class::cast).map(Failure::getException).map(exception -> _errorUtil.getErrorResponse(exception, _request, _httpHeaders)).ifSuccess(response -> {
        containerResponseContext.setStatus(response.getStatus());
        MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
        headers.remove(CONTENT_TYPE);
        MediaType mediaType = response.getMediaType();
        if (mediaType != null) {
            headers.add(CONTENT_TYPE, mediaType.toString());
        }
        Object entity = response.getEntity();
        if (entity != null) {
            containerResponseContext.setEntity(entity);
        }
    });
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) IOException(java.io.IOException) Failure(com.liferay.apio.architect.functional.Try.Failure) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ErrorUtil(com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) HttpHeaders(javax.ws.rs.core.HttpHeaders) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) MediaType(javax.ws.rs.core.MediaType) Failure(com.liferay.apio.architect.functional.Try.Failure)

Example 57 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project crnk-framework by crnk-project.

the class CrnkFilterTest method checkWebApplicationExceptionDoNotGetWrappedWithWebApplicationException.

@Test
public void checkWebApplicationExceptionDoNotGetWrappedWithWebApplicationException() throws IOException {
    CrnkFeature feature = Mockito.mock(CrnkFeature.class);
    CrnkFilter filter = new CrnkFilter(feature);
    ContainerRequestContext requestContext = Mockito.mock(ContainerRequestContext.class);
    Mockito.when(requestContext.getUriInfo()).thenThrow(new WebApplicationException("test"));
    try {
        filter.filter(requestContext);
        Assert.fail();
    } catch (WebApplicationException e) {
        Assert.assertEquals("test", e.getMessage());
        Assert.assertNull(e.getCause());
    }
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 58 with ContainerRequestContext

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

the class ServerSRAFilter 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 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 (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 != null && closeCurrent) {
                lraTrace(requestContext, (URL) newLRA, "ServerLRAFilter after: closing LRA becasue http status is " + status);
                lraClient.cancelSRA(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.commitSRA((URL) newLRA);
        }
    } finally {
        Current.updateLRAContext(responseContext.getHeaders());
        Current.popAll();
    }
}
Also used : Response(javax.ws.rs.core.Response) Status(io.narayana.sra.annotation.Status) SRA_HTTP_HEADER(io.narayana.sra.client.SRAClient.SRA_HTTP_HEADER) Arrays(java.util.Arrays) Provider(javax.ws.rs.ext.Provider) URL(java.net.URL) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Commit(io.narayana.sra.annotation.Commit) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) ResourceInfo(javax.ws.rs.container.ResourceInfo) Map(java.util.Map) URI(java.net.URI) TimeLimit(io.narayana.sra.annotation.TimeLimit) Method(java.lang.reflect.Method) Prepare(io.narayana.sra.annotation.Prepare) Context(javax.ws.rs.core.Context) Status(io.narayana.sra.annotation.Status) IOException(java.io.IOException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) RTS_HTTP_RECOVERY_HEADER(io.narayana.sra.client.SRAClient.RTS_HTTP_RECOVERY_HEADER) Response(javax.ws.rs.core.Response) Annotation(java.lang.annotation.Annotation) OnePhaseCommit(io.narayana.sra.annotation.OnePhaseCommit) SRA(io.narayana.sra.annotation.SRA) WebApplicationException(javax.ws.rs.WebApplicationException) Rollback(io.narayana.sra.annotation.Rollback) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Link(javax.ws.rs.core.Link) URL(java.net.URL)

Example 59 with ContainerRequestContext

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

the class SecurityAuthorizationFilterTest method testDenyAll.

@Test(expected = WebApplicationException.class)
public void testDenyAll() 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(methodDeny);
    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 60 with ContainerRequestContext

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

the class SecurityAuthorizationFilterTest method testRoleAllowedInvalid.

@Test(expected = WebApplicationException.class)
public void testRoleAllowedInvalid() 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 methodRole;
        }

        public Class<?> getResourceClass() {
            return targetResource;
        }
    };
    faf.setInfo(mockResInfo);
    when(mockUriInfo.getPath()).thenReturn("localhost");
    when(mockRequest.getSecurityContext()).thenReturn(new FF4jSecurityContext("user", "", Util.set("OTHER")));
    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)

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