Search in sources :

Example 1 with SerializedName

use of com.google.gson.annotations.SerializedName 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)

Example 2 with SerializedName

use of com.google.gson.annotations.SerializedName in project cosmic by MissionCriticalCloud.

the class ApiXmlDocWriter method setResponseFields.

private static ArrayList<Argument> setResponseFields(final Field[] responseFields, final Class<?> responseClas) {
    final ArrayList<Argument> arguments = new ArrayList<>();
    final ArrayList<Argument> sortedChildlessArguments = new ArrayList<>();
    final ArrayList<Argument> sortedArguments = new ArrayList<>();
    Argument id = null;
    for (final Field responseField : responseFields) {
        final SerializedName nameAnnotation = responseField.getAnnotation(SerializedName.class);
        if (nameAnnotation != null) {
            final Param paramAnnotation = responseField.getAnnotation(Param.class);
            final Argument respArg = new Argument(nameAnnotation.value());
            boolean hasChildren = false;
            if (paramAnnotation != null && paramAnnotation.includeInApiDoc()) {
                final String description = paramAnnotation.description();
                final Class fieldClass = paramAnnotation.responseObject();
                if (description != null && !description.isEmpty()) {
                    respArg.setDescription(description);
                }
                respArg.setDataType(responseField.getType().getSimpleName().toLowerCase());
                if (!paramAnnotation.since().isEmpty()) {
                    respArg.setSinceVersion(paramAnnotation.since());
                }
                if (fieldClass != null) {
                    final Class<?> superClass = fieldClass.getSuperclass();
                    if (superClass != null) {
                        final String superName = superClass.getName();
                        if (superName.equals(BaseResponse.class.getName())) {
                            final ArrayList<Argument> fieldArguments;
                            final Field[] fields = fieldClass.getDeclaredFields();
                            fieldArguments = setResponseFields(fields, fieldClass);
                            respArg.setArguments(fieldArguments);
                            hasChildren = true;
                        }
                    }
                }
            }
            if (paramAnnotation != null && paramAnnotation.includeInApiDoc()) {
                if (nameAnnotation.value().equals("id")) {
                    id = respArg;
                } else {
                    if (hasChildren) {
                        respArg.setName(nameAnnotation.value() + "(*)");
                        sortedArguments.add(respArg);
                    } else {
                        sortedChildlessArguments.add(respArg);
                    }
                }
            }
        }
    }
    Collections.sort(sortedArguments);
    Collections.sort(sortedChildlessArguments);
    if (id != null) {
        arguments.add(id);
    }
    arguments.addAll(sortedChildlessArguments);
    arguments.addAll(sortedArguments);
    if (responseClas.getName().equalsIgnoreCase(AsyncJobResponse.class.getName())) {
        final Argument jobIdArg = new Argument("jobid", "the ID of the async job");
        arguments.add(jobIdArg);
    } else if (AsyncResponses.contains(responseClas.getName())) {
        final Argument jobIdArg = new Argument("jobid", "the ID of the latest async job acting on this object");
        final Argument jobStatusArg = new Argument("jobstatus", "the current status of the latest async job acting on this object");
        arguments.add(jobIdArg);
        arguments.add(jobStatusArg);
    }
    return arguments;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) AsyncJobResponse(com.cloud.api.response.AsyncJobResponse) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) BaseResponse(com.cloud.api.BaseResponse) Param(com.cloud.serializer.Param)

Example 3 with SerializedName

use of com.google.gson.annotations.SerializedName in project cosmic by MissionCriticalCloud.

the class ApiResponseSerializer method serializeResponseObjFieldsXML.

