use of com.linkedin.restli.server.annotations.PathKeysParam in project rest.li by linkedin.
the class TestRestLiAnnotationReader method collectionRootResourceMethodPathKeyParameters.
@Test(description = "verifies path key and path keys parameters for entity level actions")
public void collectionRootResourceMethodPathKeyParameters() {
final ResourceModel model = RestLiAnnotationReader.processResource(PathKeyParamAnnotationsResource.class);
Assert.assertNotNull(model);
final Map<String, ResourceMethodDescriptor> nameToDescriptor = model.getResourceMethodDescriptors().stream().collect(Collectors.toMap(ResourceMethodDescriptor::getMethodName, Function.identity()));
// first method
final ResourceMethodDescriptor withPathKeyParamMethod = nameToDescriptor.get("withPathKeyParam");
Assert.assertNotNull(withPathKeyParamMethod);
final Action withPathKeyParamMethodAction = withPathKeyParamMethod.getAnnotation(Action.class);
Assert.assertNotNull(withPathKeyParamMethodAction);
Assert.assertEquals("withPathKeyParam", withPathKeyParamMethodAction.name());
final List<Parameter<?>> withPathKeyParamMethodParams = withPathKeyParamMethod.getParameters();
Assert.assertEquals(1, withPathKeyParamMethodParams.size());
final AnnotationSet withPathKeyParamMethodParamAnnotations = withPathKeyParamMethodParams.get(0).getAnnotations();
Assert.assertNotNull(withPathKeyParamMethodParamAnnotations);
Assert.assertTrue(withPathKeyParamMethodParamAnnotations.contains(PathKeyParam.class));
final PathKeyParam pathKeyParam = withPathKeyParamMethodParamAnnotations.get(PathKeyParam.class);
Assert.assertNotNull(pathKeyParam);
Assert.assertEquals("pathKeyParamAnnotationsId", pathKeyParam.value());
// second method
final ResourceMethodDescriptor withPathKeysParamMethod = nameToDescriptor.get("withPathKeysParam");
Assert.assertNotNull(withPathKeysParamMethod);
final Action withPathKeysParamMethodAction = withPathKeysParamMethod.getAnnotation(Action.class);
Assert.assertNotNull(withPathKeysParamMethodAction);
Assert.assertEquals("withPathKeysParam", withPathKeysParamMethodAction.name());
final List<Parameter<?>> withPathKeysParamMethodParams = withPathKeysParamMethod.getParameters();
Assert.assertEquals(1, withPathKeysParamMethodParams.size());
final AnnotationSet withPathKeysParamMethodParamAnnotations = withPathKeysParamMethodParams.get(0).getAnnotations();
Assert.assertNotNull(withPathKeysParamMethodParamAnnotations);
Assert.assertTrue(withPathKeysParamMethodParamAnnotations.contains(PathKeysParam.class));
final PathKeysParam pathKeysParam = withPathKeysParamMethodParamAnnotations.get(PathKeysParam.class);
Assert.assertNotNull(pathKeysParam);
}
use of com.linkedin.restli.server.annotations.PathKeysParam in project rest.li by linkedin.
the class RestLiAnnotationReader method buildPathKeysParam.
@SuppressWarnings("deprecation")
private static Parameter<?> buildPathKeysParam(final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
if (!paramType.equals(PathKeys.class)) {
throw new ResourceConfigException("Incorrect data type for param: @" + PathKeysParam.class.getSimpleName() + " or @" + com.linkedin.restli.server.annotations.Keys.class.getSimpleName() + " parameter annotation must be of type " + PathKeys.class.getName());
}
Optional optional = annotations.get(Optional.class);
Parameter.ParamType parameter = null;
if (paramAnnotationType.equals(com.linkedin.restli.server.annotations.Keys.class)) {
parameter = Parameter.ParamType.PATH_KEYS;
} else if (paramAnnotationType.equals(PathKeysParam.class)) {
parameter = Parameter.ParamType.PATH_KEYS_PARAM;
} else {
throw new ResourceConfigException("Param Annotation type must be 'PathKeysParam' or the deprecated 'Keys' for PathKeys");
}
@SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter("", paramType, null, optional != null, new PathKeysImpl(), parameter, false, annotations);
return param;
}
use of com.linkedin.restli.server.annotations.PathKeysParam in project rest.li by linkedin.
the class RestLiAnnotationReader method getParameters.
@SuppressWarnings("deprecation")
private static List<Parameter<?>> getParameters(final ResourceModel model, final Method method, final ResourceMethod methodType) {
Set<String> paramNames = new HashSet<>();
List<Parameter<?>> queryParameters = new ArrayList<>();
Annotation[][] paramsAnnos = method.getParameterAnnotations();
// Iterate over the method parameters.
for (int idx = 0; idx < paramsAnnos.length; idx++) {
AnnotationSet paramAnnotations = new AnnotationSet(paramsAnnos[idx]);
Class<?> paramType = method.getParameterTypes()[idx];
Parameter<?> param = getPositionalParameter(model, methodType, idx, paramAnnotations);
// if no positional definition, look for custom annotated parameters
if (param == null) {
if (paramAnnotations.contains(QueryParam.class)) {
param = buildQueryParam(method, paramAnnotations, paramType);
} else if (paramAnnotations.contains(ActionParam.class)) {
param = buildActionParam(method, paramAnnotations, paramType);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.AssocKey.class)) {
param = buildAssocKeyParam(model, method, paramAnnotations, paramType, com.linkedin.restli.server.annotations.AssocKey.class);
} else if (paramAnnotations.contains(AssocKeyParam.class)) {
param = buildAssocKeyParam(model, method, paramAnnotations, paramType, AssocKeyParam.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Context.class)) {
param = buildPagingContextParam(paramAnnotations, paramType, com.linkedin.restli.server.annotations.Context.class);
} else if (paramAnnotations.contains(PagingContextParam.class)) {
param = buildPagingContextParam(paramAnnotations, paramType, PagingContextParam.class);
} else if (paramAnnotations.contains(CallbackParam.class)) {
param = buildCallbackParam(method, methodType, idx, paramType, paramAnnotations);
} else if (paramAnnotations.contains(ParSeqContextParam.class)) {
param = buildParSeqContextParam(method, methodType, idx, paramType, paramAnnotations, ParSeqContextParam.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.ParSeqContext.class)) {
param = buildParSeqContextParam(method, methodType, idx, paramType, paramAnnotations, com.linkedin.restli.server.annotations.ParSeqContext.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Projection.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PROJECTION);
} else if (paramAnnotations.contains(ProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PROJECTION_PARAM);
} else if (paramAnnotations.contains(MetadataProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.METADATA_PROJECTION_PARAM);
} else if (paramAnnotations.contains(PagingProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PAGING_PROJECTION_PARAM);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Keys.class)) {
param = buildPathKeysParam(paramAnnotations, paramType, com.linkedin.restli.server.annotations.Keys.class);
} else if (paramAnnotations.contains(PathKeysParam.class)) {
param = buildPathKeysParam(paramAnnotations, paramType, PathKeysParam.class);
} else if (paramAnnotations.contains(PathKeyParam.class)) {
param = buildPathKeyParam(model, paramAnnotations, paramType, PathKeyParam.class);
} else if (paramAnnotations.contains(HeaderParam.class)) {
param = buildHeaderParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(ResourceContextParam.class)) {
param = buildResourceContextParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(ValidatorParam.class)) {
param = buildValidatorParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(RestLiAttachmentsParam.class)) {
param = buildRestLiAttachmentsParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(UnstructuredDataWriterParam.class)) {
param = buildUnstructuredDataWriterParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(UnstructuredDataReactiveReaderParam.class)) {
param = buildUnstructuredDataReactiveReader(paramAnnotations, paramType);
} else {
throw new ResourceConfigException(buildMethodMessage(method) + " must annotate each parameter with @QueryParam, @ActionParam, @AssocKeyParam, @PagingContextParam, " + "@ProjectionParam, @MetadataProjectionParam, @PagingProjectionParam, @PathKeysParam, @PathKeyParam, " + "@HeaderParam, @CallbackParam, @ResourceContext, @ParSeqContextParam, @ValidatorParam, " + "@RestLiAttachmentsParam, @UnstructuredDataWriterParam, @UnstructuredDataReactiveReaderParam, " + "or @ValidateParam");
}
}
if (param != null) {
validateParameter(method, methodType, paramNames, paramAnnotations, param, paramType);
queryParameters.add(param);
}
}
return queryParameters;
}
Aggregations