use of io.vertigo.vega.webservice.metamodel.WebServiceDefinition in project vertigo by KleeGroup.
the class PaginatorAndSortWebServiceHandlerPlugin method handle.
/**
* {@inheritDoc}
*/
@Override
public Object handle(final Request request, final Response response, final WebServiceCallContext routeContext, final HandlerChain chain) throws SessionException {
final WebServiceDefinition webServiceDefinition = routeContext.getWebServiceDefinition();
// Criteria in body
// DtListState in query //see at WebServiceDefinitionBuilder withAutoSortAndPagination it defined where DtListState was
// serverToken in DtListState
final WebServiceParam uiListWebServiceParams = lookupWebServiceParam(webServiceDefinition, DtListState.class);
final WebServiceParam serverTokenWebServiceParams = lookupWebServiceParam(webServiceDefinition, LIST_SERVER_TOKEN);
final DtListState parsedDtListState = (DtListState) routeContext.getParamValue(uiListWebServiceParams);
final DtListState dtListState = checkAndEnsureDefaultValue(parsedDtListState);
final Optional<String> listServerToken = (Optional<String>) routeContext.getParamValue(serverTokenWebServiceParams);
Optional<DtList<?>> fullListOption = Optional.empty();
if (listServerToken.isPresent()) {
fullListOption = tokenManager.get(listServerToken.get());
response.header(LIST_SERVER_TOKEN, listServerToken.get());
}
final DtList<?> fullList;
if (fullListOption.isPresent()) {
fullList = fullListOption.get();
} else {
final Object result = chain.handle(request, response, routeContext);
Assertion.checkArgument(result instanceof DtList, "sort and pagination only supports DtList");
fullList = (DtList<?>) result;
final String fullListServerToken = tokenManager.put(fullList);
response.header(LIST_SERVER_TOKEN, fullListServerToken);
}
// TODO total count should be list meta
response.header("x-total-count", String.valueOf(fullList.size()));
final DtList<?> filteredList = applySortAndPagination(fullList, dtListState);
filteredList.setMetaData(DtList.TOTAL_COUNT_META, fullList.size());
return filteredList;
}
use of io.vertigo.vega.webservice.metamodel.WebServiceDefinition in project vertigo by KleeGroup.
the class WebServiceManagerImpl method scanComponents.
/**
* Scan WebServices as WebServiceDefinitions on all the components.
* @param componentSpace ComponentSpace
* @return Scanned webServiceDefinitions
*/
List<WebServiceDefinition> scanComponents(final ComponentSpace componentSpace) {
final AopPlugin aopPlugin = Home.getApp().getConfig().getBootConfig().getAopPlugin();
final ListBuilder<WebServiceDefinition> allWebServiceDefinitionListBuilder = new ListBuilder<>();
// 1- We introspect all RestfulService class
for (final String componentId : componentSpace.keySet()) {
final Object component = componentSpace.resolve(componentId, Object.class);
if (component instanceof WebServices) {
final WebServices webServices = aopPlugin.unwrap(WebServices.class.cast(component));
final List<WebServiceDefinition> webServiceDefinitions = webServiceScannerPlugin.scanWebService(webServices.getClass());
allWebServiceDefinitionListBuilder.addAll(webServiceDefinitions);
}
}
// 2- We sort by path, parameterized path should be after strict path
return allWebServiceDefinitionListBuilder.sort(Comparator.comparing(WebServiceDefinition::getName)).unmodifiable().build();
}
use of io.vertigo.vega.webservice.metamodel.WebServiceDefinition in project vertigo by KleeGroup.
the class WsTsGeneratorPlugin method generateRoute.
private static void generateRoute(final String targetSubDir, final FileGeneratorConfig fileGeneratorConfig, final MdaResultBuilder mdaResultBuilder) {
final Collection<WebServiceDefinition> webServiceDefinitions = getWebServiceDefinitions();
if (!webServiceDefinitions.isEmpty()) {
final Map<String, List<WebServiceDefinitionModelTs>> webServicesPerFacades = new HashMap<>();
for (final WebServiceDefinition webServiceDefinition : webServiceDefinitions) {
final String facadeName = webServiceDefinition.getMethod().getDeclaringClass().getSimpleName().replaceAll("WebServices", "");
List<WebServiceDefinitionModelTs> facadeWebServiceDefinitions = webServicesPerFacades.get(facadeName);
if (facadeWebServiceDefinitions == null) {
facadeWebServiceDefinitions = new ArrayList<>();
webServicesPerFacades.put(facadeName, facadeWebServiceDefinitions);
}
facadeWebServiceDefinitions.add(new WebServiceDefinitionModelTs(webServiceDefinition));
}
final Map<String, List<WebServiceInitializerModelTs>> facadeByPackage = new HashMap<>();
for (final Map.Entry<String, List<WebServiceDefinitionModelTs>> entry : webServicesPerFacades.entrySet()) {
final String packageName = entry.getValue().get(0).getFunctionnalPackageName();
final String simpleClassName = entry.getKey();
final String jsFileNameWithoutExtension = JsFileNameUtil.convertCamelCaseToJsCase(simpleClassName);
final Set<String> importList = new HashSet<>();
final List<WebServiceDefinitionModelTs> routeList = entry.getValue();
for (final WebServiceDefinitionModelTs route : routeList) {
importList.addAll(route.getImportList());
}
if (!facadeByPackage.containsKey(packageName)) {
facadeByPackage.put(packageName, new ArrayList<WebServiceInitializerModelTs>());
}
facadeByPackage.get(packageName).add(new WebServiceInitializerModelTs(jsFileNameWithoutExtension, simpleClassName));
final Map<String, Object> model = new MapBuilder<String, Object>().put("routes", entry.getValue()).put("importList", importList).build();
FileGenerator.builder(fileGeneratorConfig).withModel(model).withFileName(jsFileNameWithoutExtension + ".ts").withGenSubDir(targetSubDir).withPackageName(fileGeneratorConfig.getProjectPackageName() + ".ui." + packageName + ".services.generated").withTemplateName("webservice/template/routejsts.ftl").build().generateFile(mdaResultBuilder);
}
for (final Map.Entry<String, List<WebServiceInitializerModelTs>> entry : facadeByPackage.entrySet()) {
final Map<String, Object> model = new MapBuilder<String, Object>().put("serviceList", entry.getValue()).build();
FileGenerator.builder(fileGeneratorConfig).withModel(model).withFileName("service-gen-initializer.ts").withGenSubDir(targetSubDir).withPackageName(fileGeneratorConfig.getProjectPackageName() + ".ui." + entry.getKey() + ".initializer.generated").withTemplateName("webservice/template/service-initializer.ftl").build().generateFile(mdaResultBuilder);
FileGenerator.builder(fileGeneratorConfig).withModel(model).withFileName("service-type.ts").withGenSubDir(targetSubDir).withPackageName(fileGeneratorConfig.getProjectPackageName() + ".ui." + entry.getKey() + ".services.generated").withTemplateName("webservice/template/service-type.ftl").build().generateFile(mdaResultBuilder);
}
}
}
use of io.vertigo.vega.webservice.metamodel.WebServiceDefinition in project vertigo by KleeGroup.
the class RestfulServiceWebServiceHandlerPlugin method makeArgs.
private static Object[] makeArgs(final WebServiceCallContext routeContext) {
final WebServiceDefinition webServiceDefinition = routeContext.getWebServiceDefinition();
if (webServiceDefinition.isAutoSortAndPagination()) {
// when using AutoSortAndPagination, there is 2 automanaged params
final Object[] serviceArgs = new Object[webServiceDefinition.getWebServiceParams().size() - 2];
int i = 0;
for (final WebServiceParam webServiceParam : webServiceDefinition.getWebServiceParams()) {
if (!webServiceParam.getType().isAssignableFrom(DtListState.class) && !webServiceParam.getName().equals(PaginatorAndSortWebServiceHandlerPlugin.LIST_SERVER_TOKEN)) {
serviceArgs[i] = routeContext.getParamValue(webServiceParam);
i++;
}
}
return serviceArgs;
}
return webServiceDefinition.getWebServiceParams().stream().map(webServiceParam -> routeContext.getParamValue(webServiceParam)).toArray();
}
use of io.vertigo.vega.webservice.metamodel.WebServiceDefinition in project vertigo by KleeGroup.
the class RestfulServiceWebServiceHandlerPlugin method handle.
/**
* {@inheritDoc}
*/
@Override
public Object handle(final Request request, final Response response, final WebServiceCallContext routeContext, final HandlerChain chain) throws SessionException {
final WebServiceDefinition webServiceDefinition = routeContext.getWebServiceDefinition();
final Object[] serviceArgs = makeArgs(routeContext);
final Method method = webServiceDefinition.getMethod();
final WebServices webServices = (WebServices) Home.getApp().getComponentSpace().resolve(method.getDeclaringClass());
if (method.getName().startsWith("create")) {
// by convention, if method starts with 'create', an http 201 status code is returned (if ok)
response.status(HttpServletResponse.SC_CREATED);
}
try {
return ClassUtil.invoke(webServices, method, serviceArgs);
} catch (final RuntimeException e) {
// If throwed exception was ValidationUserException, VUserException, SessionException, VSecurityException, RuntimeException
// we re throw it
final Throwable cause = e.getCause();
if (cause instanceof InvocationTargetException) {
final Throwable targetException = ((InvocationTargetException) cause).getTargetException();
if (targetException instanceof ValidationUserException) {
throw (ValidationUserException) targetException;
} else if (targetException instanceof VUserException) {
throw (VUserException) targetException;
} else if (targetException instanceof SessionException) {
throw (SessionException) targetException;
} else if (targetException instanceof VSecurityException) {
throw (VSecurityException) targetException;
} else if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
}
}
throw e;
}
}
Aggregations