private IAsyncResultHandler < IApiConnectionResponse > createApiConnectionResponseHandler ( ) { return ( IAsyncResult < IApiConnectionResponse > result ) -> { if ( result . isSuccess ( ) ) { requestMetric . setApiEnd ( new Date ( ) ) ; // The result came back. NB: still need to put it through the response chain. apiConnectionResponse = result . getResult ( ) ; ApiResponse apiResponse = apiConnectionResponse . getHead ( ) ; context . setAttribute ( "apiman.engine.apiResponse" , apiResponse ) ; //$NON-NLS-1$ // Execute the response chain to evaluate the response. responseChain = createResponseChain ( ( ApiResponse response ) -> { // Send the api response to the caller. final EngineResultImpl engineResult = new EngineResultImpl ( response ) ; engineResult . setConnectorResponseStream ( apiConnectionResponse ) ; resultHandler . handle ( AsyncResultImpl . create ( engineResult ) ) ; // We've come all the way through the response chain successfully responseChain . bodyHandler ( buffer -> { requestMetric . setBytesDownloaded ( requestMetric . getBytesDownloaded ( ) + buffer . length ( ) ) ; engineResult . write ( buffer ) ; } ) ; responseChain . endHandler ( isEnd -> { engineResult . end ( ) ; finished = true ; metrics . record ( requestMetric ) ; } ) ; // Signal to the connector that it's safe to start transmitting data. apiConnectionResponse . transmit ( ) ; } ) ; // Write data from the back-end response into the response chain. apiConnectionResponse . bodyHandler ( buffer -> responseChain . write ( buffer ) ) ; // Indicate back-end response is finished to the response chain. apiConnectionResponse . endHandler ( isEnd -> responseChain . end ( ) ) ; responseChain . doApply ( apiResponse ) ; } else { resultHandler . handle ( AsyncResultImpl . create ( result . getError ( ) ) ) ; } } ; }
Creates a response handler that is called by the api connector once a connection to the back end api has been made and a response received .