DispatchCriteriaHelper { public static String extractFromURIPattern(String pattern, String realURI){ Map<String, String> criteriaMap = new TreeMap<String, String>(); String partsPattern = null; String valuesPattern = null; if (pattern.indexOf("/{") != -1) { partsPattern = pattern.replaceAll("(\\{[^\\}]+\\})", "\\\\{(.+)\\\\}"); valuesPattern = pattern.replaceAll("(\\{[^\\}]+\\})", "(.+)"); } else { partsPattern = pattern.replaceAll("(:[^:^/]+)", "\\:(.+)"); valuesPattern = pattern.replaceAll("(:[^:^/]+)", "(.+)"); } Pattern partsP = Pattern.compile(partsPattern); Matcher partsM = partsP.matcher(pattern); Pattern valuesP = Pattern.compile(valuesPattern); Matcher valuesM = valuesP.matcher(realURI); if (valuesM.matches() && partsM.matches() && valuesM.groupCount() == partsM.groupCount()){ for (int i=1; i<partsM.groupCount()+1; i++){ criteriaMap.put(partsM.group(i), valuesM.group(i)); } } StringBuilder result = new StringBuilder(); for (String criteria : criteriaMap.keySet()){ result.append("/").append(criteria).append("=").append(criteriaMap.get(criteria)); } return result.toString(); }  static String extractParamsFromURI(String uri); static String extractCommonPrefix(List<String> uris); static String extractCommonSuffix(List<String> uris); static String extractPartsFromURIs(List<String> uris); static String extractPartsFromURIPattern(String pattern); static String extractFromURIPattern(String pattern, String realURI); static String buildFromPartsMap(Map<String, String> partsMap); static String buildFromParamsMap(String paramsRule, Map<String, String> paramsMap); static String extractFromURIParams(String paramsRule, String uri);  }
@Test public void testExtractFromURIPattern(){ String requestPath = "/deployment/byComponent/myComp/1.2"; String operationName = "/deployment/byComponent/{component}/{version}"; String dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, requestPath); assertEquals("/component=myComp/version=1.2", dispatchCriteria); requestPath = "/deployment/byComponent/myComp/1.2"; operationName = "/deployment/byComponent/:component/:version"; dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, requestPath); assertEquals("/component=myComp/version=1.2", dispatchCriteria); }
@Test public void testExtractFromURIPattern2() { String resourcePath = "/pet/2"; String operationName = "/pet/:petId"; String dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, resourcePath); assertEquals("/petId=2", dispatchCriteria); resourcePath = "/order/123456"; operationName = "/order/:id"; dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, resourcePath); assertEquals("/id=123456", dispatchCriteria); }
@Test public void testExtractFromURIPatternUnsorted(){ String requestPath = "/deployment/byComponent/1.2/myComp"; String operationName = "/deployment/byComponent/{version}/{component}"; String dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, requestPath); assertEquals("/component=myComp/version=1.2", dispatchCriteria); }
@Test public void testExtractFromURIPatternWithExtension(){ String requestPath = "/deployment/byComponent/myComp/1.2.json"; String operationName = "/deployment/byComponent/{component}/{version}.json"; String dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(operationName, requestPath); assertEquals("/component=myComp/version=1.2", dispatchCriteria); }