Search in sources :

Example 1 with MaxBatchSizeSchema

use of com.linkedin.restli.restspec.MaxBatchSizeSchema in project rest.li by linkedin.

the class RestLiMethodInvoker method validateMaxBatchSize.

/**
 * Method is used to validate if the request's batch size is under
 * the allowed max batch size which is defined in the server resource.
 *
 * @throws RestLiServiceException if request's batch size is larger than the allowed max batch size.
 */
private void validateMaxBatchSize(RestLiRequestData requestData, ResourceMethodDescriptor method, ServerResourceContext resourceContext) throws RestLiServiceException {
    MaxBatchSizeSchema maxBatchSizeAnnotation = method.getMaxBatchSize();
    if (maxBatchSizeAnnotation == null || !maxBatchSizeAnnotation.isValidate()) {
        return;
    }
    int requestBatchSize = getRequestBatchSize(requestData, method, resourceContext);
    int maxBatchSize = maxBatchSizeAnnotation.getValue();
    if (requestBatchSize > maxBatchSizeAnnotation.getValue()) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, String.format("The request batch size: %s is larger than the allowed max batch size: %s for method: %s", requestBatchSize, maxBatchSize, method.getMethodName()));
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema)

Example 2 with MaxBatchSizeSchema

use of com.linkedin.restli.restspec.MaxBatchSizeSchema in project rest.li by linkedin.

the class RestLiAnnotationReader method addMaxBatchSize.

/**
 * Reads annotations on a given method in order to get max batch size, which are then added to
 * a given resource method descriptor.
 *
 * @param resourceMethodDescriptor resource method descriptor to add max batch size to
 * @param method method annotated with max batch size
 * @param resourceMethod resource method which is used to validate the method with max batch size annotation
 *                       is a supported method.
 */
private static void addMaxBatchSize(ResourceMethodDescriptor resourceMethodDescriptor, Method method, ResourceMethod resourceMethod) {
    final MaxBatchSize maxBatchSizeAnnotation = method.getAnnotation(MaxBatchSize.class);
    if (maxBatchSizeAnnotation == null) {
        return;
    }
    // Only batch methods are allowed to use MaxBatchSize annotation.
    if (!BATCH_METHODS.contains(resourceMethod)) {
        throw new ResourceConfigException(String.format("The resource method: %s cannot specify MaxBatchSize.", resourceMethod.toString()));
    }
    int maxBatchSizeValue = maxBatchSizeAnnotation.value();
    // Max batch size value should always be greater than 0
    if (maxBatchSizeValue <= 0) {
        throw new ResourceConfigException(String.format("The resource method: %s max batch size value is %s, " + "it should be greater than 0.", resourceMethod.toString(), maxBatchSizeValue));
    }
    MaxBatchSizeSchema maxBatchSizeSchema = new MaxBatchSizeSchema();
    maxBatchSizeSchema.setValue(maxBatchSizeAnnotation.value());
    maxBatchSizeSchema.setValidate(maxBatchSizeAnnotation.validate());
    resourceMethodDescriptor.setMaxBatchSize(maxBatchSizeSchema);
}
Also used : MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema) MaxBatchSize(com.linkedin.restli.server.annotations.MaxBatchSize) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 3 with MaxBatchSizeSchema

use of com.linkedin.restli.restspec.MaxBatchSizeSchema in project rest.li by linkedin.

the class ResourceModelEncoder method createRestMethods.

private RestMethodSchemaArray createRestMethods(final ResourceModel resourceModel) {
    RestMethodSchemaArray restMethods = new RestMethodSchemaArray();
    ResourceMethod[] crudMethods = { ResourceMethod.CREATE, ResourceMethod.GET, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.BATCH_CREATE, ResourceMethod.BATCH_GET, ResourceMethod.BATCH_UPDATE, ResourceMethod.BATCH_PARTIAL_UPDATE, ResourceMethod.BATCH_DELETE, ResourceMethod.GET_ALL };
    for (ResourceMethod method : crudMethods) {
        ResourceMethodDescriptor descriptor = resourceModel.findMethod(method);
        if (descriptor == null) {
            continue;
        }
        RestMethodSchema restMethod = new RestMethodSchema();
        restMethod.setMethod(method.toString());
        String doc = _docsProvider.getMethodDoc(descriptor.getMethod());
        if (doc != null) {
            restMethod.setDoc(doc);
        }
        ParameterSchemaArray parameters = createParameters(descriptor);
        if (parameters.size() > 0) {
            restMethod.setParameters(parameters);
        }
        final DataMap customAnnotation = descriptor.getCustomAnnotationData();
        String deprecatedDoc = _docsProvider.getMethodDeprecatedTag(descriptor.getMethod());
        if (deprecatedDoc != null) {
            customAnnotation.put(DEPRECATED_ANNOTATION_NAME, deprecateDocToAnnotationMap(deprecatedDoc));
        }
        if (!customAnnotation.isEmpty()) {
            restMethod.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
        }
        if (method == ResourceMethod.GET_ALL) {
            if (descriptor.getCollectionCustomMetadataType() != null) {
                restMethod.setMetadata(createMetadataSchema(descriptor));
            }
            if (descriptor.isPagingSupported()) {
                restMethod.setPagingSupported(true);
            }
        }
        MaxBatchSizeSchema maxBatchSize = descriptor.getMaxBatchSize();
        if (maxBatchSize != null) {
            restMethod.setMaxBatchSize(maxBatchSize);
        }
        appendServiceErrors(restMethod, descriptor.getServiceErrors());
        appendSuccessStatuses(restMethod, descriptor.getSuccessStatuses());
        restMethods.add(restMethod);
    }
    return restMethods;
}
Also used : RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap)

