use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.
the class CrestProtocolEnforcementFilterTest method requestWithIncorrectProtocolMinorVersionShouldReturnBadRequestResponse.
@Test
public void requestWithIncorrectProtocolMinorVersionShouldReturnBadRequestResponse() throws IOException {
//Given
Context context = mock(Context.class);
Request request = new Request();
Handler next = mock(Handler.class);
request.getHeaders().put(AcceptApiVersionHeader.valueOf("protocol=1.1"));
//When
Response response = filter.filter(context, request, next).getOrThrowUninterruptibly();
//Then
assertThat(getUnsupportedMinorVersionExceptionJson(version(1, 1))).isEqualTo(response.getEntity().getJson());
assertThat(AcceptApiVersionHeader.valueOf(request).getProtocolVersion()).isEqualTo(version(1, 1));
verify(next, never()).handle(context, request);
}
use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.
the class RestRouterIT method shouldNotBePossibleToReachInternalResourceViaChf.
@Test
public void shouldNotBePossibleToReachInternalResourceViaChf() throws Exception {
// Given
Context context = mockContext();
Request request = newRequest("GET", "/json/internal");
// When
Promise<Response, NeverThrowsException> promise = handler.handle(context, request);
// Then
Response response = promise.get();
assertThat(response.getStatus()).isEqualTo(Status.NOT_FOUND);
verifyZeroInteractions(internalResource);
}
use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.
the class AnnotatedMethod method checkMethod.
static AnnotatedMethod checkMethod(Class<?> annotation, Object requestHandler, Method method) {
List<ContextParameter> contextParams = new ArrayList<>();
int requestParam = -1;
for (int i = 0; i < method.getParameterTypes().length; i++) {
Class<?> type = method.getParameterTypes()[i];
for (Annotation paramAnnotation : method.getParameterAnnotations()[i]) {
if (paramAnnotation instanceof Contextual) {
if (Context.class.isAssignableFrom(type)) {
contextParams.add(new ContextParameter(i, (Class<? extends Context>) type));
} else if (Request.class.isAssignableFrom(type)) {
requestParam = i;
}
}
}
}
Function<Object, Promise<Response, NeverThrowsException>, NeverThrowsException> resourceCreator;
if (Promise.class.equals(method.getReturnType())) {
resourceCreator = new PromisedResponseCreator();
} else if (Response.class.equals(method.getReturnType())) {
resourceCreator = new Function<Object, Promise<Response, NeverThrowsException>, NeverThrowsException>() {
@Override
public Promise<Response, NeverThrowsException> apply(Object o) {
return newResultPromise((Response) o);
}
};
} else {
resourceCreator = ResponseCreator.forType(method.getReturnType());
}
return new AnnotatedMethod(annotation.getSimpleName(), requestHandler, method, contextParams, requestParam, method.getParameterTypes().length, resourceCreator);
}
use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.
the class AuthenticationServiceV1 method handleErrorResponse.
/**
* Processes the given Exception into a Restlet response representation or wrap it into
* a ResourceException, which will be thrown.
*
* @param status The status to set the response to.
* @param exception The Exception to be handled.
* @return The Restlet Response Representation.
* @throws ResourceException If the given exception is wrapped in a ResourceException.
*/
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
Reject.ifNull(status);
Response response = new Response(status);
final Map<String, Object> rep = new HashMap<>();
if (exception instanceof RestAuthResponseException) {
final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
response.getHeaders().put(entry.getKey(), entry.getValue());
}
rep.putAll(authResponseException.getJsonResponse().asMap());
} else if (exception instanceof RestAuthException) {
final RestAuthException authException = (RestAuthException) exception;
if (authException.getFailureUrl() != null) {
rep.put("failureUrl", authException.getFailureUrl());
}
rep.put("errorMessage", getLocalizedMessage(request, exception));
} else if (exception == null) {
rep.put("errorMessage", status.getReasonPhrase());
} else {
rep.put("errorMessage", getLocalizedMessage(request, exception));
}
response.setEntity(rep);
return response;
}
use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.
the class AuthenticationServiceV2 method handleErrorResponse.
@Override
protected Response handleErrorResponse(Request request, Status status, Exception exception) {
Reject.ifNull(status);
Response response = new Response(status);
if (exception instanceof RestAuthResponseException) {
final RestAuthResponseException authResponseException = (RestAuthResponseException) exception;
for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) {
response.getHeaders().add(entry.getKey(), entry.getValue());
}
response.setEntity(authResponseException.getJsonResponse().asMap());
return response;
} else if (exception instanceof RestAuthException) {
final RestAuthException rae = (RestAuthException) exception;
ResourceException cause = ResourceException.getException(rae.getStatusCode(), getLocalizedMessage(request, rae));
if (rae.getFailureUrl() != null) {
cause.setDetail(json(object(field("failureUrl", rae.getFailureUrl()))));
}
return createExceptionResponse(response, cause);
} else if (exception == null) {
return createExceptionResponse(response, ResourceException.getException(status.getCode()));
} else {
return createExceptionResponse(response, ResourceException.getException(status.getCode(), exception.getMessage(), exception));
}
}
Aggregations