protected List<String> getParamNamesForCommand(final BaseCmd cmd) { final List<String> paramNames = new ArrayList<String>(); // The expected param names are all the specific for the current command class ... for (final Field field : cmd.getParamFields()) { final Parameter parameterAnnotation = field.getAnnotation(Parameter.class); paramNames.add(parameterAnnotation.name()); } // ... plus the default ones paramNames.addAll(defaultParamNames); return paramNames; } }
public static void set(BaseCmd cmd, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException { for (Field field : cmd.getClass().getDeclaredFields()) { Parameter parameter = field.getAnnotation(Parameter.class); if (parameter != null && fieldName.equals(parameter.name())) { field.setAccessible(true); field.set(cmd, value); } } }
validFields.add(field); } else { s_logger.debug("Ignoring paremeter " + parameterAnnotation.name() + " as the caller is not authorized to pass it in");
continue; Object paramObj = parameterMap.get(parameterAnnotation.name()); if (paramObj != null) { if (!parameterAnnotation.acceptedOnAdminPort()) { throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, "Parameter " + parameterAnnotation.name() + " can't be passed through the API integration port");
CallContext.current().putContextParameter(entity, internalId); validateNaturalNumber(internalId, annotation.name()); return internalId; if (s_logger.isDebugEnabled()) s_logger.debug("Object entity uuid = " + uuid + " does not exist in the database."); throw new InvalidParameterValueException("Invalid parameter " + annotation.name() + " value=" + uuid + " due to incorrect long value format, or entity does not exist or due to incorrect parameter annotation for the field in api cmd class."); validateNaturalNumber(internalId, annotation.name()); return internalId;
Parameter parameterAnnotation = f.getAnnotation(Parameter.class); if (parameterAnnotation != null && parameterAnnotation.expose() && parameterAnnotation.includeInApiDoc()) { Argument reqArg = new Argument(parameterAnnotation.name()); reqArg.setRequired(parameterAnnotation.required()); if (!parameterAnnotation.description().isEmpty()) { if (parameterAnnotation.name().equals("id")) { id = reqArg; } else {
final Object paramObj = params.get(parameterAnnotation.name()); if (paramObj == null) { if (parameterAnnotation.required()) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to execute API command " + cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8) + " due to missing parameter " + parameterAnnotation.name()); if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name()); parameterAnnotation.name()); } catch (final ParseException parseEx) { if (s_logger.isDebugEnabled()) {
private void validateField(final Object paramObj, final Parameter annotation) throws ServerApiException { if (annotation == null) { return; } final String argName = annotation.name(); for (final ApiArgValidator validator : annotation.validations()) { if (validator == null) { continue; } switch (validator) { case NotNullOrEmpty: switch (annotation.type()) { case UUID: case STRING: validateNonEmptyString(paramObj, argName); break; } break; case PositiveNumber: switch (annotation.type()) { case SHORT: case INTEGER: case LONG: validateNaturalNumber(paramObj, argName); break; } break; } } }
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; }