Example 4 with MaxBatchSizeSchema

use of com.linkedin.restli.restspec.MaxBatchSizeSchema in project rest.li by linkedin.

the class ResourceModelEncoder method createBatchFinderSchema.

private BatchFinderSchema createBatchFinderSchema(ResourceMethodDescriptor resourceMethodDescriptor) {
    BatchFinderSchema batchFinder = new BatchFinderSchema();
    batchFinder.setName(resourceMethodDescriptor.getBatchFinderName());
    String doc = _docsProvider.getMethodDoc(resourceMethodDescriptor.getMethod());
    if (doc != null) {
        batchFinder.setDoc(doc);
    }
    ParameterSchemaArray parameters = createParameters(resourceMethodDescriptor);
    if (parameters.size() > 0) {
        batchFinder.setParameters(parameters);
    }
    StringArray assocKeys = createAssocKeyParameters(resourceMethodDescriptor);
    if (assocKeys.size() > 0) {
        batchFinder.setAssocKeys(assocKeys);
    }
    if (resourceMethodDescriptor.getCollectionCustomMetadataType() != null) {
        batchFinder.setMetadata(createMetadataSchema(resourceMethodDescriptor));
    }
    final DataMap customAnnotation = resourceMethodDescriptor.getCustomAnnotationData();
    String deprecatedDoc = _docsProvider.getMethodDeprecatedTag(resourceMethodDescriptor.getMethod());
    if (deprecatedDoc != null) {
        customAnnotation.put(DEPRECATED_ANNOTATION_NAME, deprecateDocToAnnotationMap(deprecatedDoc));
    }
    if (!customAnnotation.isEmpty()) {
        batchFinder.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
    }
    if (resourceMethodDescriptor.isPagingSupported()) {
        batchFinder.setPagingSupported(true);
    }
    MaxBatchSizeSchema maxBatchSize = resourceMethodDescriptor.getMaxBatchSize();
    if (maxBatchSize != null) {
        batchFinder.setMaxBatchSize(maxBatchSize);
    }
    appendServiceErrors(batchFinder, resourceMethodDescriptor.getServiceErrors());
    appendSuccessStatuses(batchFinder, resourceMethodDescriptor.getSuccessStatuses());
    BatchFinder batchFinderAnnotation = resourceMethodDescriptor.getMethod().getAnnotation(BatchFinder.class);
    batchFinder.setBatchParam(batchFinderAnnotation.batchParam());
    return batchFinder;
}
Also used : StringArray(com.linkedin.data.template.StringArray) MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder) BatchFinderSchema(com.linkedin.restli.restspec.BatchFinderSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) DataMap(com.linkedin.data.DataMap)

Aggregations

MaxBatchSizeSchema (com.linkedin.restli.restspec.MaxBatchSizeSchema)4 DataMap (com.linkedin.data.DataMap)2 CustomAnnotationContentSchemaMap (com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap)2 ParameterSchemaArray (com.linkedin.restli.restspec.ParameterSchemaArray)2 StringArray (com.linkedin.data.template.StringArray)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 BatchFinderSchema (com.linkedin.restli.restspec.BatchFinderSchema)1 RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)1 RestMethodSchemaArray (com.linkedin.restli.restspec.RestMethodSchemaArray)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 BatchFinder (com.linkedin.restli.server.annotations.BatchFinder)1 MaxBatchSize (com.linkedin.restli.server.annotations.MaxBatchSize)1