private void addResourceMethod(
      final JDefinedClass resourceInterface,
      final String resourceInterfacePath,
      final Action action,
      final MimeType bodyMimeType,
      final boolean addBodyMimeTypeInMethodName,
      final Collection<MimeType> uniqueResponseMimeTypes)
      throws Exception {
    final String methodName =
        Names.buildResourceMethodName(action, addBodyMimeTypeInMethodName ? bodyMimeType : null);

    final JType resourceMethodReturnType =
        getResourceMethodReturnType(
            methodName, action, uniqueResponseMimeTypes.isEmpty(), resourceInterface);

    // the actually created unique method name should be needed in the previous method but
    // no way of doing this :(
    final JMethod method =
        context.createResourceMethod(resourceInterface, methodName, resourceMethodReturnType);

    context.addHttpMethodAnnotation(action.getType().toString(), method);

    addParamAnnotation(resourceInterfacePath, action, method);
    addConsumesAnnotation(bodyMimeType, method);
    addProducesAnnotation(uniqueResponseMimeTypes, method);

    final JDocComment javadoc = addBaseJavaDoc(action, method);

    addPathParameters(action, method, javadoc);
    addHeaderParameters(action, method, javadoc);
    addQueryParameters(action, method, javadoc);
    addBodyParameters(bodyMimeType, method, javadoc);
  }
  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);
    }
  }