GitHeaderInterceptor.java 1.82 KB
package etunicorn;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by geoffrey on 29/01/17.
 */
public class GitHeaderInterceptor extends HandlerInterceptorAdapter {

    private String version = "inconnue";

    public GitHeaderInterceptor() {
        super();
        try {
            String[] commande = {"git", "describe", "--tags", "--dirty"};
            Process p = Runtime.getRuntime().exec(commande);
            BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
            p.waitFor();
            this.version = output.readLine();
        } catch(IOException e) {
        } catch (InterruptedException e) {
        }
    }

    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.addHeader("X-Version", this.version);
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(javax.servlet.http.HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(javax.servlet.http.HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(javax.servlet.http.HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }
}