Search in sources :

Example 1 with ApiParameterResponse

use of org.apache.cloudstack.api.response.ApiParameterResponse in project cloudstack by apache.

the class ApiDiscoveryServiceImpl method getCmdRequestMap.

private ApiDiscoveryResponse getCmdRequestMap(Class<?> cmdClass, APICommand apiCmdAnnotation) {
    String apiName = apiCmdAnnotation.name();
    ApiDiscoveryResponse response = new ApiDiscoveryResponse();
    response.setName(apiName);
    response.setDescription(apiCmdAnnotation.description());
    if (!apiCmdAnnotation.since().isEmpty()) {
        response.setSince(apiCmdAnnotation.since());
    }
    Set<Field> fields = ReflectUtil.getAllFieldsForClass(cmdClass, new Class<?>[] { BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
    boolean isAsync = ReflectUtil.isCmdClassAsync(cmdClass, new Class<?>[] { BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
    response.setAsync(isAsync);
    for (Field field : fields) {
        Parameter parameterAnnotation = field.getAnnotation(Parameter.class);
        if (parameterAnnotation != null && parameterAnnotation.expose() && parameterAnnotation.includeInApiDoc()) {
            ApiParameterResponse paramResponse = new ApiParameterResponse();
            paramResponse.setName(parameterAnnotation.name());
            paramResponse.setDescription(parameterAnnotation.description());
            paramResponse.setType(parameterAnnotation.type().toString().toLowerCase());
            paramResponse.setLength(parameterAnnotation.length());
            paramResponse.setRequired(parameterAnnotation.required());
            if (!parameterAnnotation.since().isEmpty()) {
                paramResponse.setSince(parameterAnnotation.since());
            }
            paramResponse.setRelated(parameterAnnotation.entityType()[0].getName());
            response.addParam(paramResponse);
        }
    }
    return response;
}
Also used : Field(java.lang.reflect.Field) ApiParameterResponse(org.apache.cloudstack.api.response.ApiParameterResponse) Parameter(org.apache.cloudstack.api.Parameter) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse)

Example 2 with ApiParameterResponse

use of org.apache.cloudstack.api.response.ApiParameterResponse in project cloudstack by apache.

the class ApiDiscoveryServiceImpl method cacheResponseMap.

protected Map<String, List<String>> cacheResponseMap(Set<Class<?>> cmdClasses) {
    Map<String, List<String>> responseApiNameListMap = new HashMap<String, List<String>>();
    for (Class<?> cmdClass : cmdClasses) {
        APICommand apiCmdAnnotation = cmdClass.getAnnotation(APICommand.class);
        if (apiCmdAnnotation == null) {
            apiCmdAnnotation = cmdClass.getSuperclass().getAnnotation(APICommand.class);
        }
        if (apiCmdAnnotation == null || !apiCmdAnnotation.includeInApiDoc() || apiCmdAnnotation.name().isEmpty()) {
            continue;
        }
        String apiName = apiCmdAnnotation.name();
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Found api: " + apiName);
        }
        ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
        Class<? extends BaseResponse> responseClass = apiCmdAnnotation.responseObject();
        String responseName = responseClass.getName();
        if (!responseName.contains("SuccessResponse")) {
            if (!responseApiNameListMap.containsKey(responseName)) {
                responseApiNameListMap.put(responseName, new ArrayList<String>());
            }
            responseApiNameListMap.get(responseName).add(apiName);
        }
        response.setRelated(responseName);
        Set<Field> responseFields = ReflectionUtils.getAllFields(responseClass);
        for (Field responseField : responseFields) {
            ApiResponseResponse responseResponse = getFieldResponseMap(responseField);
            response.addApiResponse(responseResponse);
        }
        response.setObjectName("api");
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    for (String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
        ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
        Set<ApiParameterResponse> processedParams = new HashSet<ApiParameterResponse>();
        for (ApiParameterResponse param : response.getParams()) {
            if (responseApiNameListMap.containsKey(param.getRelated())) {
                List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
                param.setRelated(StringUtils.defaultString(StringUtils.join(relatedApis, ",")));
            } else {
                param.setRelated(null);
            }
            processedParams.add(param);
        }
        response.setParams(processedParams);
        if (responseApiNameListMap.containsKey(response.getRelated())) {
            List<String> relatedApis = responseApiNameListMap.get(response.getRelated());
            relatedApis.remove(apiName);
            response.setRelated(StringUtils.join(relatedApis, ","));
        } else {
            response.setRelated(null);
        }
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    return responseApiNameListMap;
}
Also used : HashMap(java.util.HashMap) ApiResponseResponse(org.apache.cloudstack.api.response.ApiResponseResponse) ApiParameterResponse(org.apache.cloudstack.api.response.ApiParameterResponse) APICommand(org.apache.cloudstack.api.APICommand) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ApiDiscoveryResponse(org.apache.cloudstack.api.response.ApiDiscoveryResponse) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Field (java.lang.reflect.Field)2 ApiDiscoveryResponse (org.apache.cloudstack.api.response.ApiDiscoveryResponse)2 ApiParameterResponse (org.apache.cloudstack.api.response.ApiParameterResponse)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 APICommand (org.apache.cloudstack.api.APICommand)1 Parameter (org.apache.cloudstack.api.Parameter)1 ApiResponseResponse (org.apache.cloudstack.api.response.ApiResponseResponse)1