Class ContextAndRequest


  • public class ContextAndRequest
    extends Object
    This a value class to hold a Context and a Request during the processing of a request. It can be used as a parameter in a Function. Example:
     
     public class EnforcementFilter implements Filter {
    
         private final AsyncFunction<ContextAndRequest, Boolean, Exception> condition;
    
         public EnforcementFilter(AsyncFunction<ContextAndRequest, Boolean, Exception> condition) {
             this.condition = condition;
         }
    
         {@literal @}Override
         public Promise<Response, NeverThrowsException> filter(final Context context, final Request request,
                 final Handler next) {
             return condition.apply(new ContextAndRequest(context, request))
                             .thenAsync(new AsyncFunction<Boolean, Response, NeverThrowsException>() {
                                            {@literal @}Override
                                            public Promise<Response, NeverThrowsException> apply(
                                                    Boolean condition) throws NeverThrowsException {
                                                if (condition) {
                                                    return next.handle(context, request);
                                                }
                                                return newResponsePromise(new Response(Status.FORBIDDEN));
                                            }
                                        },
                                        new AsyncFunction<Exception, Response, NeverThrowsException>() {
                                            {@literal @}Override
                                            public Promise<Response, NeverThrowsException> apply(
                                                    Exception cause) throws NeverThrowsException {
                                                Response response = new Response(Status.INTERNAL_SERVER_ERROR);
                                                response.setCause(cause);
                                                return newResponsePromise(response);
                                            }
                                        });
         }
     }
     
     
    See Also:
    ThrottlingFilter
    • Constructor Detail

      • ContextAndRequest

        public ContextAndRequest​(Context context,
                                 Request request)
        Constructs a new ContextAndRequest.
        Parameters:
        context - the context
        request - the request
    • Method Detail

      • getContext

        public Context getContext()
        Returns the context.
        Returns:
        the context
      • getRequest

        public Request getRequest()
        Returns the request.
        Returns:
        the request