Search in sources :

Example 1 with BatchUpdateResult

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

the class BatchUpdateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    @SuppressWarnings({ "unchecked" }) final BatchUpdateResult<Object, ?> /** constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchUpdate(java.util.Map)} */
    updateResult = (BatchUpdateResult<Object, ?>) result;
    final Map<Object, UpdateResponse> results = updateResult.getResults();
    //Verify the map is not null. If so, this is a developer error.
    if (results == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    final Map<Object, RestLiServiceException> serviceErrors = updateResult.getErrors();
    if (serviceErrors == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null errors Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    Map<Object, BatchResponseEntry> batchResponseMap = new HashMap<Object, BatchResponseEntry>();
    for (Map.Entry<Object, UpdateResponse> entry : results.entrySet()) {
        if (entry.getKey() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        if (!serviceErrors.containsKey(entry.getKey())) {
            Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
            batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), new UpdateStatus()));
        }
    }
    for (Map.Entry<Object, RestLiServiceException> entry : serviceErrors.entrySet()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key or value inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    for (Map.Entry<Object, RestLiServiceException> entry : ((ServerResourceContext) routingResult.getContext()).getBatchKeyErrors().entrySet()) {
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
    responseData.setResponseEnvelope(new BatchUpdateResponseEnvelope(batchResponseMap, responseData));
    return responseData;
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) HashMap(java.util.HashMap) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 2 with BatchUpdateResult

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

the class ComplexKeysDataProvider method batchUpdate.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchUpdate(BatchPatchRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> patches) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<>();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>> patch : patches.getData().entrySet()) {
        try {
            this.partialUpdate(patch.getKey(), patch.getValue());
            results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
        } catch (DataProcessingException e) {
            results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_400_BAD_REQUEST));
        }
    }
    return new BatchUpdateResult<>(results);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) HashMap(java.util.HashMap) Map(java.util.Map) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Example 3 with BatchUpdateResult

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

the class ComplexKeysDataProvider method batchDelete.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchDelete(BatchDeleteRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> ids) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<>();
    for (ComplexResourceKey<TwoPartKey, TwoPartKey> id : ids.getKeys()) {
        _db.remove(keyToString(id.getKey()));
        results.put(id, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }
    return new BatchUpdateResult<>(results);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 4 with BatchUpdateResult

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

the class AutomaticValidationDemoResource method batchUpdate.

@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates) {
    Map<Integer, UpdateResponse> results = new HashMap<>();
    Map<Integer, RestLiServiceException> errors = new HashMap<>();
    for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
        Integer key = entry.getKey();
        PatchRequest<ValidationDemo> patch = entry.getValue();
        results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }
    return new BatchUpdateResult<>(results, errors);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) HashMap(java.util.HashMap) Map(java.util.Map) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 5 with BatchUpdateResult

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

the class CustomTypesResource3 method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, Greeting> batchUpdate(BatchUpdateRequest<CompoundKey, Greeting> entities) {
    Set<CompoundKey> keys = entities.getData().keySet();
    Map<CompoundKey, UpdateResponse> responseMap = new HashMap<>();
    Map<CompoundKey, RestLiServiceException> errorMap = new HashMap<>();
    for (CompoundKey key : keys) {
        responseMap.put(key, new UpdateResponse(HttpStatus.S_201_CREATED));
    }
    return new BatchUpdateResult<>(responseMap);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap)

Aggregations

BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)29 UpdateResponse (com.linkedin.restli.server.UpdateResponse)21 HashMap (java.util.HashMap)21 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)11 Map (java.util.Map)11 CompoundKey (com.linkedin.restli.common.CompoundKey)10 Test (org.testng.annotations.Test)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)8 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)8 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)8 AfterTest (org.testng.annotations.AfterTest)8 BeforeTest (org.testng.annotations.BeforeTest)8 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)7 Callback (com.linkedin.common.callback.Callback)6 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)6 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)6 PatchRequest (com.linkedin.restli.common.PatchRequest)4 UpdateStatus (com.linkedin.restli.common.UpdateStatus)4 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)4 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)4