private ClientExecChain addFetchEvent ( final ClientExecChain wrapped ) { return new ClientExecChain ( ) { @ Override public CloseableHttpResponse execute ( HttpRoute route , HttpRequestWrapper request , HttpClientContext httpClientContext , HttpExecutionAware execAware ) throws IOException , HttpException { OutgoingRequestContext context = OutgoingRequestContext . adapt ( httpClientContext ) ; // Create request event FetchEvent fetchEvent = new FetchEvent ( context , request ) ; eventManager . fire ( EventManager . EVENT_FETCH_PRE , fetchEvent ) ; if ( fetchEvent . isExit ( ) ) { if ( fetchEvent . getHttpResponse ( ) == null ) { // Provide an error page in order to avoid a NullPointerException fetchEvent . setHttpResponse ( HttpErrorPage . generateHttpResponse ( HttpStatus . SC_INTERNAL_SERVER_ERROR , "An extension stopped the processing of the request without providing a response" ) ) ; } } else { try { fetchEvent . setHttpResponse ( wrapped . execute ( route , request , context , execAware ) ) ; eventManager . fire ( EventManager . EVENT_FETCH_POST , fetchEvent ) ; } catch ( IOException | HttpException e ) { fetchEvent . setHttpResponse ( HttpErrorPage . generateHttpResponse ( e ) ) ; // Usually we want to render and cache the exception but we let an extension decide fetchEvent . setExit ( true ) ; eventManager . fire ( EventManager . EVENT_FETCH_POST , fetchEvent ) ; if ( ! fetchEvent . isExit ( ) ) throw e ; // Throw the exception and let http client process it (may retry) } } return fetchEvent . getHttpResponse ( ) ; } } ; }
Decorate with fetch event managements