package interceptor;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import adapters.InterfaceToClass;
import lifting.ClientLifterCaller;
/**
* This interceptor implements the JAX-RS ReaderInterceptor. It is added client-side and
* is called when the client receives a response from the server.
*
* When this filter is called, it calls the lifting algorithm used client-side. The latter
* compares the resource (the xml representing the entity, found in the context) with
* what is expected in the client, which contains a proxy of the server. The expected resource
* is given in argument of the client lifter caller (ClientLifterCaller), in this class. It
* corresponds to the return type of the service called.
*
* @author Raphaël Martignoni
*
*/
@Provider
public class LiftingInterceptor implements ReaderInterceptor{
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
throws IOException, WebApplicationException {
System.out.println("Debut intercepteur de la réponse");
Type expectedType = context.getGenericType();
InterfaceToClass adpt;
if(expectedType instanceof ParameterizedType){
ParameterizedType typeP=(ParameterizedType) expectedType;
adpt = new InterfaceToClass(((Class<?>)typeP.getActualTypeArguments()[0]).getPackage());
}
else{
if(true){
adpt = new InterfaceToClass(((Class<?>)expectedType).getPackage());
}
}
ClientLifterCaller lifterCaller = new ClientLifterCaller(context.getInputStream(),expectedType, adpt);
context.setInputStream(lifterCaller.callStream());
System.out.println("Fin intercepteur de la réponse");
return context.proceed();
}
}