use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.
the class PendingRequestEmailTemplate method resolveScope.
private String resolveScope(String scope, Locale locale) {
if (URI.create(scope).getScheme() != null) {
Request request = new Request().setMethod("GET").setUri(URI.create(scope));
request.getHeaders().put("Accept-Language", locale.toLanguageTag());
Response response = client.send(request).getOrThrowUninterruptibly();
if (Status.OK.equals(response.getStatus())) {
try {
JsonValue json = json(response.getEntity().getJson());
if (json.isDefined("name") && json.get("name").isString()) {
return json.get("name").asString();
}
} catch (IOException e) {
debug.warning("Failed to parse Scope description JSON", e);
}
}
}
return scope;
}
use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.
the class HandlerProviderTest method shouldNotCallInjectorHolderTwice.
@Test
public void shouldNotCallInjectorHolderTwice() {
//Given
Context context = new RootContext();
Request request = new Request();
//When
handlerProvider.handle(context, request);
handlerProvider.handle(context, request);
//Then
assertThat(handlerReturnCount.get()).isEqualTo(1);
verify(handler, times(2)).handle(context, request);
}
use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.
the class HandlerProviderTest method shouldCallInjectorHolderOnFirstCallToHandler.
@Test
public void shouldCallInjectorHolderOnFirstCallToHandler() {
//Given
Context context = new RootContext();
Request request = new Request();
//When
handlerProvider.handle(context, request);
//Then
assertThat(handlerReturnCount.get()).isEqualTo(1);
verify(handler).handle(context, request);
}
use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.
the class Endpoints method from.
/**
* Produce a {@code Handler} from the annotated methods on the provided object.
* <p>
* This method currently only distinguishes requests by their method type. In future this
* should be extended to support selection by request and response media types, and request
* path.
* @param obj The object containing annotated methods.
* @return A new {@code Handler}.
*/
public static Handler from(final Object obj) {
final Map<String, AnnotatedMethod> methods = new HashMap<>();
methods.put("DELETE", AnnotatedMethod.findMethod(obj, Delete.class));
methods.put("GET", AnnotatedMethod.findMethod(obj, Get.class));
methods.put("POST", AnnotatedMethod.findMethod(obj, Post.class));
methods.put("PUT", AnnotatedMethod.findMethod(obj, Put.class));
return new Handler() {
@Override
public Promise<Response, NeverThrowsException> handle(Context context, Request request) {
AnnotatedMethod method = methods.get(getMethod(request));
if (method == null) {
Response response = new Response(Status.METHOD_NOT_ALLOWED);
response.setEntity(new NotSupportedException().toJsonValue().getObject());
return newResultPromise(response);
}
return method.invoke(context, request);
}
};
}
use of org.forgerock.http.protocol.Request in project OpenAM by OpenRock.
the class AuthenticationServiceV2Test method shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage.
@Test
public void shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage() throws IOException {
// given
Request httpRequest = new Request();
RestAuthException testException = new RestAuthException(401, "Invalid Password!!");
testException.setFailureUrl("http://localhost:8080");
// when
Response response = authServiceV2.handleErrorResponse(httpRequest, Status.valueOf(401), testException);
// then
assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED);
JsonValue responseBody = json(response.getEntity().getJson());
assertThat(responseBody).integerAt("code").isEqualTo(401);
assertThat(responseBody).stringAt("reason").isEqualTo("Unauthorized");
assertThat(responseBody).stringAt("message").isEqualTo("Invalid Password!!");
assertThat(responseBody).stringAt("detail/failureUrl").isEqualTo("http://localhost:8080");
}
Aggregations