Search in sources :

Example 1 with WebServiceParam

use of io.vertigo.vega.webservice.metamodel.WebServiceParam 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 WebServiceParam

use of io.vertigo.vega.webservice.metamodel.WebServiceParam in project vertigo by KleeGroup.

the class AnnotationsWebServiceScannerUtil method buildWebServiceDefinition.

private static Optional<WebServiceDefinition> buildWebServiceDefinition(final Method method) {
    final WebServiceDefinitionBuilder builder = WebServiceDefinition.builder(method);
    final PathPrefix pathPrefix = method.getDeclaringClass().getAnnotation(PathPrefix.class);
    if (pathPrefix != null) {
        builder.withPathPrefix(pathPrefix.value());
    }
    for (final Annotation annotation : method.getAnnotations()) {
        if (annotation instanceof GET) {
            builder.with(Verb.GET, ((GET) annotation).value());
        } else if (annotation instanceof POST) {
            builder.with(Verb.POST, ((POST) annotation).value());
        } else if (annotation instanceof PUT) {
            builder.with(Verb.PUT, ((PUT) annotation).value());
        } else if (annotation instanceof PATCH) {
            builder.with(Verb.PATCH, ((PATCH) annotation).value());
        } else if (annotation instanceof DELETE) {
            builder.with(Verb.DELETE, ((DELETE) annotation).value());
        } else if (annotation instanceof AnonymousAccessAllowed) {
            builder.withNeedAuthentication(false);
        } else if (annotation instanceof SessionLess) {
            builder.withNeedSession(false);
        } else if (annotation instanceof SessionInvalidate) {
            builder.withSessionInvalidate(true);
        } else if (annotation instanceof ExcludedFields) {
            builder.addExcludedFields(((ExcludedFields) annotation).value());
        } else if (annotation instanceof IncludedFields) {
            builder.addIncludedFields(((IncludedFields) annotation).value());
        } else if (annotation instanceof AccessTokenPublish) {
            builder.withAccessTokenPublish(true);
        } else if (annotation instanceof AccessTokenMandatory) {
            builder.withAccessTokenMandatory(true);
        } else if (annotation instanceof AccessTokenConsume) {
            builder.withAccessTokenMandatory(true);
            builder.withAccessTokenConsume(true);
        } else if (annotation instanceof ServerSideSave) {
            builder.withServerSideSave(true);
        } else if (annotation instanceof AutoSortAndPagination) {
            builder.withAutoSortAndPagination(true);
        } else if (annotation instanceof Doc) {
            builder.withDoc(((Doc) annotation).value());
        }
    }
    if (builder.hasVerb()) {
        final Type[] paramType = method.getGenericParameterTypes();
        final Annotation[][] parameterAnnotation = method.getParameterAnnotations();
        for (int i = 0; i < paramType.length; i++) {
            final WebServiceParam webServiceParam = buildWebServiceParam(parameterAnnotation[i], paramType[i]);
            builder.addWebServiceParam(webServiceParam);
        }
        // ---
        return Optional.of(builder.build());
    }
    return Optional.empty();
}
Also used : SessionLess(io.vertigo.vega.webservice.stereotype.SessionLess) IncludedFields(io.vertigo.vega.webservice.stereotype.IncludedFields) AutoSortAndPagination(io.vertigo.vega.webservice.stereotype.AutoSortAndPagination) AccessTokenMandatory(io.vertigo.vega.webservice.stereotype.AccessTokenMandatory) POST(io.vertigo.vega.webservice.stereotype.POST) ServerSideSave(io.vertigo.vega.webservice.stereotype.ServerSideSave) SessionInvalidate(io.vertigo.vega.webservice.stereotype.SessionInvalidate) Annotation(java.lang.annotation.Annotation) PUT(io.vertigo.vega.webservice.stereotype.PUT) PATCH(io.vertigo.vega.webservice.stereotype.PATCH) AccessTokenConsume(io.vertigo.vega.webservice.stereotype.AccessTokenConsume) AccessTokenPublish(io.vertigo.vega.webservice.stereotype.AccessTokenPublish) DELETE(io.vertigo.vega.webservice.stereotype.DELETE) WebServiceParamType(io.vertigo.vega.webservice.metamodel.WebServiceParam.WebServiceParamType) Type(java.lang.reflect.Type) WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam) GET(io.vertigo.vega.webservice.stereotype.GET) WebServiceDefinitionBuilder(io.vertigo.vega.webservice.metamodel.WebServiceDefinitionBuilder) Doc(io.vertigo.vega.webservice.stereotype.Doc) AnonymousAccessAllowed(io.vertigo.vega.webservice.stereotype.AnonymousAccessAllowed) PathPrefix(io.vertigo.vega.webservice.stereotype.PathPrefix) ExcludedFields(io.vertigo.vega.webservice.stereotype.ExcludedFields)

Example 3 with WebServiceParam

use of io.vertigo.vega.webservice.metamodel.WebServiceParam in project vertigo by KleeGroup.

the class SwaggerApiBuilder method appendParameters.

