2

i am trying to use RemoteServiceServlet with Filter to catch the request to track what methods are requested in my webapplicaiton of a user.

i am unable to do this...

can anyone suggest a best way to track the methods called something like a ServletFilter.

NOTE i like to track user and his request he requested for.

3 Answers 3

0

You can override public boolean preProcess(HttpExchange exchange) in your implementation of RemoteServiceServlet as follow.

public boolean preProcess(HttpExchange exchange) {
    String body = null;
    RPCRequest req = null;

    // Read and parse the request
    byte[] bytes = null;
    Object bytebuf = getAttribute("RequestBody");
    if ((bytebuf != null) && (bytebuf instanceof ByteBuffer)) {
        bytes = ((ByteBuffer) bytebuf).array();
    }

    if (bytes == null) {
        setAttribute("ResponseCode", new Integer(500));
        setAttribute("ResponseBody", "Missing Request Body.");
        return true;
    }

    body = new String(bytes, "UTF-8");

    req = RPC.decodeRequest(body, this.getClass());

    String methodName = req.getMethod().toString();
    //Here you have name of a method
    //You can get parameters
    //Object[] parms = req.getParameters();
    return true;
}
2
  • what package does HttpExchange belong to? I cannot override it because no such method
    – Alex
    Oct 6, 2015 at 15:55
  • It's com.sun.net.httpserver.HttpExchange. But there is no preProcess method in RemoteServiceServlet now. Now AbstractRemoteServiceServlet has onAfterRequestDeserialized(RPCRequest rpcRequest)
    – Ilya
    Oct 20, 2015 at 15:39
0

Use at the beginning of every Method java.util.logging.Logger then write parameters and User info. The information will be written into your tomcat log.

0

You can do it using "Spring MVC" or "Google Guice", the steps behind this idea are:

  1. Write a general servlet and map the URL of all your RPC to it.
  2. The general servlet must extend "RemoteServiceServlet" class.
  3. Override processCall method so that you can find out the client want to call which method of which class.

    @Override public String processCall(String payload) throws SerializationException {

    try {
        RPCRequest rpcRequest = RPC.decodeRequest(payload);
    
        RemoteService service = getServiceInstance(rpcRequest.getMethod().getDeclaringClass());
    
        return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
                rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
    
    } catch (IncompatibleRemoteServiceException ex) {
        getServletContext()
                .log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    }
    

    }

You can see this sample that use Google guice.

Have a nice time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.