Example #1
0
  private Param resolveParameter(Param param) {
    String href = param.getHref();
    if (!StringUtils.hasContent(href)) {
      return param;
    }

    try {
      Application app = application;

      if (!href.startsWith("#")) {
        ApplicationDocument applicationDocument = loadReferencedWadl(href);
        app = applicationDocument.getApplication();
      }

      if (app != null) {
        int ix = href.lastIndexOf('#');
        if (ix >= 0) {
          href = href.substring(ix + 1);
        }

        for (Param p : application.getParamList()) {
          if (p.getId().equals(href)) {
            return p;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }
Example #2
0
  private void initResourceFromWadlResource(RestResource newResource, Resource resource) {
    for (Param param : resource.getParamList()) {
      param = resolveParameter(param);
      if (param != null) {
        String nm = param.getName();
        RestParamProperty prop =
            newResource.hasProperty(nm) ? newResource.getProperty(nm) : newResource.addProperty(nm);

        initParam(param, prop);
      }
    }

    for (Method method : resource.getMethodList()) {
      method = resolveMethod(method);
      initMethod(newResource, method);
    }

    List<?> types = resource.getType();
    if (types != null && types.size() > 0) {
      for (Object obj : types) {
        ResourceTypeDocument.ResourceType type = resolveResource(obj.toString());
        if (type != null) {
          for (Method method : type.getMethodList()) {
            method = resolveMethod(method);
            RestMethod restMethod = initMethod(newResource, method);

            for (Param param : type.getParamList()) {
              param = resolveParameter(param);
              if (param != null) {
                String nm = param.getName();
                RestParamProperty prop =
                    restMethod.hasProperty(nm)
                        ? restMethod.getProperty(nm)
                        : restMethod.addProperty(nm);

                initParam(param, prop);
              }
            }
          }
        }
      }
    }
  }
Example #3
0
  private void initParam(Param param, RestParamProperty prop) {
    prop.setDefaultValue(param.getDefault());
    prop.setValue(param.getDefault());
    ParamStyle.Enum paramStyle = param.getStyle();
    if (paramStyle == null) {
      paramStyle = ParamStyle.QUERY;
    }

    prop.setStyle(ParameterStyle.valueOf(paramStyle.toString().toUpperCase()));
    prop.setRequired(param.getRequired());
    QName paramType = param.getType();
    prop.setType(paramType);

    String[] options = new String[param.sizeOfOptionArray()];
    for (int c = 0; c < options.length; c++) {
      options[c] = param.getOptionArray(c).getValue();
    }

    if (options.length > 0) {
      prop.setOptions(options);
    }
  }
Example #4
0
  private RestMethod initMethod(RestResource newResource, Method method) {
    // build name
    String name = getFirstTitle(method.getDocList(), method.getName());
    String id = method.getId();
    if (StringUtils.hasContent(id) && !id.trim().equals(name.trim())) {
      name += " - " + method.getId();
    }

    // ensure unique name
    if (newResource.getRestMethodByName(name) != null) {
      int cnt = 0;
      String orgName = name;

      while (newResource.getRestMethodByName(name) != null) {
        cnt++;
        name = orgName + "-" + cnt;
      }
    }

    // add to resource
    RestMethod restMethod = newResource.addNewMethod(name);
    restMethod.setMethod(RestRequestInterface.HttpMethod.valueOf(method.getName()));

    if (method.getRequest() != null) {
      for (Param param : method.getRequest().getParamList()) {
        param = resolveParameter(param);
        if (param != null) {
          RestParamProperty p = restMethod.addProperty(param.getName());
          initParam(param, p);
        }
      }

      for (Representation representation : method.getRequest().getRepresentationList()) {
        representation = resolveRepresentation(representation);
        addRepresentationFromConfig(
            restMethod, representation, RestRepresentation.Type.REQUEST, null);
      }
    }

    for (Response response : method.getResponseList()) {
      for (Representation representation : response.getRepresentationList()) {
        addRepresentation(response, restMethod, representation);
      }
      if (!isWADL11) {
        NodeList children = response.getDomNode().getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
          Node n = children.item(i);
          if ("fault".equals(n.getNodeName())) {
            String content = XmlUtils.serialize(n, false);
            try {
              Map<Object, Object> map = new HashMap<Object, Object>();
              XmlCursor cursor = response.newCursor();
              cursor.getAllNamespaces(map);
              cursor.dispose();
              XmlOptions options = new XmlOptions();
              options.setLoadAdditionalNamespaces(map);
              // XmlObject obj = XmlObject.Factory.parse(
              // content.replaceFirst( "<(([a-z]+:)?)fault ",
              // "<$1representation " ), options );
              XmlObject obj =
                  XmlUtils.createXmlObject(
                      content.replaceFirst("<(([a-z]+:)?)fault ", "<$1representation "), options);
              RepresentationDocument representation =
                  (RepresentationDocument) obj.changeType(RepresentationDocument.type);
              addRepresentation(response, restMethod, representation.getRepresentation());
            } catch (XmlException e) {
            }
          }
        }
      }
    }

    restMethod.addNewRequest("Request 1");
    return restMethod;
  }