Example #1
0
  public void testVariableNames() throws Exception {
    Template tpl = new Template("http://{userId}.noelios.com/invoices/{invoiceId}");
    tpl.setLogger(Engine.getAnonymousLogger());
    List<String> names = tpl.getVariableNames();

    assertEquals(2, names.size());
    assertEquals("userId", names.get(0));
    assertEquals("invoiceId", names.get(1));
  }
  private static Resource getResource(
      CollectInfo collectInfo, Object restlet, String basePath, ChallengeScheme scheme) {
    Resource resource = new Resource();
    resource.setResourcePath(basePath);

    if (restlet instanceof Directory) {
      Directory directory = (Directory) restlet;
      resource.setName(directory.getName());
      resource.setDescription(directory.getDescription());
    }
    if (restlet instanceof ServerResource) {
      ServerResource serverResource = (ServerResource) restlet;
      resource.setName(serverResource.getName());
      resource.setDescription(serverResource.getDescription());
    }
    if (restlet instanceof DocumentedResource) {
      DocumentedResource documentedServerResource = (DocumentedResource) restlet;
      resource.setSections(documentedServerResource.getSections());
    } else if (collectInfo.isUseSectionNamingPackageStrategy()) {
      String sectionName = restlet.getClass().getPackage().getName();
      resource.getSections().add(sectionName);
    }

    if (StringUtils.isNullOrEmpty(resource.getName())) {
      String name = restlet.getClass().getSimpleName();
      if (name.endsWith(SUFFIX_SERVER_RESOURCE)
          && name.length() > SUFFIX_SERVER_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_SERVER_RESOURCE.length());
      }
      if (name.endsWith(SUFFIX_RESOURCE) && name.length() > SUFFIX_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_RESOURCE.length());
      }
      resource.setName(name);
    }

    Template template = new Template(basePath);
    for (String variable : template.getVariableNames()) {
      PathVariable pathVariable = new PathVariable();
      pathVariable.setName(variable);
      resource.getPathVariables().add(pathVariable);
    }

    if (scheme != null) {
      resource.setAuthenticationProtocol(scheme.getName());
    }

    return resource;
  }