use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class EntitiesMetadataControllerManager method getView.
public String getView(String entityName, String viewName) {
MetaClass metaClass = restControllersUtils.getMetaClass(entityName);
View view = viewRepository.findView(metaClass, viewName);
if (view == null) {
throw new RestAPIException("View not found", String.format("View %s for metaClass %s not found", viewName, entityName), HttpStatus.NOT_FOUND);
}
return viewSerializationAPI.toJson(view);
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class EnumsControllerManager method getEnumInfo.
public EnumInfo getEnumInfo(String enumClassName) {
Class<?> enumClass;
try {
enumClass = Class.forName(enumClassName);
} catch (ClassNotFoundException e) {
throw new RestAPIException("Enum not found", "Enum with class name " + enumClassName + " not found", HttpStatus.NOT_FOUND);
}
List<EnumValueInfo> enumValues = new ArrayList<>();
Object[] enumConstants = enumClass.getEnumConstants();
for (Object enumConstant : enumConstants) {
Enum enumValue = (Enum) enumConstant;
EnumValueInfo enumValueInfo = new EnumValueInfo(enumValue.name(), ((EnumClass) enumValue).getId(), messages.getMessage(enumValue));
enumValues.add(enumValueInfo);
}
return new EnumInfo(enumClass.getName(), enumValues);
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class QueriesControllerManager method _getCount.
protected String _getCount(String entityName, String queryName, String version, Map<String, String> params) {
entityName = restControllerUtils.transformEntityNameIfRequired(entityName, version, JsonTransformationDirection.FROM_VERSION);
LoadContext<Entity> ctx;
try {
ctx = createQueryLoadContext(entityName, queryName, null, null, params);
} catch (ClassNotFoundException | ParseException e) {
throw new RestAPIException("Error on executing the query", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e);
}
long count = dataManager.getCount(ctx);
return String.valueOf(count);
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class QueriesControllerManager method createQueryLoadContext.
protected LoadContext<Entity> createQueryLoadContext(String entityName, String queryName, @Nullable Integer limit, @Nullable Integer offset, Map<String, String> params) throws ClassNotFoundException, ParseException {
MetaClass metaClass = restControllerUtils.getMetaClass(entityName);
checkCanReadEntity(metaClass);
RestQueriesConfiguration.QueryInfo queryInfo = restQueriesConfiguration.getQuery(entityName, queryName);
if (queryInfo == null) {
throw new RestAPIException("Query not found", String.format("Query with name %s for entity %s not found", queryName, entityName), HttpStatus.NOT_FOUND);
}
LoadContext<Entity> ctx = new LoadContext<>(metaClass);
LoadContext.Query query = new LoadContext.Query(queryInfo.getJpql());
if (limit != null) {
query.setMaxResults(limit);
} else {
query.setMaxResults(persistenceManagerClient.getMaxFetchUI(entityName));
}
if (offset != null) {
query.setFirstResult(offset);
}
for (RestQueriesConfiguration.QueryParamInfo paramInfo : queryInfo.getParams()) {
String paramName = paramInfo.getName();
String requestParamValue = params.get(paramName);
if (requestParamValue == null) {
throw new RestAPIException("Query parameter not found", String.format("Query parameter %s not found", paramName), HttpStatus.BAD_REQUEST);
}
Class<?> clazz = ClassUtils.forName(paramInfo.getType(), getClass().getClassLoader());
Object objectParamValue = toObject(clazz, requestParamValue);
query.setParameter(paramName, objectParamValue);
}
if (queryInfo.getJpql().contains(":session$userId")) {
query.setParameter("session$userId", userSessionSource.currentOrSubstitutedUserId());
}
if (queryInfo.getJpql().contains(":session$userLogin")) {
query.setParameter("session$userLogin", userSessionSource.getUserSession().getCurrentOrSubstitutedUser().getLoginLowerCase());
}
query.setCacheable(queryInfo.isCacheable());
ctx.setQuery(query);
ctx.setView(queryInfo.getViewName());
return ctx;
}
use of com.haulmont.restapi.exception.RestAPIException in project cuba by cuba-platform.
the class ServicesControllerManager method _invokeServiceMethod.
@Nullable
protected ServiceCallResult _invokeServiceMethod(String serviceName, String methodName, List<String> paramNames, List<String> paramValuesStr, String modelVersion) {
Object service = AppBeans.get(serviceName);
RestServicesConfiguration.RestMethodInfo restMethodInfo = restServicesConfiguration.getRestMethodInfo(serviceName, methodName, paramNames);
if (restMethodInfo == null) {
throw new RestAPIException("Service method not found", serviceName + "." + methodName + "(" + paramNames.stream().collect(Collectors.joining(",")) + ")", HttpStatus.NOT_FOUND);
}
Method serviceMethod = restMethodInfo.getMethod();
List<Object> paramValues = new ArrayList<>();
Type[] types = restMethodInfo.getMethod().getGenericParameterTypes();
for (int i = 0; i < types.length; i++) {
int idx = i;
try {
idx = paramNames.indexOf(restMethodInfo.getParams().get(i).getName());
paramValues.add(restParseUtils.toObject(types[i], paramValuesStr.get(idx), modelVersion));
} catch (Exception e) {
log.error("Error on parsing service param value", e);
throw new RestAPIException("Invalid parameter value", "Invalid parameter value for " + paramNames.get(idx), HttpStatus.BAD_REQUEST);
}
}
Object methodResult;
try {
methodResult = serviceMethod.invoke(service, paramValues.toArray());
} catch (InvocationTargetException | IllegalAccessException ex) {
if (ex.getCause() instanceof ValidationException) {
throw (ValidationException) ex.getCause();
} else {
log.error("Error on service method invoke", ex.getCause());
throw new RestAPIException("Error on service method invoke", "", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
if (methodResult == null) {
return null;
}
Class<?> methodReturnType = serviceMethod.getReturnType();
if (Entity.class.isAssignableFrom(methodReturnType)) {
Entity entity = (Entity) methodResult;
restControllerUtils.applyAttributesSecurity(entity);
String entityJson = entitySerializationAPI.toJson(entity, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
entityJson = restControllerUtils.transformJsonIfRequired(entity.getMetaClass().getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entityJson);
return new ServiceCallResult(entityJson, true);
} else if (Collection.class.isAssignableFrom(methodReturnType)) {
Class returnTypeArgument = getMethodReturnTypeArgument(serviceMethod);
if ((returnTypeArgument != null && Entity.class.isAssignableFrom(returnTypeArgument)) || isEntitiesCollection((Collection) methodResult)) {
Collection<? extends Entity> entities = (Collection<? extends Entity>) methodResult;
entities.forEach(entity -> restControllerUtils.applyAttributesSecurity(entity));
String entitiesJson = entitySerializationAPI.toJson(entities, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
if (returnTypeArgument != null) {
MetaClass metaClass = metadata.getClass(returnTypeArgument);
if (metaClass != null) {
entitiesJson = restControllerUtils.transformJsonIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entitiesJson);
} else {
log.error("MetaClass for service collection parameter type {} not found", returnTypeArgument);
}
}
return new ServiceCallResult(entitiesJson, true);
} else {
return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
}
} else {
Datatype<?> datatype = Datatypes.get(methodReturnType);
if (datatype != null) {
return new ServiceCallResult(datatype.format(methodResult), false);
} else {
return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
}
}
}
Aggregations