public static Object provideContext ( Class < ? > type , String defaultValue , RoutingContext context ) throws ContextException { // vert.x context if ( type . isAssignableFrom ( HttpServerResponse . class ) ) { return context . response ( ) ; } if ( type . isAssignableFrom ( HttpServerRequest . class ) ) { return context . request ( ) ; } if ( type . isAssignableFrom ( RoutingContext . class ) ) { return context ; } // provide vertx via @Context if ( type . isAssignableFrom ( Vertx . class ) ) { return context . vertx ( ) ; } // provide event bus via @Context if ( type . isAssignableFrom ( EventBus . class ) ) { return context . vertx ( ) . eventBus ( ) ; } if ( type . isAssignableFrom ( User . class ) ) { return context . user ( ) ; } // internal context / reflection of route definition if ( type . isAssignableFrom ( RouteDefinition . class ) ) { return new RouteDefinition ( context ) ; } // browse through context storage if ( context . data ( ) != null && context . data ( ) . size ( ) > 0 ) { Object item = context . data ( ) . get ( getContextKey ( type ) ) ; if ( item != null ) { // found in storage ... return return item ; } } if ( defaultValue != null ) { // check if type has constructor that can be used with defaultValue ... // and create Context type on the fly constructed with defaultValue try { return ClassFactory . constructType ( type , defaultValue ) ; } catch ( ClassFactoryException e ) { throw new ContextException ( "Can't provide @Context of type: " + type + ". " + e . getMessage ( ) ) ; } } throw new ContextException ( "Can't provide @Context of type: " + type ) ; }
Provides vertx context of desired type if possible