1515 */
1616package org .commonjava .indy .bind .jaxrs .util ;
1717
18+ import org .slf4j .Logger ;
1819import org .slf4j .LoggerFactory ;
1920
2021import javax .interceptor .AroundInvoke ;
2324import javax .ws .rs .Path ;
2425import java .nio .file .Paths ;
2526
27+ import static java .lang .System .currentTimeMillis ;
28+ import static org .apache .commons .lang3 .StringUtils .isNotBlank ;
2629import static org .commonjava .indy .util .RequestContextHelper .REST_CLASS ;
2730import static org .commonjava .indy .util .RequestContextHelper .REST_CLASS_PATH ;
2831import static org .commonjava .indy .util .RequestContextHelper .REST_ENDPOINT_PATH ;
3437@ REST
3538public class RestInterceptor
3639{
40+ /**
41+ * Interceptor decorating MDC. Log the beginning/end of REST request and time elapsed.
42+ */
3743 @ AroundInvoke
3844 public Object operation ( InvocationContext context ) throws Exception
3945 {
46+ final Logger logger = LoggerFactory .getLogger ( context .getTarget ().getClass () );
47+
4048 Class <?> targetClass = context .getTarget ().getClass ();
41- Path classAnno = null ;
49+ Path classAnno ;
4250 do
4351 {
4452 classAnno = targetClass .getAnnotation ( Path .class );
4553 targetClass = targetClass .getSuperclass ();
4654 }
4755 while ( classAnno == null && targetClass != null );
4856
57+ String endpointPath = null ;
58+
4959 if ( getContext ( REST_CLASS ) == null )
5060 {
5161 String targetName = context .getMethod ().getDeclaringClass ().getSimpleName ();
@@ -64,15 +74,29 @@ public Object operation( InvocationContext context ) throws Exception
6474 String methodPath = methAnno .value ();
6575 setContext ( REST_METHOD_PATH , methodPath );
6676
67- String endpointPath = Paths .get ( classPath , methodPath ).toString ();
77+ endpointPath = Paths .get ( classPath , methodPath ).toString ();
6878 setContext ( REST_ENDPOINT_PATH , endpointPath );
6979 }
7080 }
7181
72- LoggerFactory . getLogger ( context . getTarget (). getClass () ) .trace ( "Interceptor decorating MDC." );
82+ //logger .trace( "Interceptor decorating MDC." );
7383
74- return context .proceed ();
84+ final boolean isEndpoint = isNotBlank ( endpointPath );
85+ final long begin = currentTimeMillis ();
86+ try
87+ {
88+ if ( isEndpoint )
89+ {
90+ logger .info ( "Start REST: {}" , endpointPath );
91+ }
92+ return context .proceed ();
93+ }
94+ finally
95+ {
96+ if ( isEndpoint )
97+ {
98+ logger .info ( "End REST: {}, elapsed: {}ms" , endpointPath , ( currentTimeMillis () - begin ) );
99+ }
100+ }
75101 }
76-
77-
78102}
0 commit comments