public static String buildURIFromPattern(String pattern, List<Parameter> parameters) {
      if (parameters != null) {
         // Browse parameters and choose between template or query one.
         for (Parameter parameter : parameters) {
            String wadlTemplate = "{" + parameter.getName() + "}";
            String swaggerTemplate = "/:" + parameter.getName();
            if (pattern.contains(wadlTemplate)) {
               // It's a template parameter.
               pattern = pattern.replace(wadlTemplate, encodePath(parameter.getValue()));
            } else if (pattern.contains(swaggerTemplate)) {
               // It's a template parameter.
               pattern = pattern.replace(":" + parameter.getName(), encodePath(parameter.getValue()));
            } else {
               // It's a query parameter, ensure we have started delimiting them.
               if (!pattern.contains("?")) {
                  pattern += "?";
               }
               if (pattern.contains("=")) {
                  pattern += "&";
               }
               pattern += parameter.getName() + "=" + encodeValue(parameter.getValue());
            }
         }
      }
      return pattern;
   }
@Test
   void testBuildURIFromPattern() {
      // Prepare a bunch of parameters.
      Parameter yearParam = new Parameter();
      yearParam.setName("year");
      yearParam.setValue("2017");

      Parameter monthParam = new Parameter();
      monthParam.setName("month");
      monthParam.setValue("08");

      Parameter statusParam = new Parameter();
      statusParam.setName("status");
      statusParam.setValue("published");

      Parameter pageParam = new Parameter();
      pageParam.setName("page");
      pageParam.setValue("0");

      List<Parameter> parameters = new ArrayList<>();
      parameters.add(yearParam);
      parameters.add(monthParam);
      parameters.add(statusParam);
      parameters.add(pageParam);

      // Test with old wadl like template format.
      String pattern = "http://localhost:8080/blog/{year}/{month}";
      String uri = URIBuilder.buildURIFromPattern(pattern, parameters);
      assertTrue("http://localhost:8080/blog/2017/08?page=0&status=published".equals(uri)
            || "http://localhost:8080/blog/2017/08?status=published&page=0".equals(uri));

      // Test with new swagger like template format.
      pattern = "http://localhost:8080/blog/:year/:month";
      uri = URIBuilder.buildURIFromPattern(pattern, parameters);
      assertTrue("http://localhost:8080/blog/2017/08?page=0&status=published".equals(uri)
            || "http://localhost:8080/blog/2017/08?status=published&page=0".equals(uri));
   }