Search in sources :

Example 1 with BatchFinderResult

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;
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) ArrayList(java.util.ArrayList) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) DataMap(com.linkedin.data.DataMap)

Example 2 with 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;
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) NoMetadata(com.linkedin.restli.server.NoMetadata) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ArrayList(java.util.ArrayList) Photo(com.linkedin.restli.example.Photo) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) PhotoCriteria(com.linkedin.restli.example.PhotoCriteria) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder)

Example 3 with 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);
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) BatchFinderEntry(com.linkedin.restli.internal.server.response.BatchFinderResponseEnvelope.BatchFinderEntry) ArrayList(java.util.ArrayList) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) DataList(com.linkedin.data.DataList) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 4 with BatchFinderResult

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;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult)

Example 5 with BatchFinderResult

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;
}
Also used : ArrayList(java.util.ArrayList) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) CollectionResult(com.linkedin.restli.server.CollectionResult) Empty(com.linkedin.restli.examples.greetings.api.Empty) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder)

Aggregations

BatchFinderResult (com.linkedin.restli.server.BatchFinderResult)9 CollectionResult (com.linkedin.restli.server.CollectionResult)7 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 BatchFinder (com.linkedin.restli.server.annotations.BatchFinder)6 Empty (com.linkedin.restli.examples.greetings.api.Empty)5 ArrayList (java.util.ArrayList)5 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)2 GreetingCriteria (com.linkedin.restli.examples.greetings.api.GreetingCriteria)2 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)2 ValidationDemoCriteria (com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria)2 DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 Foo (com.linkedin.pegasus.generator.examples.Foo)1 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)1 Photo (com.linkedin.restli.example.Photo)1 PhotoCriteria (com.linkedin.restli.example.PhotoCriteria)1 Message (com.linkedin.restli.examples.greetings.api.Message)1 MessageCriteria (com.linkedin.restli.examples.greetings.api.MessageCriteria)1