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