-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set custom http header with elapsed time on all api endpoints (#10994)
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/cbioportal/web/ExecuterTimeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.cbioportal.web; | ||
|
||
import org.springframework.ui.ModelMap; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.context.request.WebRequestInterceptor; | ||
|
||
public class ExecuterTimeInterceptor implements WebRequestInterceptor { | ||
|
||
@Override | ||
public void postHandle(WebRequest webRequest, ModelMap modelMap) { | ||
//unimplemented | ||
} | ||
|
||
@Override | ||
public void afterCompletion(WebRequest webRequest, Exception e) { | ||
//unimplemented | ||
} | ||
|
||
@Override | ||
public void preHandle(WebRequest webRequest) { | ||
|
||
long startTime = System.currentTimeMillis(); | ||
webRequest.setAttribute("startTime", startTime, 0); | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/cbioportal/web/GeneralControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.cbioportal.web; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.http.server.ServletServerHttpRequest; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
@ControllerAdvice | ||
public class GeneralControllerAdvice implements ResponseBodyAdvice<Object> { | ||
|
||
@Override | ||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { | ||
ServletServerHttpRequest servletServerRequest = (ServletServerHttpRequest) request; | ||
long startTime = (long) servletServerRequest.getServletRequest().getAttribute("startTime"); | ||
long timeElapsed = System.currentTimeMillis() - startTime; | ||
response.getHeaders().add("Elapsed-Time", String.valueOf(timeElapsed)); | ||
return body; | ||
} | ||
} |