From 9182ba39356804806230f637ccceaa31158ae0f5 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sun, 29 Jan 2017 01:45:44 +0100 Subject: [PATCH] Ça affiche le numéro de version dans les réponses ! --- src/main/java/etunicorn/GitHeaderInterceptor.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/etunicorn/WebMvcConfig.java | 18 ++++++++++++++++++ 2 files changed, 67 insertions(+), 0 deletions(-) create mode 100644 src/main/java/etunicorn/GitHeaderInterceptor.java create mode 100644 src/main/java/etunicorn/WebMvcConfig.java diff --git a/src/main/java/etunicorn/GitHeaderInterceptor.java b/src/main/java/etunicorn/GitHeaderInterceptor.java new file mode 100644 index 0000000..aefbe19 --- /dev/null +++ b/src/main/java/etunicorn/GitHeaderInterceptor.java @@ -0,0 +1,49 @@ +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); + } +} diff --git a/src/main/java/etunicorn/WebMvcConfig.java b/src/main/java/etunicorn/WebMvcConfig.java new file mode 100644 index 0000000..7c668ea --- /dev/null +++ b/src/main/java/etunicorn/WebMvcConfig.java @@ -0,0 +1,18 @@ +package etunicorn; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.*; + +/** + * Created by geoffrey on 29/01/17. + */ + +@Configuration +public class WebMvcConfig extends WebMvcConfigurerAdapter { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new GitHeaderInterceptor()); + } +} + -- libgit2 0.21.2