-
Notifications
You must be signed in to change notification settings - Fork 25
fix: return 200 for CDA GUI client routes #1871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krowvin
wants to merge
2
commits into
develop
Choose a base branch
from
fix/1687-spa-routing-status
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
cwms-data-api/src/main/java/cwms/cda/servlet/SpaErrorStatusFilter.java
This file contains hidden or 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,77 @@ | ||
| package cwms.cda.servlet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Set; | ||
| import javax.servlet.DispatcherType; | ||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.RequestDispatcher; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.annotation.WebFilter; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** | ||
| * Converts the error dispatch used to load known client-side routes into a successful response. | ||
| */ | ||
| @WebFilter(urlPatterns = {"/index.html"}, dispatcherTypes = {DispatcherType.ERROR}) | ||
| public final class SpaErrorStatusFilter implements Filter { | ||
|
|
||
| // Keep these paths synchronized with cda-gui/src/route-paths.js. | ||
| private static final Set<String> SPA_ROUTES = Set.of( | ||
| "/data-query", | ||
| "/filter-expressions", | ||
| "/legacy-format", | ||
| "/location-search", | ||
| "/regexp", | ||
| "/swagger-ui", | ||
| "/timestamps", | ||
| "/user-lists" | ||
| ); | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
| throws IOException, ServletException { | ||
| HttpServletRequest httpRequest = (HttpServletRequest)request; | ||
| HttpServletResponse httpResponse = (HttpServletResponse)response; | ||
|
|
||
| if (isClientRoute(httpRequest)) { | ||
| httpResponse.setStatus(HttpServletResponse.SC_OK); | ||
| } | ||
|
|
||
| chain.doFilter(request, response); | ||
| } | ||
|
|
||
| private boolean isClientRoute(HttpServletRequest request) { | ||
| String method = request.getMethod(); | ||
| if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method)) { | ||
| return false; | ||
| } | ||
|
|
||
| Object errorRequestUri = request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI); | ||
| if (!(errorRequestUri instanceof String)) { | ||
| return false; | ||
| } | ||
|
|
||
| String path = removeContextPath((String)errorRequestUri, request.getContextPath()); | ||
| if (path.length() > 1 && path.endsWith("/")) { | ||
| path = path.substring(0, path.length() - 1); | ||
| } | ||
| return SPA_ROUTES.contains(path); | ||
| } | ||
|
|
||
| private String removeContextPath(String requestUri, String contextPath) { | ||
| if (contextPath == null || contextPath.isEmpty()) { | ||
| return requestUri; | ||
| } | ||
| if (requestUri.equals(contextPath)) { | ||
| return "/"; | ||
| } | ||
| if (requestUri.startsWith(contextPath + "/")) { | ||
| return requestUri.substring(contextPath.length()); | ||
| } | ||
| return requestUri; | ||
| } | ||
| } | ||
This file contains hidden or 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
115 changes: 115 additions & 0 deletions
115
cwms-data-api/src/test/java/cwms/cda/servlet/SpaErrorStatusFilterTest.java
This file contains hidden or 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,115 @@ | ||
| package cwms.cda.servlet; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.DispatcherType; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.RequestDispatcher; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebFilter; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class SpaErrorStatusFilterTest { | ||
|
|
||
| private final SpaErrorStatusFilter filter = new SpaErrorStatusFilter(); | ||
|
|
||
| @Test | ||
| void registersForIndexErrorDispatches() { | ||
| WebFilter annotation = SpaErrorStatusFilter.class.getAnnotation(WebFilter.class); | ||
|
|
||
| assertArrayEquals(new String[] {"/index.html"}, annotation.urlPatterns()); | ||
| assertArrayEquals(new DispatcherType[] {DispatcherType.ERROR}, annotation.dispatcherTypes()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "/data-query", | ||
| "/filter-expressions", | ||
| "/legacy-format", | ||
| "/location-search", | ||
| "/regexp", | ||
| "/swagger-ui", | ||
| "/swagger-ui/", | ||
| "/timestamps", | ||
| "/user-lists" | ||
| }) | ||
| void returnsOkForClientRoutes(String route) throws ServletException, IOException { | ||
| HttpServletRequest request = buildRequest("GET", "/cwms-data" + route); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| FilterChain chain = mock(FilterChain.class); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| verify(response).setStatus(HttpServletResponse.SC_OK); | ||
| verify(chain).doFilter(request, response); | ||
| } | ||
|
|
||
| @Test | ||
| void returnsOkForHeadRequest() throws ServletException, IOException { | ||
| HttpServletRequest request = buildRequest("HEAD", "/cwms-data/swagger-ui"); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| FilterChain chain = mock(FilterChain.class); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| verify(response).setStatus(HttpServletResponse.SC_OK); | ||
| verify(chain).doFilter(request, response); | ||
| } | ||
|
|
||
| @Test | ||
| void returnsOkForAlternateContextPath() throws ServletException, IOException { | ||
| HttpServletRequest request = buildRequest("GET", "/spk-data/swagger-ui", "/spk-data"); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| FilterChain chain = mock(FilterChain.class); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| verify(response).setStatus(HttpServletResponse.SC_OK); | ||
| verify(chain).doFilter(request, response); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesNotFoundStatusForUnknownRoutes() throws ServletException, IOException { | ||
| HttpServletRequest request = buildRequest("GET", "/cwms-data/not-a-client-route"); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| FilterChain chain = mock(FilterChain.class); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| verify(response, never()).setStatus(HttpServletResponse.SC_OK); | ||
| verify(chain).doFilter(request, response); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesNotFoundStatusForNonPageRequests() throws ServletException, IOException { | ||
| HttpServletRequest request = buildRequest("POST", "/cwms-data/swagger-ui"); | ||
| HttpServletResponse response = mock(HttpServletResponse.class); | ||
| FilterChain chain = mock(FilterChain.class); | ||
|
|
||
| filter.doFilter(request, response, chain); | ||
|
|
||
| verify(response, never()).setStatus(HttpServletResponse.SC_OK); | ||
| verify(chain).doFilter(request, response); | ||
| } | ||
|
|
||
| private HttpServletRequest buildRequest(String method, String requestUri) { | ||
| return buildRequest(method, requestUri, "/cwms-data"); | ||
| } | ||
|
|
||
| private HttpServletRequest buildRequest(String method, String requestUri, String contextPath) { | ||
| HttpServletRequest request = mock(HttpServletRequest.class); | ||
| when(request.getMethod()).thenReturn(method); | ||
| when(request.getContextPath()).thenReturn(contextPath); | ||
| when(request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).thenReturn(requestUri); | ||
| return request; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@WebFilter(I think, look at the other filters for the right annotation) and remove the web.xml change.Unless there's a specific reason to configure it in the web.xml instead. That recommendation is likely left over from all the examples that existed before annotations were supported in Java.