Exemplo n.º 1
0
  private void visit(IMethodModel m, String path) {
    boolean hasPath = m.hasAnnotation(PATH);
    if (hasPath) {
      String localPath = m.getAnnotationValue(PATH);
      if (path.endsWith("/")) { // $NON-NLS-1$
        if (localPath.startsWith("/")) { // $NON-NLS-1$
          localPath = localPath.substring(1);
        }
      }

      path += localPath;
    }

    boolean isWs = hasPath;
    for (ActionType q : ActionType.values()) {
      boolean hasAnnotation = m.hasAnnotation(q.name());
      isWs |= hasAnnotation;
    }
    if (isWs) {
      Resource res = new Resource();
      IDocInfo documentation = m.getBasicDocInfo();
      String text = documentation.getDocumentation();
      if (!"".equals(text)) { // $NON-NLS-1$
        res.setDescription(text);
      }
      String returnName = null;
      String parameterName = null;

      ITypeModel returnedType = m.getReturnedType();

      if (returnedType != null) {
        if (returnedType.hasAnnotation(XML_ROOT_ELEMENT)) {
          generateXMLSchema(returnedType);
          returnName = returnedType.getName().toLowerCase();
        }
        if (hasPath) {
          if (consumedTypes.add(returnedType)) {
            ResourceVisitor resourceVisitor = createResourceVisitor();
            resourceVisitor.consumedTypes.addAll(this.consumedTypes);
            resourceVisitor.basePath = path;
            resourceVisitor.spec = this.spec;
            resourceVisitor.visit(returnedType);
          }
        }
      }
      ITypeModel bodyType = m.getBodyType();
      if (bodyType != null) {
        if (bodyType.hasAnnotation(XML_ROOT_ELEMENT)) {
          generateXMLSchema(bodyType);
          parameterName = bodyType.getName().toLowerCase();
        }
      }
      if (path.endsWith("/")) { // $NON-NLS-1$
        res.setRelativeUri(path.substring(0, path.length() - 1));
      } else {
        res.setRelativeUri(path);
      }
      for (ActionType q : ActionType.values()) {
        boolean hasAnnotation = m.hasAnnotation(q.name());
        if (hasAnnotation) {
          addMethod(q, res, m, documentation, returnName, parameterName);
        }
      }
      spec.addResource(res);
    }
  }
  private void addMethod(
      ActionType actionType,
      Resource res,
      IMethodModel m,
      IDocInfo documentation,
      String returnName,
      String parameterName) {

    Action action = new Action();
    String description = documentation.getDocumentation();
    if (!"".equals(description)) { // $NON-NLS-1$
      action.setDescription(description);
    }

    ActionType adjustedActionType = adjustActionType(m, actionType);
    action.setType(adjustedActionType);
    res.getActions().put(adjustedActionType, action);

    processResponses(m, action, documentation, returnName);

    IParameterModel[] parameters = m.getParameters();
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(QUERY_PARAM)) {
        IAnnotationModel paramAnnotation = pm.getAnnotation(QUERY_PARAM);
        QueryParameter value2 = new QueryParameter();
        String paramName = configureParam(pm, value2, documentation, paramAnnotation);
        action.getQueryParameters().put(paramName, value2);
      }
    }
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(HEADER_PARAM)) {
        IAnnotationModel paramAnnotation = pm.getAnnotation(HEADER_PARAM);
        Header value2 = new Header();
        String paramName = configureParam(pm, value2, documentation, paramAnnotation);
        action.getHeaders().put(paramName, value2);
      }
    }
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(PATH_PARAM)) {
        IAnnotationModel paramAnnotation = pm.getAnnotation(PATH_PARAM);
        UriParameter value2 = new UriParameter();
        String paramName = configureParam(pm, value2, documentation, paramAnnotation);
        res.getUriParameters().put(paramName, value2);
      }
    }

    boolean hasBody = m.getBodyType() != null;
    String[] consumesValue =
        extractMediaTypes(m, CONSUMES, classConsumes, hasBody, adjustedActionType);
    if (consumesValue != null) {
      for (String s : consumesValue) {
        s = sanitizeMediaType(s);
        MimeType bodyType = new MimeType();
        tryAppendSchemesAndExamples(bodyType, s, parameterName);
        bodyType.setType(s);
        if (s.contains(FORM)) {
          for (IParameterModel pm : parameters) {
            if (pm.hasAnnotation(FORM_PARAM)) {
              IAnnotationModel paramAnnotation = pm.getAnnotation(FORM_PARAM);
              FormParameter vl = new FormParameter();
              String paramName = configureParam(pm, vl, documentation, paramAnnotation);
              ArrayList<FormParameter> arrayList = new ArrayList<FormParameter>();
              arrayList.add(vl);
              if (bodyType.getFormParameters() == null) {
                bodyType.setFormParameters(new HashMap<String, java.util.List<FormParameter>>());
              }
              bodyType.getFormParameters().put(paramName, arrayList);
            }
          }
        }
        action.getBody().put(s, bodyType);
      }
    }
  }