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 processResponses(
      IMethodModel m, Action action, IDocInfo documentation, String returnName) {

    HashMap<String, ResponseModel> responses = new HashMap<String, ResponseModel>();
    String mainResponseCode = DEFAULT_RESPONSE;
    if (config != null) {
      ActionType actionType = action.getType();
      mainResponseCode = config.getResponseCode(actionType);
    }

    ResponseModel mainResponse = new ResponseModel(mainResponseCode, null, returnName);
    responses.put(mainResponseCode, mainResponse);

    IAnnotationModel apiResponse = m.getAnnotation(ResourceVisitor.API_RESPONSE);
    if (apiResponse != null) {
      String code = apiResponse.getValue(ResourceVisitor.CODE);
      String message = apiResponse.getValue(ResourceVisitor.MESSAGE);
      ResponseModel response = new ResponseModel(code, message, returnName);
      responses.put(code, response);
    }

    IAnnotationModel apiResponses = m.getAnnotation(ResourceVisitor.API_RESPONSES);
    if (apiResponses != null) {
      IAnnotationModel[] subAnnotations = apiResponses.getSubAnnotations("value");
      if (subAnnotations != null) {
        for (IAnnotationModel subAnn : subAnnotations) {
          String code = subAnn.getValue(ResourceVisitor.CODE);
          String message = subAnn.getValue(ResourceVisitor.MESSAGE);

          String adjustedReturnName = returnName;
          String responseQualifiedName = subAnn.getValue(RESPONSE);
          if (responseQualifiedName != null) {
            try {
              Class<?> responseClass = classLoader.loadClass(responseQualifiedName);
              ReflectionType rt = new ReflectionType(responseClass);
              generateXMLSchema(rt, null);
            } catch (ClassNotFoundException e) {
              e.printStackTrace();
            }
            adjustedReturnName = firstLetterToLowerCase(getSimpleName(responseQualifiedName));
          }
          ResponseModel response = responses.get(code);
          if (response == null) {
            response = new ResponseModel(code, message, adjustedReturnName);
            responses.put(code, response);
          } else {
            response.setMessage(message);
            response.setReturnTypeName(adjustedReturnName);
          }
        }
      }
    }

    String[] producesValues = new String[0];
    ITypeModel returnType = m.getReturnedType();
    if (returnType != null) {
      boolean returnsValue = !returnType.getName().toLowerCase().equals("void");
      producesValues = extractMediaTypes(m, PRODUCES, classProduces, returnsValue, null);
      if (producesValues != null) {
        for (ResponseModel responseModel : responses.values()) {
          responseModel.setProduces(producesValues);
        }
      }
    }
    IAnnotationModel apiOperation = m.getAnnotation(API_OPERATION);
    if (apiOperation != null) {
      String responseContainer = apiOperation.getValue("responseContainer");
      StructureType st = StructureType.COMMON;
      if (responseContainer != null) {
        responseContainer = responseContainer.toLowerCase();
        if (responseContainer.equals("set") || responseContainer.equals("list")) {
          st = StructureType.COLLECTION;
        } else if (responseContainer.equals("map")) {
          st = StructureType.MAP;
        }
      }

      String responseQualifiedName = apiOperation.getValue(RESPONSE);
      if (responseQualifiedName != null) {
        try {
          Class<?> responseClass = classLoader.loadClass(responseQualifiedName);
          ReflectionType rt = new ReflectionType(responseClass);
          generateXMLSchema(rt, st);
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        String adjustedReturnType = firstLetterToLowerCase(getSimpleName(responseQualifiedName));
        mainResponse.setReturnTypeName(adjustedReturnType);
      }
    }

    for (ResponseModel rm : responses.values()) {

      Response response = new Response();

      String description = rm.getMessage();
      if (description == null || description.trim().isEmpty()) {
        description = documentation.getReturnInfo();
      }
      if (description != null && !description.trim().isEmpty()) {
        response.setDescription(description);
      }

      String[] produces = rm.getProduces();
      if (produces != null) {

        String returnTypeName = rm.getReturnTypeName();
        for (String mediaType : producesValues) {
          mediaType = sanitizeMediaType(mediaType);
          MimeType mimeType = new MimeType();
          tryAppendSchemesAndExamples(mimeType, mediaType, returnTypeName);
          mimeType.setType(mediaType);
          response.getBody().put(mediaType, mimeType);
        }
      }

      String code = rm.getCode();
      action.getResponses().put(code, response);
    }
  }