Skip to content

Instantly share code, notes, and snippets.

@olkunmustafa
Created September 3, 2016 07:21
Show Gist options
  • Save olkunmustafa/a4ff3c2812236d2bd86d3ad5f3a523ad to your computer and use it in GitHub Desktop.
Save olkunmustafa/a4ff3c2812236d2bd86d3ad5f3a523ad to your computer and use it in GitHub Desktop.
/**
* This filter uses to add new Header elements
* or it can be added new Log elements.
*
* @since 0.1.1
*/
private class ProgressFilter implements ServiceFilter {
/**
* @param request Request object
* @return Log message
* @since 0.1.1
*/
@NonNull
private String getRequest( ServiceFilterRequest request ) {
StringBuilder requestMessage = new StringBuilder( "====== REQUEST START ======" );
requestMessage.append( "\n" );
requestMessage.append( "URL : " );
requestMessage.append( request.getUrl() );
requestMessage.append( "\n" );
requestMessage.append( "\n" );
requestMessage.append( "Headers : " );
requestMessage.append( request.getHeaders() );
requestMessage.append( "\n" );
requestMessage.append( "Content : " );
requestMessage.append( request.getContent() );
requestMessage.append( "\n" );
requestMessage.append( "====== REQUEST END ======" );
return requestMessage.toString();
}
@NonNull
private String getResponse( ServiceFilterResponse response ) {
StringBuilder responseMessage = new StringBuilder( "====== RESPONSE START ======" );
responseMessage.append( "\n" );
responseMessage.append( "Headers : " );
responseMessage.append( response.getHeaders() );
responseMessage.append( "\n" );
responseMessage.append( "Content : " );
responseMessage.append( response.getContent() );
responseMessage.append( "\n" );
responseMessage.append( "====== RESPONSE END ======" );
return responseMessage.toString();
}
@Override
public ListenableFuture< ServiceFilterResponse > handleRequest( ServiceFilterRequest request, NextServiceFilterCallback next ) {
final SettableFuture< ServiceFilterResponse > resultFuture = SettableFuture.create();
ListenableFuture< ServiceFilterResponse > future = next.onNext( request );
Logger.t( "REQUEST" ).i( getRequest( request ) );
Futures.addCallback( future, new FutureCallback< ServiceFilterResponse >() {
@Override
public void onFailure( Throwable e ) {
resultFuture.setException( e );
e.printStackTrace();
Logger.t( "RESPONSE" ).i( e.getMessage() );
}
@Override
public void onSuccess( ServiceFilterResponse response ) {
resultFuture.set( response );
Logger.t( "RESPONSE" ).i( getResponse( response ) );
}
} );
return resultFuture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment