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;
}
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();
}
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);
}
}
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;
}
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();
}
Aggregations