Search in sources :

Example 1 with ApiResponseResponse

use of com.cloud.api.response.ApiResponseResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryServiceImpl method cacheResponseMap.

protected Map<String, List<String>> cacheResponseMap(final Set<Class<?>> cmdClasses) {
    final Map<String, List<String>> responseApiNameListMap = new HashMap<>();
    for (final 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;
        }
        final String apiName = apiCmdAnnotation.name();
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Found api: " + apiName);
        }
        final ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
        final String responseName = apiCmdAnnotation.responseObject().getName();
        if (!responseName.contains("SuccessResponse")) {
            if (!responseApiNameListMap.containsKey(responseName)) {
                responseApiNameListMap.put(responseName, new ArrayList<>());
            }
            responseApiNameListMap.get(responseName).add(apiName);
        }
        response.setRelated(responseName);
        final Field[] responseFields = apiCmdAnnotation.responseObject().getDeclaredFields();
        for (final Field responseField : responseFields) {
            final ApiResponseResponse responseResponse = getFieldResponseMap(responseField);
            response.addApiResponse(responseResponse);
        }
        response.setObjectName("api");
        s_apiNameDiscoveryResponseMap.put(apiName, response);
    }
    for (final String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
        final ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
        final Set<ApiParameterResponse> processedParams = new HashSet<>();
        for (final ApiParameterResponse param : response.getParams()) {
            if (responseApiNameListMap.containsKey(param.getRelated())) {
                final List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
                param.setRelated(StringUtils.join(relatedApis, ","));
            } else {
                param.setRelated(null);
            }
            processedParams.add(param);
        }
        response.setParams(processedParams);
        if (responseApiNameListMap.containsKey(response.getRelated())) {
            final 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(com.cloud.api.response.ApiResponseResponse) ApiParameterResponse(com.cloud.api.response.ApiParameterResponse) APICommand(com.cloud.api.APICommand) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ApiDiscoveryResponse(com.cloud.api.response.ApiDiscoveryResponse) HashSet(java.util.HashSet)

Example 2 with ApiResponseResponse

use of com.cloud.api.response.ApiResponseResponse in project cosmic by MissionCriticalCloud.

the class ApiDiscoveryServiceImpl method getFieldResponseMap.

private ApiResponseResponse getFieldResponseMap(final Field responseField) {
    final ApiResponseResponse responseResponse = new ApiResponseResponse();
    final SerializedName serializedName = responseField.getAnnotation(SerializedName.class);
    final Param param = responseField.getAnnotation(Param.class);
    if (serializedName != null && param != null) {
        responseResponse.setName(serializedName.value());
        responseResponse.setDescription(param.description());
        responseResponse.setType(responseField.getType().getSimpleName().toLowerCase());
        // If response is not of primitive type - we have a nested entity
        final Class fieldClass = param.responseObject();
        if (fieldClass != null) {
            final Class<?> superClass = fieldClass.getSuperclass();
            if (superClass != null) {
                final String superName = superClass.getName();
                if (superName.equals(BaseResponse.class.getName())) {
                    final Field[] fields = fieldClass.getDeclaredFields();
                    for (final Field field : fields) {
                        final ApiResponseResponse innerResponse = getFieldResponseMap(field);
                        if (innerResponse != null) {
                            responseResponse.addApiResponse(innerResponse);
                        }
                    }
                }
            }
        }
    }
    return responseResponse;
}
Also used : BaseResponse(com.cloud.api.BaseResponse) Field(java.lang.reflect.Field) ApiResponseResponse(com.cloud.api.response.ApiResponseResponse) SerializedName(com.google.gson.annotations.SerializedName) Param(com.cloud.serializer.Param)

Aggregations

ApiResponseResponse (com.cloud.api.response.ApiResponseResponse)2 Field (java.lang.reflect.Field)2 APICommand (com.cloud.api.APICommand)1 BaseResponse (com.cloud.api.BaseResponse)1 ApiDiscoveryResponse (com.cloud.api.response.ApiDiscoveryResponse)1 ApiParameterResponse (com.cloud.api.response.ApiParameterResponse)1 Param (com.cloud.serializer.Param)1 SerializedName (com.google.gson.annotations.SerializedName)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1