package net.etalia.crepuscolo.auth;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class RefreshAuthTokenFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		chain.doFilter(request, response);
		AuthData auth = AuthFilter.getAuthData();
		if (auth.getCurrentToken() != null) {
			if (response instanceof HttpServletResponse) {
				String newToken = AuthData.produce(auth.getUserId(), auth.getUserPassword(), auth.getSystemId());
				((HttpServletResponse) response).setHeader("X-Authorization", newToken);
			}
		}
	}

	@Override
	public void destroy() {
	}

}

@@ -376,16 +376,50 @@
 lter %7B%0A%0A
+%09private long maxTokenTime = -1;%0A%0A
 %09@Overri
@@ -413,32 +413,32 @@
 -1;%0A%0A%09@Override%0A
-
 %09public void ini
@@ -492,16 +492,164 @@
 ption %7B%0A
+%09%09if (filterConfig.getInitParameter(%22maxTokenTime%22) != null) %7B%0A%09%09%09maxTokenTime = Long.parseLong(filterConfig.getInitParameter(%22maxTokenTime%22));%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -956,16 +956,114 @@
 onse) %7B%0A
+%09%09%09%09if (maxTokenTime != -1 && System.currentTimeMillis() %3C auth.getTimeStamp() + maxTokenTime) %7B%0A%09
 %09%09%09%09Stri
@@ -1160,16 +1160,17 @@
 ));%0A%09%09%09%09
+%09
 ((HttpSe
@@ -1234,16 +1234,22 @@
 Token);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Refresh occur only if token is not expired