Search in sources :

Example 26 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method checkAnnotation.

/**
   * Check if resourceClass is annotated with the expected annotation of its template class,
   * expressed in the {@link RestLiTemplate} annotation.
   */
private static void checkAnnotation(final Class<?> resourceClass) {
    Class templateClass = resourceClass;
    while (templateClass != Object.class) {
        templateClass = templateClass.getSuperclass();
        final RestLiTemplate templateAnnotation = (RestLiTemplate) templateClass.getAnnotation(RestLiTemplate.class);
        if (templateAnnotation != null) {
            final Class<? extends Annotation> currentExpect = templateAnnotation.expectedAnnotation();
            if (currentExpect == RestLiCollection.class || currentExpect == RestLiAssociation.class || currentExpect == RestLiSimpleResource.class) {
                if (resourceClass.getAnnotation(currentExpect) == null) {
                    throw new ResourceConfigException(resourceClass.getName() + " is not annotated with " + currentExpect.getName() + ", expected by " + templateClass.getName());
                } else {
                    return;
                }
            } else {
                throw new ResourceConfigException("Unknown expected annotation " + currentExpect.getName() + " from " + templateClass.getName());
            }
        }
    }
}
Also used : RestLiTemplate(com.linkedin.restli.server.annotations.RestLiTemplate) RestLiSimpleResource(com.linkedin.restli.server.annotations.RestLiSimpleResource) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 27 with ResourceConfigException

use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.

the class RestLiAnnotationReader method getParameters.

private static List<Parameter<?>> getParameters(final ResourceModel model, final Method method, final ResourceMethod methodType) {
    Set<String> paramNames = new HashSet<String>();
    List<Parameter<?>> queryParameters = new ArrayList<Parameter<?>>();
    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(AssocKey.class)) {
                param = buildAssocKeyParam(model, method, paramAnnotations, paramType, AssocKey.class);
            } else if (paramAnnotations.contains(AssocKeyParam.class)) {
                param = buildAssocKeyParam(model, method, paramAnnotations, paramType, AssocKeyParam.class);
            } else if (paramAnnotations.contains(Context.class)) {
                param = buildPagingContextParam(paramAnnotations, paramType, 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(ParSeqContext.class)) {
                param = buildParSeqContextParam(method, methodType, idx, paramType, paramAnnotations, ParSeqContext.class);
            } else if (paramAnnotations.contains(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(Keys.class)) {
                param = buildPathKeysParam(paramAnnotations, paramType, 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 {
                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, or @ValidateParam");
            }
        }
        if (param != null) {
            validateParameter(method, methodType, paramNames, paramAnnotations, param, paramType);
            queryParameters.add(param);
        }
    }
    return queryParameters;
}
Also used : PagingContextParam(com.linkedin.restli.server.annotations.PagingContextParam) ArrayList(java.util.ArrayList) Projection(com.linkedin.restli.server.annotations.Projection) AssocKey(com.linkedin.restli.server.annotations.AssocKey) ParSeqContext(com.linkedin.restli.server.annotations.ParSeqContext) PathKeyParam(com.linkedin.restli.server.annotations.PathKeyParam) RestLiAttachmentsParam(com.linkedin.restli.server.annotations.RestLiAttachmentsParam) MetadataProjectionParam(com.linkedin.restli.server.annotations.MetadataProjectionParam) HashSet(java.util.HashSet) PagingContext(com.linkedin.restli.server.PagingContext) ResourceContext(com.linkedin.restli.server.ResourceContext) ParSeqContext(com.linkedin.restli.server.annotations.ParSeqContext) Context(com.linkedin.restli.server.annotations.Context) ActionParam(com.linkedin.restli.server.annotations.ActionParam) PathKeysParam(com.linkedin.restli.server.annotations.PathKeysParam) PathKeys(com.linkedin.restli.server.PathKeys) AlternativeKeys(com.linkedin.restli.server.annotations.AlternativeKeys) Keys(com.linkedin.restli.server.annotations.Keys) ParSeqContextParam(com.linkedin.restli.server.annotations.ParSeqContextParam) AssocKeyParam(com.linkedin.restli.server.annotations.AssocKeyParam) ResourceContextParam(com.linkedin.restli.server.annotations.ResourceContextParam) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)27 DataMap (com.linkedin.data.DataMap)7 Optional (com.linkedin.restli.server.annotations.Optional)7 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)6 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)3 RecordTemplate (com.linkedin.data.template.RecordTemplate)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3 PagingContext (com.linkedin.restli.server.PagingContext)3 AlternativeKey (com.linkedin.restli.server.annotations.AlternativeKey)3 AssocKey (com.linkedin.restli.server.annotations.AssocKey)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)2 InterfaceType (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType)2 CollectionResult (com.linkedin.restli.server.CollectionResult)2 Key (com.linkedin.restli.server.Key)2 PathKeys (com.linkedin.restli.server.PathKeys)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 ActionParam (com.linkedin.restli.server.annotations.ActionParam)2