private void appendParameters(final WebServiceParam webServiceParam, final List<Map<String, Object>> parameters, final Map<String, Object> bodyParameter) {
    if (isOneInMultipleOutParams(webServiceParam)) {
        for (final WebServiceParam pseudoWebServiceParam : createPseudoWebServiceParams(webServiceParam)) {
            final Map<String, Object> parameter = createParameterObject(pseudoWebServiceParam);
            // query params aren't required
            parameter.remove(REQUIRED);
            parameters.add(parameter);
        }
    } else if (isMultipleInOneOutParams(webServiceParam)) {
        final Map<String, Object> parameter = createParameterObject(webServiceParam);
        if (bodyParameter.isEmpty()) {
            bodyParameter.putAll(parameter);
        } else {
            final String newDescription = (String) parameter.get(DESCRIPTION);
            final String oldDescription = (String) bodyParameter.get(DESCRIPTION);
            bodyParameter.put(DESCRIPTION, oldDescription + ", " + newDescription);
            final Map<String, Object> newSchema = (Map<String, Object>) parameter.get(SCHEMA);
            final Map<String, Object> oldSchema = (Map<String, Object>) bodyParameter.get(SCHEMA);
            oldSchema.putAll(newSchema);
        }
    } else {
        final Map<String, Object> parameter = createParameterObject(webServiceParam);
        parameters.add(parameter);
    }
}
Also used : WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam) DtObject(io.vertigo.dynamo.domain.model.DtObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with WebServiceParam

use of io.vertigo.vega.webservice.metamodel.WebServiceParam in project vertigo by KleeGroup.

the class SwaggerApiBuilder method createParametersArray.

private List<Map<String, Object>> createParametersArray(final WebServiceDefinition webServiceDefinition) {
    final Map<String, Object> bodyParameter = new LinkedHashMap<>();
    final List<Map<String, Object>> parameters = new ArrayList<>();
    for (final WebServiceParam webServiceParam : webServiceDefinition.getWebServiceParams()) {
        if (webServiceParam.getParamType() != WebServiceParamType.Implicit) {
            // if implicit : no public parameter
            appendParameters(webServiceParam, parameters, bodyParameter);
        }
    }
    if (webServiceDefinition.isAccessTokenMandatory()) {
        final Map<String, Object> parameter = new LinkedHashMap<>();
        parameter.put("name", "x-access-token");
        parameter.put("in", "header");
        parameter.put(DESCRIPTION, "Security access token, must be sent to allow operation.");
        parameter.put(REQUIRED, "true");
        parameter.put("type", "string");
        parameters.add(parameter);
    }
    if (!bodyParameter.isEmpty()) {
        final String[] splittedDefinitionName = webServiceDefinition.getName().split("\\$");
        final String bodyName = splittedDefinitionName[0].replaceAll("_+", "_") + "$" + splittedDefinitionName[1] + "Body";
        final Map<String, Object> compositeSchema = (Map<String, Object>) bodyParameter.get(SCHEMA);
        bodyParameter.put(SCHEMA, Collections.singletonMap("$ref", "#/definitions/" + bodyName));
        final Map<String, Object> bodyDefinition = new LinkedHashMap<>();
        bodyDefinition.put(REQUIRED, compositeSchema.keySet().toArray(new String[compositeSchema.size()]));
        putIfNotEmpty(bodyDefinition, "properties", compositeSchema);
        builderDefinitions.put(bodyName, bodyDefinition);
        parameters.add(0, bodyParameter);
    }
    return parameters;
}
Also used : WebServiceParam(io.vertigo.vega.webservice.metamodel.WebServiceParam) ArrayList(java.util.ArrayList) DtObject(io.vertigo.dynamo.domain.model.DtObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with WebServiceParam

use of io.vertigo.vega.webservice.metamodel.WebServiceParam 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)

Aggregations

WebServiceParam (io.vertigo.vega.webservice.metamodel.WebServiceParam)7 DtObject (io.vertigo.dynamo.domain.model.DtObject)4 WebServiceDefinition (io.vertigo.vega.webservice.metamodel.WebServiceDefinition)4 DtListState (io.vertigo.dynamo.domain.model.DtListState)2 ValidationUserException (io.vertigo.vega.webservice.validation.ValidationUserException)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Home (io.vertigo.app.Home)1 DtList (io.vertigo.dynamo.domain.model.DtList)1 VUserException (io.vertigo.lang.VUserException)1 ClassUtil (io.vertigo.util.ClassUtil)1 WebServiceHandlerPlugin (io.vertigo.vega.impl.webservice.WebServiceHandlerPlugin)1 WebServices (io.vertigo.vega.webservice.WebServices)1 SessionException (io.vertigo.vega.webservice.exception.SessionException)1 VSecurityException (io.vertigo.vega.webservice.exception.VSecurityException)1 WebServiceDefinitionBuilder (io.vertigo.vega.webservice.metamodel.WebServiceDefinitionBuilder)1 WebServiceParamType (io.vertigo.vega.webservice.metamodel.WebServiceParam.WebServiceParamType)1 ExtendedObject (io.vertigo.vega.webservice.model.ExtendedObject)1