Search in sources :

Example 21 with Promise

use of org.forgerock.util.promise.Promise 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);
}
Also used : Context(org.forgerock.services.context.Context) ArrayList(java.util.ArrayList) Request(org.forgerock.http.protocol.Request) Annotation(java.lang.annotation.Annotation) NeverThrowsException(org.forgerock.util.promise.NeverThrowsException) Response(org.forgerock.http.protocol.Response) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) Function(org.forgerock.util.Function)

Aggregations

Promise (org.forgerock.util.promise.Promise)21 ResourceException (org.forgerock.json.resource.ResourceException)18 JsonValue (org.forgerock.json.JsonValue)14 Promises.newResultPromise (org.forgerock.util.promise.Promises.newResultPromise)14 ArrayList (java.util.ArrayList)11 ResourceResponse (org.forgerock.json.resource.ResourceResponse)9 List (java.util.List)8 BadRequestException (org.forgerock.json.resource.BadRequestException)8 ActionResponse (org.forgerock.json.resource.ActionResponse)7 QueryRequest (org.forgerock.json.resource.QueryRequest)7 SSOToken (com.iplanet.sso.SSOToken)6 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)6 NotFoundException (org.forgerock.json.resource.NotFoundException)6 RealmContext (org.forgerock.openam.rest.RealmContext)6 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)5 QueryResponse (org.forgerock.json.resource.QueryResponse)5 Context (org.forgerock.services.context.Context)5 Pair (org.forgerock.util.Pair)5 IdentityDetails (com.sun.identity.idsvcs.IdentityDetails)4 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)4