private static void serializeResponseObjFieldsXML(final StringBuilder sb, final StringBuilder log, final ResponseObject obj) {
    boolean isAsync = false;
    if (obj instanceof AsyncJobResponse) {
        isAsync = true;
    }
    final Field[] fields = getFlattenFields(obj.getClass());
    for (final Field field : fields) {
        if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
            // skip transient fields
            continue;
        }
        final SerializedName serializedName = field.getAnnotation(SerializedName.class);
        if (serializedName == null) {
            // skip fields w/o serialized name
            continue;
        }
        boolean logField = true;
        final Param param = field.getAnnotation(Param.class);
        if (param != null) {
            final RoleType[] allowedRoles = param.authorized();
            if (allowedRoles.length > 0) {
                boolean permittedParameter = false;
                final Account caller = CallContext.current().getCallingAccount();
                for (final RoleType allowedRole : allowedRoles) {
                    if (allowedRole.getValue() == caller.getType()) {
                        permittedParameter = true;
                        break;
                    }
                }
                if (!permittedParameter) {
                    s_logger.trace("Ignoring parameter " + param.name() + " as the caller is not authorized to see it");
                    continue;
                }
            }
            if (param.isSensitive()) {
                logField = false;
            }
        }
        field.setAccessible(true);
        final Object fieldValue;
        try {
            fieldValue = field.get(obj);
        } catch (final IllegalArgumentException e) {
            throw new CloudRuntimeException("how illegal is it?", e);
        } catch (final IllegalAccessException e) {
            throw new CloudRuntimeException("come on...we set accessible already", e);
        }
        if (fieldValue != null) {
            if (fieldValue instanceof ResponseObject) {
                final ResponseObject subObj = (ResponseObject) fieldValue;
                if (isAsync) {
                    sb.append("<jobresult>");
                    log.append("<jobresult>");
                }
                serializeResponseObjXML(sb, log, subObj);
                if (isAsync) {
                    sb.append("</jobresult>");
                    log.append("</jobresult>");
                }
            } else if (fieldValue instanceof Collection<?>) {
                final Collection<?> subResponseList = (Collection<?>) fieldValue;
                boolean usedUuidList = false;
                for (final Object value : subResponseList) {
                    if (value instanceof ResponseObject) {
                        final ResponseObject subObj = (ResponseObject) value;
                        if (serializedName != null) {
                            subObj.setObjectName(serializedName.value());
                        }
                        serializeResponseObjXML(sb, log, subObj);
                    } else if (value instanceof ExceptionProxyObject) {
                        // Only exception reponses carry a list of
                        // ExceptionProxyObject objects.
                        final ExceptionProxyObject idProxy = (ExceptionProxyObject) value;
                        // encountered, put in a uuidList tag.
                        if (!usedUuidList) {
                            sb.append("<" + serializedName.value() + ">");
                            log.append("<" + serializedName.value() + ">");
                            usedUuidList = true;
                        }
                        sb.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        log.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        // Append the new descriptive property also.
                        final String idFieldName = idProxy.getDescription();
                        if (idFieldName != null) {
                            sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                            log.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                        }
                    } else if (value instanceof String) {
                        sb.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        if (logField) {
                            log.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        }
                    }
                }
                if (usedUuidList) {
                    // close the uuidList.
                    sb.append("</").append(serializedName.value()).append(">");
                    log.append("</").append(serializedName.value()).append(">");
                }
            } else if (fieldValue instanceof Date) {
                sb.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
                log.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
            } else {
                String resultString = escapeSpecialXmlChars(fieldValue.toString());
                if (!(obj instanceof ExceptionResponse)) {
                    resultString = encodeParam(resultString);
                }
                sb.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                if (logField) {
                    log.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                }
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) RoleType(com.cloud.acl.RoleType) SerializedName(com.google.gson.annotations.SerializedName) Date(java.util.Date) Field(java.lang.reflect.Field) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Param(com.cloud.serializer.Param) ResponseObject(com.cloud.api.ResponseObject) Collection(java.util.Collection) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject) ResponseObject(com.cloud.api.ResponseObject) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject)

Example 4 with SerializedName

use of com.google.gson.annotations.SerializedName in project lastaflute by lastaflute.

the class LaReflectiveTypeAdapterFactory method getFieldNames.

/**
 * first element holds the default name
 * @param f The field to get name. (NotNull)
 * @return The read-only list of field names. (NotNull)
 */
protected List<String> getFieldNames(Field f) {
    final SerializedName annotation = f.getAnnotation(SerializedName.class);
    if (annotation == null) {
        String name = fieldNamingPolicy.translateName(f);
        return Collections.singletonList(name);
    }
    final String serializedName = annotation.value();
    final String[] alternates = annotation.alternate();
    if (alternates.length == 0) {
        return Collections.singletonList(serializedName);
    }
    final List<String> fieldNames = new ArrayList<String>(alternates.length + 1);
    fieldNames.add(serializedName);
    for (String alternate : alternates) {
        fieldNames.add(alternate);
    }
    return fieldNames;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) ArrayList(java.util.ArrayList)

Example 5 with SerializedName

use of com.google.gson.annotations.SerializedName in project maple-ir by LLVM-but-worse.

the class ReflectiveTypeAdapterFactory method getFieldName.

/**
 * first element holds the default name
 */
static List<String> getFieldName(FieldNamingStrategy fieldNamingPolicy, Field f) {
    SerializedName serializedName = f.getAnnotation(SerializedName.class);
    List<String> fieldNames = new LinkedList<String>();
    if (serializedName == null) {
        fieldNames.add(fieldNamingPolicy.translateName(f));
    } else {
        fieldNames.add(serializedName.value());
        for (String alternate : serializedName.alternate()) {
            fieldNames.add(alternate);
        }
    }
    return fieldNames;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) LinkedList(java.util.LinkedList)

Aggregations

SerializedName (com.google.gson.annotations.SerializedName)11 Field (java.lang.reflect.Field)9 Param (com.cloud.serializer.Param)6 ArrayList (java.util.ArrayList)4 BaseResponse (com.cloud.api.BaseResponse)2 Account (com.cloud.user.Account)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ExceptionProxyObject (com.cloud.utils.exception.ExceptionProxyObject)2 Collection (java.util.Collection)2 Date (java.util.Date)2 BaseResponse (org.apache.cloudstack.api.BaseResponse)2 AsyncJobResponse (org.apache.cloudstack.api.response.AsyncJobResponse)2 RoleType (com.cloud.acl.RoleType)1 ResponseObject (com.cloud.api.ResponseObject)1 ApiResponseResponse (com.cloud.api.response.ApiResponseResponse)1 AsyncJobResponse (com.cloud.api.response.AsyncJobResponse)1 KubernetesType (io.kubernetes.client.common.KubernetesType)1 IntOrString (io.kubernetes.client.custom.IntOrString)1 Quantity (io.kubernetes.client.custom.Quantity)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1