Search in sources :

Example 1 with ApiMethodParameter

use of org.entando.entando.aps.system.services.api.model.ApiMethodParameter in project entando-core by entando.

the class ResponseBuilder method checkParameter.

private void checkParameter(ApiMethod apiMethod, Properties parameters) throws ApiException, Throwable {
    try {
        List<ApiMethodParameter> apiParameters = apiMethod.getParameters();
        if (null == apiParameters || apiParameters.isEmpty()) {
            return;
        }
        List<ApiError> errors = new ArrayList<ApiError>();
        for (int i = 0; i < apiParameters.size(); i++) {
            ApiMethodParameter apiParam = apiParameters.get(i);
            String paramName = apiParam.getKey();
            Object value = parameters.get(paramName);
            if (apiParam.isRequired() && (null == value || value.toString().trim().length() == 0)) {
                errors.add(new ApiError(IApiErrorCodes.API_PARAMETER_REQUIRED, "Parameter '" + paramName + "' is required", Response.Status.BAD_REQUEST));
            }
        }
        if (!errors.isEmpty()) {
            throw new ApiException(errors);
        }
    } catch (ApiException t) {
        throw t;
    } catch (Throwable t) {
        _logger.error("Error checking api parameters", t);
        throw new ApsSystemException("Internal Error", t);
    }
}
Also used : ArrayList(java.util.ArrayList) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) ApiMethodParameter(org.entando.entando.aps.system.services.api.model.ApiMethodParameter) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 2 with ApiMethodParameter

use of org.entando.entando.aps.system.services.api.model.ApiMethodParameter in project entando-core by entando.

the class ApiServiceInterface method createServiceInfo.

protected ServiceInfo createServiceInfo(ApiService service, String langCode, String defaultLangCode) {
    String description = service.getDescription().getProperty(langCode);
    if (null == description || description.trim().length() == 0) {
        description = service.getDescription().getProperty(defaultLangCode);
    }
    ServiceInfo smallService = new ServiceInfo(service.getKey(), description, service.getTag());
    String[] freeParameters = service.getFreeParameters();
    if (null != freeParameters && freeParameters.length > 0) {
        for (int i = 0; i < freeParameters.length; i++) {
            String freeParameter = freeParameters[i];
            ApiMethodParameter apiParameter = service.getMaster().getParameter(freeParameter);
            if (null != apiParameter) {
                ServiceParameterInfo spi = new ServiceParameterInfo(apiParameter);
                ApsProperties serviceParameters = service.getParameters();
                String defaultValue = (null != serviceParameters) ? serviceParameters.getProperty(freeParameter) : null;
                if (null != defaultValue) {
                    spi.setDefaultValue(defaultValue);
                    spi.setRequired(false);
                }
                smallService.addParameter(spi);
            }
        }
    }
    return smallService;
}
Also used : ServiceInfo(org.entando.entando.aps.system.services.api.model.ServiceInfo) ApiMethodParameter(org.entando.entando.aps.system.services.api.model.ApiMethodParameter) ServiceParameterInfo(org.entando.entando.aps.system.services.api.model.ServiceParameterInfo) ApsProperties(com.agiletec.aps.util.ApsProperties)

Example 3 with ApiMethodParameter

use of org.entando.entando.aps.system.services.api.model.ApiMethodParameter in project entando-core by entando.

the class ApiServiceAction method checkParameters.

private void checkParameters() {
    try {
        this.setApiParameterValues(new ApsProperties());
        ApiMethod masterMethod = this.getMethod(this.getNamespace(), this.getResourceName());
        List<ApiMethodParameter> apiParameters = masterMethod.getParameters();
        if (null == apiParameters) {
            return;
        }
        this.extractFreeParameters(apiParameters);
        this.setApiParameters(apiParameters);
        for (int i = 0; i < apiParameters.size(); i++) {
            ApiMethodParameter apiParameter = apiParameters.get(i);
            String fieldName = apiParameter.getKey() + "_apiParam";
            String value = this.getRequest().getParameter(fieldName);
            if (null != value && value.trim().length() > 0) {
                this.getApiParameterValues().put(apiParameter.getKey(), value);
            }
            boolean isFreeParameter = (null != this.getFreeParameters()) ? this.getFreeParameters().contains(apiParameter.getKey()) : false;
            if (apiParameter.isRequired() && (null == value || value.trim().length() == 0) && !isFreeParameter) {
                this.addFieldError(fieldName, this.getText("error.service.parameter.invalidSetting", new String[] { apiParameter.getKey(), apiParameter.getDescription() }));
            }
        }
    } catch (Throwable t) {
        _logger.error("Error checking parameters", t);
        throw new RuntimeException("Error checking parameters", t);
    }
}
Also used : ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) ApiMethodParameter(org.entando.entando.aps.system.services.api.model.ApiMethodParameter) ApsProperties(com.agiletec.aps.util.ApsProperties)

Example 4 with ApiMethodParameter

use of org.entando.entando.aps.system.services.api.model.ApiMethodParameter in project entando-core by entando.

the class ApiServiceAction method extractFreeParameters.

private void extractFreeParameters(List<ApiMethodParameter> apiParameters) {
    if (null == apiParameters) {
        return;
    }
    for (int i = 0; i < apiParameters.size(); i++) {
        ApiMethodParameter apiMethodParameter = apiParameters.get(i);
        String requestParamName = "freeParameter_" + apiMethodParameter.getKey();
        String value = this.getRequest().getParameter(requestParamName);
        if (null != value && Boolean.parseBoolean(value)) {
            this.getFreeParameters().add(apiMethodParameter.getKey());
        }
    }
}
Also used : ApiMethodParameter(org.entando.entando.aps.system.services.api.model.ApiMethodParameter)

Aggregations

ApiMethodParameter (org.entando.entando.aps.system.services.api.model.ApiMethodParameter)4 ApsProperties (com.agiletec.aps.util.ApsProperties)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 ArrayList (java.util.ArrayList)1 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)1 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)1 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)1 ServiceInfo (org.entando.entando.aps.system.services.api.model.ServiceInfo)1 ServiceParameterInfo (org.entando.entando.aps.system.services.api.model.ServiceParameterInfo)1