private ResponseHandler < String > getJsonResponseHandler ( final String urlCalledParam ) { // Create a custom response handler ResponseHandler < String > responseHandler = new ResponseHandler < String > ( ) { /** * Process the {@code responseParam} and return text if valid. * * @param responseParam The HTTP response from the server. * @return Text response. * @throws IOException If there are any communication or I/O problems. */ public String handleResponse ( final HttpResponse responseParam ) throws IOException { int status = responseParam . getStatusLine ( ) . getStatusCode ( ) ; if ( status == 404 ) { throw new FluidClientException ( "Endpoint for Service not found. URL [" + urlCalledParam + "]." , FluidClientException . ErrorCode . CONNECT_ERROR ) ; } else if ( status >= 200 && status < 300 ) { HttpEntity entity = responseParam . getEntity ( ) ; String responseJsonString = ( entity == null ) ? null : EntityUtils . toString ( entity ) ; return responseJsonString ; } else if ( status == 400 ) { //Bad Request... Server Side Error meant for client... HttpEntity entity = responseParam . getEntity ( ) ; String responseJsonString = ( entity == null ) ? null : EntityUtils . toString ( entity ) ; return responseJsonString ; } else { HttpEntity entity = responseParam . getEntity ( ) ; String responseString = ( entity != null ) ? EntityUtils . toString ( entity ) : null ; throw new FluidClientException ( "Unexpected response status: " + status + ". " + responseParam . getStatusLine ( ) . getReasonPhrase ( ) + ". \nResponse Text [" + responseString + "]" , FluidClientException . ErrorCode . IO_ERROR ) ; } } } ; return responseHandler ; }
Get a text based response handler used mainly for JSON .