Skip to content

Commit

Permalink
Set custom http header with elapsed time on all api endpoints (#10994)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisman authored Sep 16, 2024
1 parent a4ff971 commit a475fc9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/cbioportal/WebAppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.cbioportal.web.ExecuterTimeInterceptor;
import org.cbioportal.web.util.InvolvedCancerStudyExtractorInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -12,6 +13,7 @@
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;

// TODO Consider creating separate DispatcherServlets as in the original web.xml
// See: https://stackoverflow.com/a/30686733/11651683
Expand Down Expand Up @@ -74,6 +76,9 @@ public HandlerInterceptor involvedCancerStudyExtractorInterceptor() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(involvedCancerStudyExtractorInterceptor());
registry.addInterceptor(new WebRequestHandlerInterceptorAdapter(
new ExecuterTimeInterceptor()
)).addPathPatterns("/**");
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/cbioportal/web/ExecuterTimeInterceptor.java
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 src/main/java/org/cbioportal/web/GeneralControllerAdvice.java
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;
}
}

0 comments on commit a475fc9

Please sign in to comment.