public static RequestAndResponseData of ( HttpRequestBase request , HttpEntitySnapshot requestEntity , HttpResponse response , HttpEntitySnapshot responseEntity , Set < Trace > traces ) throws IOException { RequestData . Builder requestBuilder = new RequestData . Builder ( ) ; ResponseData . Builder responseBuilder = new ResponseData . Builder ( ) ; if ( request != null ) { requestBuilder . withCommand ( request . getMethod ( ) + " " + request . getURI ( ) ) ; boolean binaryBody = false ; if ( traces . contains ( Trace . RequestHeaders ) && request . getAllHeaders ( ) != null ) { requestBuilder . withHeaders ( ) ; for ( Header header : request . getAllHeaders ( ) ) { String headerName = header . getName ( ) ; String headerValue = header . getValue ( ) ; if ( "Authorization" . equals ( headerName ) && headerValue . length ( ) > 0 ) { headerValue = "Bearer ****" + headerValue . substring ( Math . max ( 0 , headerValue . length ( ) - 4 ) ) ; } else if ( "Content-Disposition" . equals ( headerName ) ) { binaryBody = true ; } requestBuilder . addHeader ( headerName , headerValue ) ; } } if ( requestEntity != null ) { if ( traces . contains ( Trace . RequestBody ) ) { requestBuilder . setBody ( binaryBody ? binaryBody ( requestEntity ) : getContentAsText ( requestEntity ) ) ; } else if ( traces . contains ( Trace . RequestBodySummary ) ) { requestBuilder . setBody ( binaryBody ? binaryBody ( requestEntity ) : truncateAsNeeded ( getContentAsText ( requestEntity ) , TRUNCATE_LENGTH ) ) ; } } } if ( response != null ) { boolean binaryBody = false ; responseBuilder . withStatus ( response . getStatusText ( ) ) ; if ( traces . contains ( Trace . ResponseHeaders ) && response . getHeaders ( ) != null ) { responseBuilder . withHeaders ( ) ; for ( Map . Entry < String , String > header : response . getHeaders ( ) . entrySet ( ) ) { String headerName = header . getKey ( ) ; String headerValue = header . getValue ( ) ; if ( "Content-Disposition" . equals ( headerName ) ) { binaryBody = true ; } responseBuilder . addHeader ( headerName , headerValue ) ; } } if ( responseEntity != null ) { if ( traces . contains ( Trace . ResponseBody ) ) { responseBuilder . setBody ( binaryBody ? binaryBody ( responseEntity ) : getContentAsText ( responseEntity ) ) ; } else if ( traces . contains ( Trace . ResponseBodySummary ) ) { responseBuilder . setBody ( binaryBody ? binaryBody ( responseEntity ) : truncateAsNeeded ( getContentAsText ( responseEntity ) , TRUNCATE_LENGTH ) ) ; } } } return new RequestAndResponseData ( requestBuilder . build ( ) , responseBuilder . build ( ) ) ; }
factory method for creating a RequestAndResponseData object from request and response data with the specifid trace fields