1 package de.spring.example.rest.handler;
3 import javax.inject.Inject;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
7 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
9 import de.spring.example.context.UsernameThreadContext;
11 public class UsernameHandler extends HandlerInterceptorAdapter {
12 private final UsernameThreadContext usernameThreadContext;
15 public UsernameHandler(UsernameThreadContext userNameThreadContext) {
16 this.usernameThreadContext = userNameThreadContext;
20 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
22 final String userName = request.getHeader(UsernameThreadContext.USERNAME_HEADER);
24 if (userName != null) {
25 usernameThreadContext.setUsername(userName);
27 usernameThreadContext.clearUsername();
30 return super.preHandle(request, response, handler);
34 public void afterCompletion(
35 HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
37 usernameThreadContext.clearUsername();