use of com.linkedin.restli.server.BatchFinderResult in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method buildBatchFinderResult.
@SuppressWarnings({ "unchecked", "rawtypes" })
private BatchFinderResult<RecordTemplatePlaceholder, RecordTemplatePlaceholder, RecordTemplatePlaceholder> buildBatchFinderResult(RecordDataSchema batchFinderMetadataSchema, RecordTemplate batchFinderCriteria) {
final List<RecordTemplatePlaceholder> results = new ArrayList<>();
results.add(generateEntity());
results.add(generateEntity());
BatchFinderResult batchFinderResult = new BatchFinderResult();
if (batchFinderMetadataSchema != null) {
DataMap metadataDataMap = (DataMap) _dataGenerator.buildData("metadata", batchFinderMetadataSchema);
RecordTemplatePlaceholder metadata = new RecordTemplatePlaceholder(metadataDataMap, batchFinderMetadataSchema);
CollectionResult cr = new CollectionResult<>(results, results.size(), metadata);
batchFinderResult.putResult(batchFinderCriteria, cr);
} else {
CollectionResult cr = new CollectionResult<RecordTemplatePlaceholder, RecordTemplatePlaceholder>(results, results.size());
batchFinderResult.putResult(batchFinderCriteria, cr);
}
return batchFinderResult;
}
use of com.linkedin.restli.server.BatchFinderResult in project rest.li by linkedin.
the class PhotoResource method searchPhotos.
@BatchFinder(value = "searchPhotos", batchParam = "criteria")
public BatchFinderResult<PhotoCriteria, Photo, NoMetadata> searchPhotos(@PagingContextParam PagingContext pagingContext, @QueryParam("criteria") PhotoCriteria[] criteria, @QueryParam("exif") @Optional EXIF exif) {
BatchFinderResult<PhotoCriteria, Photo, NoMetadata> batchFinderResult = new BatchFinderResult<>();
for (PhotoCriteria currentCriteria : criteria) {
if (currentCriteria.getTitle() != null) {
// on success
final List<Photo> photos = new ArrayList<>();
int index = 0;
final int begin = pagingContext.getStart();
final int end = begin + pagingContext.getCount();
final Collection<Photo> dbPhotos = _db.getData().values();
for (Photo p : dbPhotos) {
if (index == end) {
break;
} else if (index >= begin) {
if (p.getTitle().equalsIgnoreCase(currentCriteria.getTitle())) {
if (currentCriteria.getFormat() == null || currentCriteria.getFormat() == p.getFormat()) {
photos.add(p);
}
}
}
index++;
}
CollectionResult<Photo, NoMetadata> cr = new CollectionResult<>(photos, photos.size());
batchFinderResult.putResult(currentCriteria, cr);
} else {
// on error: to construct error response for test
batchFinderResult.putError(currentCriteria, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Failed to find Photo!"));
}
}
return batchFinderResult;
}
use of com.linkedin.restli.server.BatchFinderResult in project rest.li by linkedin.
the class BatchFinderResponseBuilder method buildRestLiResponseData.
@Override
@SuppressWarnings("unchecked")
public RestLiResponseData<BatchFinderResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object object, Map<String, String> headers, List<HttpCookie> cookies) {
BatchFinderResult<RecordTemplate, RecordTemplate, RecordTemplate> result = (BatchFinderResult<RecordTemplate, RecordTemplate, RecordTemplate>) object;
DataList criteriaParams = getCriteriaParameters(routingResult);
List<BatchFinderEntry> collectionResponse = new ArrayList<>(criteriaParams.size());
final ResourceContextImpl resourceContext = (ResourceContextImpl) routingResult.getContext();
TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
for (Object criteriaParam : criteriaParams.values()) {
RecordTemplate criteria = new AnyRecord((DataMap) criteriaParam);
BatchFinderEntry entry;
if (result.getResults().containsKey(criteria)) {
CollectionResult<RecordTemplate, RecordTemplate> cr = result.getResult(criteria);
// Process elements
List<AnyRecord> elements = buildElements(cr, resourceContext);
// Process paging
final CollectionMetadata projectedPaging = buildPaginationMetaData(routingResult, criteria, resourceContext, request, cr);
// Process metadata
final AnyRecord projectedCustomMetadata = buildMetaData(cr, resourceContext);
entry = new BatchFinderEntry(elements, projectedPaging, projectedCustomMetadata);
} else if (result.getErrors().containsKey(criteria)) {
entry = new BatchFinderEntry(result.getErrors().get(criteria));
} else {
entry = new BatchFinderEntry(new RestLiServiceException(S_404_NOT_FOUND, "The server didn't find a representation for this criteria"));
}
collectionResponse.add(entry);
}
TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
return new RestLiResponseDataImpl<>(new BatchFinderResponseEnvelope(HttpStatus.S_200_OK, collectionResponse), headers, cookies);
}
use of com.linkedin.restli.server.BatchFinderResult in project rest.li by linkedin.
the class TestBatchFinderResponseBuilder method generateResultsWithErrors.
@SuppressWarnings("deprecation")
private static BatchFinderResult<Foo, Foo, Foo> generateResultsWithErrors(List<Criteria> criteria) {
BatchFinderResult<Foo, Foo, Foo> results = new BatchFinderResult<>();
for (int i = 0; i < criteria.size(); i++) {
if (i % 2 == 0) {
criteria.get(i).onError = true;
RestLiServiceException ex = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
ex.setServiceErrorCode(criteria.get(i).criteria.getIntField());
results.putError(criteria.get(i).criteria, ex);
} else {
List<Foo> items = generateTestList(criteria.get(i).criteria);
results.putResult(criteria.get(i).criteria, new CollectionResult<>(items));
}
}
return results;
}
use of com.linkedin.restli.server.BatchFinderResult in project rest.li by linkedin.
the class AutomaticValidationDemoResource method searchValidationDemos.
@BatchFinder(value = "searchValidationDemos", batchParam = "criteria")
public BatchFinderResult<ValidationDemoCriteria, ValidationDemo, Empty> searchValidationDemos(@PagingContextParam PagingContext context, @QueryParam("criteria") ValidationDemoCriteria[] criteria) {
BatchFinderResult<ValidationDemoCriteria, ValidationDemo, Empty> batchFinderResult = new BatchFinderResult<>();
for (ValidationDemoCriteria currentCriteria : criteria) {
List<ValidationDemo> validationDemos = new ArrayList<>();
if (currentCriteria.getIntA() == 1111) {
// Generate entities that are missing stringB fields
for (int i = 0; i < 3; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("valueA").setIntA(currentCriteria.getIntA()).setUnionFieldWithInlineRecord(union));
}
} else if (currentCriteria.getIntA() == 2222) {
// Generate entities that their stringA field has a value over the length limitation
for (int i = 0; i < 3; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("longLengthValueA").setIntA(currentCriteria.getIntA()).setStringB("valueB").setUnionFieldWithInlineRecord(union));
}
} else if (currentCriteria.getIntA() == 3333) {
// the stringA field has a value over the length limitation and miss stringB fields
for (int i = 0; i < 3; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("longLengthValueA").setIntA(currentCriteria.getIntA()).setUnionFieldWithInlineRecord(union));
}
} else if (currentCriteria.getIntA() == 4444) {
// entities without errors
for (int i = 0; i < 3; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("valueA").setIntA(currentCriteria.getIntA()).setStringB("valueB").setUnionFieldWithInlineRecord(union));
}
} else {
// on errorResponse
batchFinderResult.putError(currentCriteria, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Fail to find Validation Demo!"));
continue;
}
CollectionResult<ValidationDemo, Empty> cr = new CollectionResult<>(validationDemos, validationDemos.size());
batchFinderResult.putResult(currentCriteria, cr);
}
return batchFinderResult;
}
Aggregations