I am wondering if I can use a filter (in my case CorsFilter) to measure the time AND to put the time on the message itself. I know that the following works in order:
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { long startTime = System.nanoTime(); chain.doFilter(request, response); long endTime = System.nanoTime(); System.out.println("Time: " + (endTime - startTime) ); }
Which, of course, displays the total time in seconds. I wanted to put time in the header of the returned response so that the recipient could look at the header and see how much time the processing took. The following code does not work:
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { long startTime = System.nanoTime(); if(response instanceof HttpServletResponse) { HttpServletResponse httpResp = (HttpServletResponse)response; httpResp.addHeader("Start-time", Long.toString(startTime)); } chain.doFilter(request, response); long endTime = System.nanoTime(); if(response instanceof HttpServletResponse) { HttpServletResponse httpResp = (HttpServletResponse)response; httpResp.addHeader("End-time", Long.toString(endTime)); } }
The header includes only Start-time, not End-time. I assume this is because the message has already been sent, so changing the object will not have any effect.
Does anyone have an elegant / smart solution for setting timings in a header using a filter?
Thanks Phil
Update
I learned how to use HttpServletResponseWrapper to solve this solution. This still does not allow to display either XXX-EndTime or YYY-EndTime in the response header.
@Override public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { long startTime = System.nanoTime(); if(response instanceof HttpServletResponse) { HttpServletResponse httpResp = (HttpServletResponse)response; httpResp.addHeader("Access-Control-Allow-Origin", "*"); httpResp.addHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS"); httpResp.addHeader("Allow", "GET, HEAD, OPTIONS"); httpResp.addHeader("Access-Control-Allow-Headers", "*"); httpResp.addHeader("A-Runtime", Long.toString(startTime)); } OutputStream out = response.getOutputStream(); GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response); chain.doFilter(request,wrapper); out.write(wrapper.getData()); if(response instanceof HttpServletResponse) { HttpServletResponse httpResp = (HttpServletResponse)response; httpResp.addHeader("XXX-EndTime", Long.toString(System.nanoTime() - startTime)); wrapper.addHeader("YYY-EndTime", Long.toString(System.nanoTime() - startTime)); } out.close(); }