use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class PolicyResourceTest method shouldReportBadCreateRequests.
@Test
public void shouldReportBadCreateRequests() throws Exception {
// Given
String id = "uniqueId";
JsonValue json = new JsonValue("");
CreateRequest request = mockCreateRequest(id, json);
given(mockParser.parsePolicy(id, json)).willThrow(new EntitlementException(EntitlementException.INVALID_JSON, "Mock error message"));
// When
Promise<ResourceResponse, ResourceException> promise = policyResource.createInstance(mockServerContext, request);
// Then
assertThat(promise).failedWithException().isInstanceOf(BadRequestException.class);
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class PolicyResourceTest method shouldDeletePoliciesFromStore.
@Test
public void shouldDeletePoliciesFromStore() throws Exception {
// Given
String id = "uniqueId";
DeleteRequest request = mock(DeleteRequest.class);
// When
Promise<ResourceResponse, ResourceException> promise = policyResource.deleteInstance(mockServerContext, id, request);
// Then
verify(mockStore).delete(id);
assertThat(promise).succeeded().withId().isNotNull();
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class XMLResourceExceptionHandler method write.
@Override
public void write(MessageContext context, AuthenticationException exception) {
Reject.ifNull(exception);
try {
ResourceException jre;
if (exception instanceof AuthenticationFailedException) {
jre = new PermanentException(Status.UNAUTHORIZED.getCode(), exception.getMessage(), null);
} else if (exception.getCause() instanceof ResourceException) {
jre = (ResourceException) exception.getCause();
} else {
LOGGER.error(exception.getMessage(), exception);
jre = new InternalServerErrorException("Authentication Failed", exception);
}
AuditTrail auditTrail = context.getAuditTrail();
List<Map<String, Object>> failureReasonList = auditTrail.getFailureReasons();
if (failureReasonList != null && !failureReasonList.isEmpty()) {
jre.setDetail(json(object(field("failureReasons", failureReasonList))));
}
Response response = context.getResponse();
response.setStatus(Status.valueOf(jre.getCode()));
context.<Response>getResponse().getHeaders().put(ContentTypeHeader.valueOf(MediaType.XML_UTF_8.toString()));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Transformer transformer = XMLUtils.getTransformerFactory().newTransformer();
transformer.transform(new DOMSource(asXMLDOM(jre.includeCauseInJsonValue().toJsonValue().asMap())), new StreamResult(outputStream));
response.getEntity().setBytes(outputStream.toByteArray());
} catch (TransformerException e1) {
throw new IllegalStateException("Could not write XML to response", e1);
}
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class RealmContextFilter method filterUpdate.
@Override
public Promise<ResourceResponse, ResourceException> filterUpdate(Context context, UpdateRequest request, RequestHandler next) {
try {
Context newContext = evaluate(context, request);
UpdateRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfUpdateRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
return next.handleUpdate(newContext, evaluatedRequest);
} catch (ResourceException e) {
return newExceptionPromise(e);
}
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class RealmContextFilter method filterCreate.
@Override
public Promise<ResourceResponse, ResourceException> filterCreate(Context context, CreateRequest request, RequestHandler next) {
try {
Context newContext = evaluate(context, request);
CreateRequest evaluatedRequest = wasEvaluated(context, newContext) ? copyOfCreateRequest(request).setResourcePath(newContext.asContext(UriRouterContext.class).getRemainingUri()) : request;
return next.handleCreate(evaluate(context, request), evaluatedRequest);
} catch (ResourceException e) {
return newExceptionPromise(e);
}
}
Aggregations