thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jools <jool...@gmail.com>
Subject Re: Intercepting thrift messages before they reach the service methods
Date Fri, 17 Jul 2015 09:00:45 GMT
In it's most simple form you can simply call

*@Override*

* public Object invoke(final MethodInvocation invocation) throws Throwable
{*

*return invocation.proceed();*

*}*

All exceptions thrown will be propagated back to the caller. This includes
run time exceptions, which will depending on Transport you are using result
in a failed call.


Should you wish to say catch a particular exception and create a thrift
exception instead, you can simply do something like this :-

*@Override*

*public Object invoke(final MethodInvocation invocation) throws Throwable {*

*try {*

*return invocation.proceed();*


*} catch (RuntimeException re) {*

*// Process the exception*

*throw new TException(re.getMessage());*

*}*

*}*


A word of warning, you can't return a *null* from a service method, this
will result in an exception being raised which can be very confusing. So if
you do catch an exception, it's generally best to throw a TException (or
one based on a TException) so that the call completes and the caller is
informed of the error.


Hope that helps a bit :-)


--J



On 16 July 2015 at 18:54, Hasini Gunasinghe <hasi7786@gmail.com> wrote:

> Hi Jools,
>
> How did you get it to properly propagate the exceptions thrown in the
> "invoke" method of the MethodInterceptor to the client side?
> Although I throw a custom exception with a proper error message in the
> "invoke" method of the the MethodInterceptor (for unauthorized calls), what
> thrift propagates to the client side is a general exception:
> org.apache.thrift.TApplicationException: Internal error processing <method
> name>
>
> Appreciate any insights on resolving this.
>
> Thanks & Best Regards,
> Hasini.
>
>
>
> On Thu, Jul 16, 2015 at 7:37 AM, Jools <joolski@gmail.com> wrote:
>
> > My pleasure
> >
> > --Jools
> >
> > On 16 July 2015 at 11:07, Hasini Gunasinghe <hasi7786@gmail.com> wrote:
> >
> > > Hi Jools,
> > >
> > > Thank you very much. It worked.
> > >
> > > Best Regards,
> > > Hasini.
> > >
> > > On Wed, Jul 15, 2015 at 6:06 AM, Jools <joolski@gmail.com> wrote:
> > >
> > > > We use Google Guice to accomplish this, I'm sure you can use any of
> the
> > > AOP
> > > > frameworks,
> > > >
> > > > So, first thing you'll need to do is create an annotation.
> > > >
> > > >
> > > > *import java.lang.annotation.ElementType;*
> > > >
> > > > *import java.lang.annotation.Retention;*
> > > >
> > > > *import java.lang.annotation.RetentionPolicy;*
> > > >
> > > > *import java.lang.annotation.Target;*
> > > >
> > > >
> > > >  *import com.google.inject.BindingAnnotation;*
> > > >
> > > >
> > > >  *@Retention(RetentionPolicy.RUNTIME)*
> > > >
> > > > *@Target({ElementType.METHOD})*
> > > >
> > > > *@BindingAnnotation*
> > > >
> > > > *public @interface ServiceCall {*
> > > >
> > > > *// Add additional annotations here for meta data etc.*
> > > >
> > > >
> > > > *}*
> > > >
> > > > Add this annotation to all the service methods which require a
> callback
> > > to
> > > > be attached.
> > > >
> > > > All out service methods are defined with an authentication token on
> the
> > > > first argument, the service methods themselves don't use the token,
> but
> > > an
> > > > interceptor will interrogate the token to ensure it's valid before
> the
> > > > service method is invoked.
> > > >
> > > >
> > > > *@Override*
> > > >
> > > > * @ServiceCall*
> > > >
> > > > * public List<DataRecord> findDataRecords(final AuthToken authToken,
> > > final
> > > > ByteBuffer startKey, final int limit, final boolean includeContent)
> {*
> > > >
> > > > * return dataRecordHelper.findAll(startKey, limit, includeContent);*
> > > >
> > > > * }*
> > > >
> > > >
> > > > Now for the interceptor.
> > > >
> > > >
> > > > *public class ThriftServiceCallMethodInterceptor implements
> > > > MethodInterceptor {*
> > > >
> > > > * @Override*
> > > >
> > > > * public Object invoke(final MethodInvocation invocation) throws
> > > Throwable
> > > > {*
> > > >
> > > > *// do your thing here*
> > > >
> > > > *return invocation.proceed();*
> > > >
> > > > *}*
> > > >
> > > > *}*
> > > >
> > > >
> > > > Now we need to tell Guice about the interceptor, this code lives in
> the
> > > > configure() method of an AbstractModule.
> > > >
> > > >
> > > > *final ThriftServiceCallMethodInterceptor interceptor = new
> > > > ThriftServiceCallMethodInterceptor();*
> > > >
> > > > * requestInjection(interceptor);*
> > > >
> > > > * bindInterceptor(Matchers.any(), *
> > > >
> > > > *     Matchers.annotatedWith(ServiceCall.class), *
> > > >
> > > > *     interceptor);*
> > > >
> > > >
> > > > You will now need to create an AbstractModule for each of your
> service
> > > > implementations.
> > > >
> > > >
> > > > --Jools
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > On 15 July 2015 at 09:44, Hasini Gunasinghe <hasi7786@gmail.com>
> > wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > I want to apply some security checks on the thrift method calls,
> > before
> > > > the
> > > > > message hits the thrift service method. (similar to an interceptor,
> > > > handler
> > > > > or filter)
> > > > >
> > > > > Is it possible to do this on the server side of thrift (java)?
> > > > >
> > > > > From searching, the closest that I found was the filter implemented
> > by
> > > > > Finagle :
> > > > > http://stevenskelton.ca/developer-friendly-thrift-request-logging/
> > > > >
> > > > > But I could not figure out if it is possible to do similar thing
in
> > > java.
> > > > >
> > > > > I would appreciate any insights on this.
> > > > >
> > > > > Thanks & Best Regards,
> > > > > Hasini.
> > > > >
> > > >
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message