Commit d7a937a2 authored by Alok Joshi's avatar Alok Joshi
Browse files

Merge branch 'correlation_id' into 'master'

add correlation id to response header

See merge request !144
parents b77fb3c8 a475c5fb
Pipeline #37845 failed with stages
in 37 minutes and 4 seconds
package org.opengroup.osdu.indexer.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.java.Log;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Log
@Component
public class IndexerFilter implements Filter {
private final DpsHeaders dpsHeaders;
@Autowired
public IndexerFilter(DpsHeaders dpsHeaders) {
this.dpsHeaders = dpsHeaders;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
dpsHeaders.addCorrelationIdIfMissing();
httpResponse.addHeader(DpsHeaders.CORRELATION_ID, dpsHeaders.getCorrelationId());
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
package org.opengroup.osdu.indexer.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RunWith(MockitoJUnitRunner.class)
public class IndexerFilterTest {
@InjectMocks
private IndexerFilter indexerFilter;
@Mock
private DpsHeaders dpsHeaders;
@Test
public void shouldSetCorrectResponseHeaders() throws IOException, ServletException {
HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class);
FilterChain filterChain = Mockito.mock(FilterChain.class);
Mockito.when(dpsHeaders.getCorrelationId()).thenReturn("correlation-id-value");
indexerFilter.doFilter(httpServletRequest, httpServletResponse, filterChain);
Mockito.verify(httpServletResponse).addHeader("correlation-id", "correlation-id-value");
Mockito.verify(filterChain).doFilter(httpServletRequest, httpServletResponse);
}
}
......@@ -75,7 +75,7 @@ public class IndexFilter implements Filter {
}
filterChain.doFilter(servletRequest, servletResponse);
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
Map<String, List<Object>> standardHeaders = ResponseHeaders.STANDARD_RESPONSE_HEADERS;
for (Map.Entry<String, List<Object>> header : standardHeaders.entrySet()) {
......
......@@ -90,7 +90,7 @@ public class IndexFilter implements Filter {
}
filterChain.doFilter(servletRequest, servletResponse);
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
Map<String, List<Object>> standardHeaders = ResponseHeaders.STANDARD_RESPONSE_HEADERS;
for (Map.Entry<String, List<Object>> header : standardHeaders.entrySet()) {
......@@ -99,6 +99,7 @@ public class IndexFilter implements Filter {
if (httpResponse.getHeader(DpsHeaders.CORRELATION_ID) == null) {
httpResponse.addHeader(DpsHeaders.CORRELATION_ID, dpsHeaders.getCorrelationId());
}
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment