*What is an MSF4J Interceptor?*
In MSF4J interceptors are capable of processing an Http request before or
after the Http request arrives a resource methods. Interceptors implement
the *org.wso2.msf4j.Interceptor* interface.

public interface Interceptor {

    boolean preCall(HttpRequest request, HttpResponder responder,
ServiceMethodInfo serviceMethodInfo);

    void postCall(HttpRequest request, HttpResponseStatus status,
ServiceMethodInfo serviceMethodInfo);
}



*Problem*
With the current implementation of MSF4J, there is no way for MSF4J
interceptors to communicate with resource methods. That means, if an
interceptor is preprocessing a request and creates an object model out of
the request that needs to be accessible to the corresponding resource
method, how can the interceptor pass the created object model to the
resource method?

One example of this situation is implementing an interceptor for handling
sessions. In this case, the session interceptor will process the http
request and create a session object. This session object should then be
passed to the resource methods. With current implementation there is no
proper way to do this.

*Suggested Solution*
Currently the *ServiceMethodInfo* parameter in
*org.wso2.msf4j.Interceptor* contains
only an attribute map and *a java.lang.reflect.Method* type reference to
the resource method.

We can improve this class to support type based parameter injection to the
resource method. That means,

   - We rename *ServiceMethodInfo* class to a name similar to
   *ServiceMethodContext*
   - Then we introduce an *inject(Object parameter)* method to this context
   class. Interceptors can use this method to inject the objects that they
   create to the resource method context.

IE: If we consider  a session interceptor,

 servicemethodCtx.inject(session) // session object is is injected to the
resource method context

Further we can introduce methods to *ServiceMethodInfo* to query the
resource method's parameter support. This helps to limit processing
information in interceptors that resource methods does not accept.


   - Resource methods can use the *@Context* annotation to consume the
   available objects in the resource method's context as shown bellow.

@GET

public Response helloService(@Context Session session) {

// If the session interceptor is available the session will be injected to
the resource method by matching the type of the @Context
annotated parameters

....

}

Further, with this solution, we can even consider to break current
centralised annotation processing logic into individual interceptors that
process a certain kind of annotation. This will improve modularity,
scalability as well as the consistency of the design.

WDYT?


Best Regards,
Samiyuru


-- 
Samiyuru Senarathne
*Software Engineer*
Mobile : +94 (0) 71 134 6087
[email protected]
_______________________________________________
Architecture mailing list
[email protected]
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture

Reply via email to