3

The server, a stand-alone SE application running Spring 2.5.6 and an embedded jetty. Clients, Swing application, connects to the server using HttpInvoker.

There are a lot of services exposed by the server and now, new requirements have emerged saying I need to log (almost) every invocation made by the client.

What I would like to to do is for the client to send some extra information, (username, workstationId etc. Strings and ints). A typical method on the server would look like this

public void doStuff(int someParam) {
   // Do stuff
   List result = method(someParam)

   // Audit
   // get the client information from somewhere?!!
   String username;
   int workstationId;

   auditDao.doStuffPerformed(username, workstationId, someParam, result);

}

So, how do I get the client information from within a method on the server.

One solution that I've tried is to add client information as request attributes and call method RequestContextHolder.getRequestAttributes(); from within method.

I have added a CommonsHttpInvokerRequestExecutor on the client side and overloaded the following method in order to add the additional information.

@Override
protected PostMethod createPostMethod(HttpInvokerClientConfiguration config) throws IOException {
  PostMethod postMethod = super.createPostMethod(config);
  postMethod.addRequestHeader("someHeader", "someHeader2");
  postMethod.addParameter("someParam", "someParam2");
  postMethod.setRequestHeader("someRequestHeader", "someRequestHeader2");
  return postMethod;
}

This will however not work. The headers or parameters are not accessible on the server.

Any response would be greatly appreciated.

user971483
  • 33
  • 3

1 Answers1

0

I think you're on the right track. You should just use a custom SimpleHttpInvokerServiceExporter subclass on the server-side, and override readRemoteInvocation to extract the headers set by the client from the HttpExchange argument.

These header values could be stored in a static ThreadLocal session variable, which would be accessible anywhere in the server-side code.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your help. I found that I wasn't calling the service with the CommonsHttpInvokerRequestExecutor and that was why the headers didn't appear. When this was fixed, all I had to do was to call RequestContextHolder.getRequestAttributes(); from inside the method on the server to get the headers which I guess is the easiest way since the request should be thread safe and all :) – user971483 Sep 29 '11 at 18:15