Search in sources :

Example 1 with WebServiceDefinition

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;
}
Also used : WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam) Optional(java.util.Optional) DtListState(io.vertigo.dynamo.domain.model.DtListState) DtObject(io.vertigo.dynamo.domain.model.DtObject) DtList(io.vertigo.dynamo.domain.model.DtList)

Example 2 with WebServiceDefinition

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();
}
Also used : WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) WebServices(io.vertigo.vega.webservice.WebServices) ListBuilder(io.vertigo.util.ListBuilder) AopPlugin(io.vertigo.core.component.AopPlugin)

Example 3 with WebServiceDefinition

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) WebServiceDefinitionModelTs(io.vertigo.studio.plugins.mda.webservice.model.WebServiceDefinitionModelTs) WebServiceInitializerModelTs(io.vertigo.studio.plugins.mda.webservice.model.WebServiceInitializerModelTs) WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) ArrayList(java.util.ArrayList) List(java.util.List) MapBuilder(io.vertigo.util.MapBuilder) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with WebServiceDefinition

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();
}
Also used : WebServiceHandlerPlugin(io.vertigo.vega.impl.webservice.WebServiceHandlerPlugin) HttpServletResponse(javax.servlet.http.HttpServletResponse) ValidationUserException(io.vertigo.vega.webservice.validation.ValidationUserException) ClassUtil(io.vertigo.util.ClassUtil) WebServices(io.vertigo.vega.webservice.WebServices) DtListState(io.vertigo.dynamo.domain.model.DtListState) InvocationTargetException(java.lang.reflect.InvocationTargetException) VUserException(io.vertigo.lang.VUserException) Home(io.vertigo.app.Home) VSecurityException(io.vertigo.vega.webservice.exception.VSecurityException) WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam) Request(spark.Request) SessionException(io.vertigo.vega.webservice.exception.SessionException) Response(spark.Response) WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) Method(java.lang.reflect.Method) WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam)

Example 5 with WebServiceDefinition

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;
    }
}
Also used : WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) ValidationUserException(io.vertigo.vega.webservice.validation.ValidationUserException) WebServices(io.vertigo.vega.webservice.WebServices) SessionException(io.vertigo.vega.webservice.exception.SessionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) VSecurityException(io.vertigo.vega.webservice.exception.VSecurityException) VUserException(io.vertigo.lang.VUserException)

Aggregations

WebServiceDefinition (io.vertigo.vega.webservice.metamodel.WebServiceDefinition)10 WebServiceParam (io.vertigo.vega.webservice.metamodel.WebServiceParam)4 DtObject (io.vertigo.dynamo.domain.model.DtObject)3 WebServices (io.vertigo.vega.webservice.WebServices)3 SessionException (io.vertigo.vega.webservice.exception.SessionException)3 ValidationUserException (io.vertigo.vega.webservice.validation.ValidationUserException)3 DtListState (io.vertigo.dynamo.domain.model.DtListState)2 VUserException (io.vertigo.lang.VUserException)2 VSecurityException (io.vertigo.vega.webservice.exception.VSecurityException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Home (io.vertigo.app.Home)1 AopPlugin (io.vertigo.core.component.AopPlugin)1 DtList (io.vertigo.dynamo.domain.model.DtList)1 WebServiceDefinitionModelTs (io.vertigo.studio.plugins.mda.webservice.model.WebServiceDefinitionModelTs)1 WebServiceInitializerModelTs (io.vertigo.studio.plugins.mda.webservice.model.WebServiceInitializerModelTs)1 ClassUtil (io.vertigo.util.ClassUtil)1 ListBuilder (io.vertigo.util.ListBuilder)1