Search in sources :

Example 1 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class GroupMembershipsResource2 method batchGet.

/**
  * @see GroupMembershipsResource2#batchGet(Set)
  */
@Override
public BatchResult<CompoundKey, GroupMembership> batchGet(Set<CompoundKey> ids) {
    Map<CompoundKey, GroupMembership> result = new HashMap<CompoundKey, GroupMembership>(ids.size());
    Map<CompoundKey, RestLiServiceException> errors = new HashMap<CompoundKey, RestLiServiceException>();
    Iterator<CompoundKey> iterator = ids.iterator();
    while (iterator.hasNext()) {
        CompoundKey key = iterator.next();
        GroupMembership membership = _app.getMembershipMgr().get(key);
        if (membership != null) {
            result.put(key, membership);
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<CompoundKey, GroupMembership>(result, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) BatchResult(com.linkedin.restli.server.BatchResult)

Example 2 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class GroupMembershipsResource3 method batchGet.

/**
  * @see GroupMembershipsResource2#batchGet(Set)
  */
@Override
public BatchResult<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> batchGet(Set<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>> ids) {
    Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> result = new HashMap<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership>(ids.size());
    Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, RestLiServiceException> errors = new HashMap<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, RestLiServiceException>();
    Iterator<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>> iterator = ids.iterator();
    while (iterator.hasNext()) {
        ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> key = iterator.next();
        ComplexKeyGroupMembership membership = fromGroupMembership(_app.getMembershipMgr().get(complexKeyToCompoundKey(key)));
        if (membership != null) {
            result.put(key, membership);
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership>(result, errors);
}
Also used : GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) BatchResult(com.linkedin.restli.server.BatchResult)

Example 3 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class BatchGetResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    @SuppressWarnings({ "unchecked" }) final Map<Object, RecordTemplate> /** constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchGet(java.util.Set)} */
    entities = (Map<Object, RecordTemplate>) result;
    Map<Object, HttpStatus> statuses = Collections.emptyMap();
    Map<Object, RestLiServiceException> serviceErrors = Collections.emptyMap();
    if (result instanceof BatchResult) {
        @SuppressWarnings({ "unchecked" }) final BatchResult<Object, RecordTemplate> /** constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchGet(java.util.Set)} */
        batchResult = (BatchResult<Object, RecordTemplate>) result;
        statuses = batchResult.getStatuses();
        serviceErrors = batchResult.getErrors();
    }
    try {
        if (statuses.containsKey(null)) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of a Map returned by the resource method: " + routingResult.getResourceMethod());
        }
    } catch (NullPointerException e) {
    // Some map implementations will throw an NPE if they do not support null keys.
    // In this case it is OK to swallow this exception and proceed.
    }
    Map<Object, BatchResponseEntry> batchResult = new HashMap<Object, BatchResponseEntry>(entities.size() + serviceErrors.size());
    for (Map.Entry<Object, RecordTemplate> entity : entities.entrySet()) {
        if (entity.getKey() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of a Map returned by the resource method: " + routingResult.getResourceMethod());
        }
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entity.getKey(), routingResult);
        final DataMap projectedData = RestUtils.projectFields(entity.getValue().data(), routingResult.getContext().getProjectionMode(), routingResult.getContext().getProjectionMask());
        AnyRecord anyRecord = new AnyRecord(projectedData);
        batchResult.put(finalKey, new BatchResponseEntry(statuses.get(entity.getKey()), anyRecord));
    }
    for (Map.Entry<Object, RestLiServiceException> entity : serviceErrors.entrySet()) {
        if (entity.getKey() == null || entity.getValue() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of a Map returned by the resource method: " + routingResult.getResourceMethod());
        }
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entity.getKey(), routingResult);
        batchResult.put(finalKey, new BatchResponseEntry(statuses.get(entity.getKey()), entity.getValue()));
    }
    final Map<Object, RestLiServiceException> contextErrors = ((ServerResourceContext) routingResult.getContext()).getBatchKeyErrors();
    for (Map.Entry<Object, RestLiServiceException> entry : contextErrors.entrySet()) {
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResult.put(finalKey, new BatchResponseEntry(statuses.get(entry.getKey()), entry.getValue()));
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
    responseData.setResponseEnvelope(new BatchGetResponseEnvelope(batchResult, responseData));
    return responseData;
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HttpStatus(com.linkedin.restli.common.HttpStatus) HashMap(java.util.HashMap) BatchResult(com.linkedin.restli.server.BatchResult) DataMap(com.linkedin.data.DataMap) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 4 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class AutomaticValidationDemoResource method batchGet.

@RestMethod.BatchGet
public BatchResult<Integer, ValidationDemo> batchGet(Set<Integer> ids) {
    Map<Integer, ValidationDemo> resultMap = new HashMap<Integer, ValidationDemo>();
    Map<Integer, RestLiServiceException> errorMap = new HashMap<Integer, RestLiServiceException>();
    // Generate entities that are missing a required field
    for (Integer id : ids) {
        if (id == 0) {
            errorMap.put(id, new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
        } else if (id == 1) {
            ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
            union.setMyRecord(new myRecord().setFoo1(100).setFoo2(200));
            resultMap.put(id, new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union));
        } else {
            ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
            union.setMyRecord(new myRecord());
            ValidationDemo validationDemo = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union);
            resultMap.put(id, validationDemo);
        }
    }
    ;
    return new BatchResult<Integer, ValidationDemo>(resultMap, errorMap);
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) BatchResult(com.linkedin.restli.server.BatchResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 5 with BatchResult

use of com.linkedin.restli.server.BatchResult in project rest.li by linkedin.

the class ComplexKeysDataProvider method batchGet.

public BatchResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGet(Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> keys) {
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> data = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException> errors = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException>();
    for (ComplexResourceKey<TwoPartKey, TwoPartKey> key : keys) {
        String stringKey = keyToString(key.getKey());
        if (_db.containsKey(stringKey)) {
            data.put(key, _db.get(stringKey));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>(data, errors);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) BatchResult(com.linkedin.restli.server.BatchResult)

Aggregations

BatchResult (com.linkedin.restli.server.BatchResult)13 HashMap (java.util.HashMap)12 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 Foo (com.linkedin.pegasus.generator.examples.Foo)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 DataMap (com.linkedin.data.DataMap)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3 HttpStatus (com.linkedin.restli.common.HttpStatus)3 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 DataProvider (org.testng.annotations.DataProvider)3 BatchResponse (com.linkedin.restli.common.BatchResponse)2 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)2 ErrorResponse (com.linkedin.restli.common.ErrorResponse)2 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)2 Message (com.linkedin.restli.examples.greetings.api.Message)2 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)2 LinkedHashMap (java.util.LinkedHashMap)2