public static String extractFromURIParams(String paramsRule, String uri) {
      Multimap<String, String> criteriaMap = TreeMultimap.create();

      if (uri.contains("?") && uri.contains("=")) {
         String parameters = uri.substring(uri.indexOf("?") + 1);

         for (String parameter : parameters.split("&")) {
            String[] pair = parameter.split("=");
            if (pair.length > 1) {
               String key = URLDecoder.decode(pair[0], StandardCharsets.UTF_8);
               String value = URLDecoder.decode(pair[1], StandardCharsets.UTF_8);
               criteriaMap.put(key, value);
            }
         }

         // Just appends sorted entries, separating them with ?.
         StringBuilder result = new StringBuilder();
         for (Map.Entry<String, String> criteria : criteriaMap.entries()) {
            if (paramsRule.contains(criteria.getKey())) {
               result.append("?").append(criteria.getKey()).append("=").append(criteria.getValue());
            }
         }
         return result.toString();
      }
      return "";
   }
@Test
   void testExtractFromURIParamsWithEmpty() {
      // Check with parameters that allows empty.
      String requestPath = "/search?param1=test&param2=&param3=";

      // Only 1 parameter should be taken into account according to rules.
      String dispatchCriteria = DispatchCriteriaHelper.extractFromURIParams("param1 && param2", requestPath);
      assertEquals("?param1=test", dispatchCriteria);
   }