RequestUtils { @SuppressWarnings("unchecked") public static String replaceRestPathVariables(String viewName, Map<String, Object> model, HttpServletRequest request) { String newViewName = viewName; Matcher matcher = Pattern.compile(PATH_VAR_PATTERN).matcher(newViewName); while (matcher.find()) { String match = matcher.group(); String varName = match.substring(1, match.length() - 1); String baseVarName = null; String subPath = null; if (varName.contains(".")) { baseVarName = varName.substring(0, varName.indexOf(".")); subPath = varName.substring(varName.indexOf(".") + 1); } else { baseVarName = varName; } Map<String, String> uriTemplateVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); if (uriTemplateVariables != null && uriTemplateVariables.containsKey(varName)) { newViewName = newViewName.replace(match, String.valueOf(uriTemplateVariables.get(varName))); } else { Object resolvedObject = model.get(baseVarName); if (resolvedObject == null) { throw new IllegalArgumentException(varName + " is not present in model."); } if (subPath != null) { BeanWrapperImpl beanWrapper = new BeanWrapperImpl(resolvedObject); resolvedObject = beanWrapper.getPropertyValue(subPath); } if (resolvedObject == null) { throw new IllegalArgumentException(varName + " is not present in model."); } newViewName = newViewName.replace(match, String.valueOf(resolvedObject)); } matcher.reset(newViewName); } return newViewName; }  @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 testReplacePathVariablesNoReplacements() { Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> uriTemplateVariables = new HashMap<String, Object>(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/foo/list", model, request); assertEquals("/foo/list", replaced); }
@Test public void testReplacePathVariablesFromUriTemplateVariables() { Map<String, Object> model = new HashMap<String, Object>(); model.put("id", 4); Map<String, Object> uriTemplateVariables = new HashMap<String, Object>(); uriTemplateVariables.put("id", 5); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/foo/{id}", model, request); assertEquals("/foo/5", replaced); }
@Test public void testReplacePathVariablesFromModelWhenNotInUriVariables() { Map<String, Object> model = new HashMap<String, Object>(); model.put("id", 4); Map<String, Object> uriTemplateVariables = new HashMap<String, Object>(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/foo/{id}", model, request); assertEquals("/foo/4", replaced); }
@Test public void testReplacePathVariablesFromModelNullUriVariables() { Map<String, Object> model = new HashMap<String, Object>(); model.put("id", 4); Map<String, Object> uriTemplateVariables = null; HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/foo/{id}", model, request); assertEquals("/foo/4", replaced); }
@Test public void testReplacePathVariablesFromNestedModelPath() { Map<String, Object> model = new HashMap<String, Object>(); Person person = new Customer(); person.setId(4L); model.put("person", person); Map<String, Object> uriTemplateVariables = new HashMap<String, Object>(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/person/{person.id}", model, request); assertEquals("/person/4", replaced); }
@Test(expected=IllegalArgumentException.class) public void testReplacePathVariablesMissingVariable() { Map<String, Object> model = new HashMap<String, Object>(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); RequestUtils.replaceRestPathVariables("/foo/{id}", model, request); }
@Test(expected=IllegalArgumentException.class) public void testReplacePathVariablesMissingVariableOnNestedPath() { Map<String, Object> model = new HashMap<String, Object>(); Person person = new Customer(); model.put("person", person); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); RequestUtils.replaceRestPathVariables("/person/{person.id}", model, request); }
@Test public void testReplacePathVariablesMultipleTimes() { Map<String, Object> model = new HashMap<String, Object>(); Person person = new Customer(); person.setId(4L); model.put("person", person); Map<String, Object> uriTemplateVariables = new HashMap<String, Object>(); uriTemplateVariables.put("id", 5); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(uriTemplateVariables); String replaced = RequestUtils.replaceRestPathVariables("/record/{id}/person/{person.id}", model, request); assertEquals("/record/5/person/4", replaced); }