Search in sources :

Example 1 with BatchFinderEntry

use of com.linkedin.restli.internal.server.response.BatchFinderResponseEnvelope.BatchFinderEntry 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 2 with BatchFinderEntry

use of com.linkedin.restli.internal.server.response.BatchFinderResponseEnvelope.BatchFinderEntry in project rest.li by linkedin.

the class BatchFinderResponseBuilder method buildResponse.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public RestLiResponse buildResponse(RoutingResult routingResult, RestLiResponseData<BatchFinderResponseEnvelope> responseData) {
    BatchFinderResponseEnvelope response = responseData.getResponseEnvelope();
    DataMap dataMap = new DataMap();
    DataList elementsMap = new DataList();
    for (BatchFinderEntry entry : response.getItems()) {
        CheckedUtil.addWithoutChecking(elementsMap, entry.toResponse(_errorResponseBuilder));
    }
    CheckedUtil.putWithoutChecking(dataMap, CollectionResponse.ELEMENTS, elementsMap);
    BatchCollectionResponse<?> collectionResponse = new BatchCollectionResponse<>(dataMap, null);
    RestLiResponse.Builder builder = new RestLiResponse.Builder();
    return builder.entity(collectionResponse).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : DataList(com.linkedin.data.DataList) BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) BatchFinderEntry(com.linkedin.restli.internal.server.response.BatchFinderResponseEnvelope.BatchFinderEntry) DataMap(com.linkedin.data.DataMap)

Aggregations

DataList (com.linkedin.data.DataList)2 BatchFinderEntry (com.linkedin.restli.internal.server.response.BatchFinderResponseEnvelope.BatchFinderEntry)2 DataMap (com.linkedin.data.DataMap)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)1 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)1 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)1 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)1 BatchFinderResult (com.linkedin.restli.server.BatchFinderResult)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 ArrayList (java.util.ArrayList)1