RequestUtils { public static String findFirstMatchingRestPath(String[] candidateViewNames, String[] controllerPaths, HttpServletRequest request) { String pathWithinHandlerMapping = removeLeadingAndTrailingSlashes( (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)); AntPathMatcher antPathMatcher = new AntPathMatcher(); for (String candidatePath : candidateViewNames) { if ((controllerPaths == null || candidatePath.startsWith("/")) && antPathMatcher.match(removeLeadingAndTrailingSlashes(candidatePath), pathWithinHandlerMapping)) { return candidatePath; } else if (controllerPaths != null) { for (String controllerPath : controllerPaths) { String testPath = (controllerPath + "/" + candidatePath).replace(" if (antPathMatcher.match(removeLeadingAndTrailingSlashes(testPath), pathWithinHandlerMapping)) { return candidatePath; } } } } return null; }  @SuppressWarnings("unchecked") static String replaceRestPathVariables(String viewName, Map<String, Object> model, HttpServletRequest request); static String findFirstMatchingRestPath(String[] candidateViewNames, 
			String[] controllerPaths, HttpServletRequest request); static String[] getControllerRequestPaths(HandlerMethod handler); static HttpServletRequest getCurrentRequest(); static String getPathWithinHandlerMapping(); static String removeLeadingAndTrailingSlashes(String path); static final String PATH_VAR_PATTERN; }
@Test public void testFindFirstMatchingRestPathWithAntPathWithSingleControllerPath() { String[] candidateViewNames = new String[]{"/financial/edit","new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithAntPathWithMultipleControllerPaths() { String[] candidateViewNames = new String[]{"/financial/edit","new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithAntPathWithAntControllerPaths() { String[] candidateViewNames = new String[]{"/financial/edit","new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithNestedAntPath() { String[] candidateViewNames = new String[]{"/account/financial/edit","new","/account/financial/new"}; String[] controllerPaths = null; HttpServletRequest request = mockRequestWithPattern("/account/financial/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithNestedAntPathWithSingleControllerPath() { String[] candidateViewNames = new String[]{"/financial/holdings/edit","new","/financial/holdings/new"}; String[] controllerPaths = new String[]{"/account"}; HttpServletRequest request = mockRequestWithPattern("/account/financial/holdings/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithNestedAntPathWithMultipleControllerPaths() { String[] candidateViewNames = new String[]{"/financial/holdings/edit","new","/financial/holdings/new"}; String[] controllerPaths = new String[]{"/hamburger", "/account"}; HttpServletRequest request = mockRequestWithPattern("/account/financial/holdings/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithAntPathWithNestedAntControllerPaths() { String[] candidateViewNames = new String[]{"/financial/holdings/edit","/*","/financial/holdings/new"}; String[] controllerPaths = new String[]{"/hamburger", "/**"}; HttpServletRequest request = mockRequestWithPattern("/account/financial/holdings/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/*", matchingRestPath); }
@Test public void testPathVariableRequestPath() { String[] candidateViewNames = new String[]{"any/path/will/do"}; String[] controllerPaths = new String[]{"{recordId}"}; HttpServletRequest request = mockRequestWithPattern("1/any/path/will/do"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("any/path/will/do", matchingRestPath); }
@Test public void testPathVariableControllerPath() { String[] candidateViewNames = new String[]{"/financial/edit","/{id}/new","/financial/new"}; String[] controllerPaths = new String[]{"/hamburger", "/account"}; HttpServletRequest request = mockRequestWithPattern("/account/financial/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/{id}/new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathBasic() { String[] candidateViewNames = new String[]{"/success/view"}; String[] controllerPaths = null; HttpServletRequest request = mockRequestWithPattern("/success/view"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/success/view", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathBasicWithSingleControllerPath() { String[] candidateViewNames = new String[]{"/success/view"}; String[] controllerPaths = new String[]{"/account"}; HttpServletRequest request = mockRequestWithPattern("account/success/view"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/success/view", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathBasicWithMultipleControllerPaths() { String[] candidateViewNames = new String[]{"/success/view"}; String[] controllerPaths = new String[]{"/hamburger", "/account"}; HttpServletRequest request = mockRequestWithPattern("account/success/view"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/success/view", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathMultiCandidate() { String[] candidateViewNames = new String[]{"/account/edit","/account/new"}; String[] controllerPaths = null; HttpServletRequest request = mockRequestWithPattern("/account/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/account/new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathMultiCandidateWithSingleControllerPath() { String[] candidateViewNames = new String[]{"/edit","/new"}; String[] controllerPaths = new String[]{"/account"}; HttpServletRequest request = mockRequestWithPattern("/account/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathMultiCandidateWithMultipleControllerPaths() { String[] candidateViewNames = new String[]{"/edit","/new"}; String[] controllerPaths = new String[]{"/hamburger", "/account"}; HttpServletRequest request = mockRequestWithPattern("/account/new"); String matchingRestPath = RequestUtils.findFirstMatchingRestPath(candidateViewNames, controllerPaths, request); assertEquals("/new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithAntPath() { String[] candidateViewNames = new String[]{"/account/edit","new", matchingRestPath); }
@Test public void testFindFirstMatchingRestPathWithPartialAntPath() { String[] candidateViewNames = new String[]{"/account/edit","n*w", matchingRestPath); }