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);
}
});
}
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());
}
}
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();
}
}
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
}
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
}
Aggregations