use of com.linkedin.restli.server.annotations.MaxBatchSize in project rest.li by linkedin.
the class BatchGreetingResource method searchGreetings.
@BatchFinder(value = "searchGreetings", batchParam = "criteria")
@MaxBatchSize(value = 2, validate = true)
public BatchFinderResult<GreetingCriteria, Greeting, Empty> searchGreetings(@QueryParam("criteria") GreetingCriteria[] criteria) {
BatchFinderResult<GreetingCriteria, Greeting, Empty> batchFinderResult = new BatchFinderResult<>();
for (GreetingCriteria currentCriteria : criteria) {
if (currentCriteria.getId() == 1l) {
CollectionResult<Greeting, Empty> c1 = new CollectionResult<>(Arrays.asList(GREETING_ONE), 1);
batchFinderResult.putResult(currentCriteria, c1);
} else if (currentCriteria.getId() == 2l) {
CollectionResult<Greeting, Empty> c2 = new CollectionResult<>(Arrays.asList(GREETING_TWO), 1);
batchFinderResult.putResult(currentCriteria, c2);
}
}
return batchFinderResult;
}
use of com.linkedin.restli.server.annotations.MaxBatchSize in project rest.li by linkedin.
the class BatchGreetingResource method batchGet.
@RestMethod.BatchGet
@MaxBatchSize(value = 2, validate = true)
public Map<Long, Greeting> batchGet(Set<Long> ids) {
Map<Long, Greeting> batch = new HashMap<>();
Map<Long, RestLiServiceException> errors = new HashMap<>();
for (Long id : ids) {
Greeting greeting = DB.get(id);
if (greeting != null) {
batch.put(id, greeting);
} else {
errors.put(id, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
}
}
return new BatchResult<>(batch, errors);
}
use of com.linkedin.restli.server.annotations.MaxBatchSize 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);
}
Aggregations