示例#1
0
  private ParamsValidator getParamsValidator(
      Class<? extends ParamsValidator> validatorClass, String[] customParams) throws ApiException {

    // use cache only if there is no custom parameters
    boolean useCache = customParams == null || customParams.length == 0;

    if (useCache) {
      ParamsValidator validator = paramsValidatorsMap.get(validatorClass);
      if (validator != null) {
        return validator;
      }
    }

    try {
      ParamsValidator validator = validatorClass.newInstance();
      validator.setCustomParams(customParams);

      if (useCache) {
        paramsValidatorsMap.put(validatorClass, validator);
      }
      return validator;
    } catch (Exception e) {
      throw new ApiException("Failed to instantiate a validator: " + validatorClass.getName());
    }
  }
示例#2
0
  private ModelValidator getModelValidator(Class<? extends ModelValidator> validatorClass)
      throws ApiException {
    ModelValidator validator = modelValidatorsMap.get(validatorClass);
    if (validator != null) {
      return validator;
    }

    try {
      validator = validatorClass.newInstance();
      modelValidatorsMap.put(validatorClass, validator);
    } catch (Exception e) {
      throw new ApiException("Failed to instantiate a validator: " + validatorClass.getName());
    }

    return validator;
  }
示例#3
0
  private void fillParamsMetadata(ApiAction action, MethodRef methodRef) throws ApiException {
    final Class[] params = methodRef.getMethod().getParameterTypes();

    for (final Class paramType : params) {
      if (paramType.equals(RequestContext.class)) {
        methodRef.addParam(RequestObjectParam.getInstance());
      } else if (paramType.equals(HttpServletRequest.class)) {
        methodRef.addParam(HttpServletRequestParam.getInstance());
      } else if (paramType.equals(HttpServletResponse.class)) {
        methodRef.addParam(HttpServletResponseParam.getInstance());
      } else if (Model.class.isAssignableFrom(paramType) && action.getModelType() == null) {
        methodRef.addParam(buildModelParam(paramType));
        action.setModelType(paramType);
      } else {
        throw new ApiException(
            "Unexpected action parameter " + paramType.getName() + " for action " + action);
      }
    }
  }
示例#4
0
  private ModelParam buildModelParam(Class type) throws ApiException {
    ModelParam param = paramsCache.get(type);
    if (param != null) {
      return param;
    }

    Class<Model> modelType = (Class<Model>) type;
    param = new ModelParam(modelType);
    Validator annotation = modelType.getAnnotation(Validator.class);
    if (annotation != null) {
      final Class<? extends ModelValidator> validatorClass = annotation.value();
      if (validatorClass == null) {
        throw new ApiException("Validator is not specified for model " + type.getName());
      }

      ModelValidator validator = getModelValidator(validatorClass);
      if (validator != null) {
        param.setValidator(validator);
      }
    }

    return param;
  }