Search in sources :

Example 6 with UpdateStatus

use of com.linkedin.restli.common.UpdateStatus 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 7 with UpdateStatus

use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.

the class BatchUpdateResponseDecoder method wrapResponse.

@Override
public BatchKVResponse<K, UpdateStatus> wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    if (dataMap == null) {
        return null;
    }
    final DataMap mergedResults = new DataMap();
    final DataMap inputResults = dataMap.containsKey(BatchResponse.RESULTS) ? dataMap.getDataMap(BatchResponse.RESULTS) : new DataMap();
    final DataMap inputErrors = dataMap.containsKey(BatchResponse.ERRORS) ? dataMap.getDataMap(BatchResponse.ERRORS) : new DataMap();
    final Set<String> mergedKeys = new HashSet<String>(inputResults.keySet());
    mergedKeys.addAll(inputErrors.keySet());
    for (String key : mergedKeys) {
        // DataMap for UpdateStatus
        final DataMap updateData;
        // status field is mandatory
        if (inputResults.containsKey(key)) {
            updateData = inputResults.getDataMap(key);
        } else {
            updateData = new DataMap();
        }
        // DataMap for ErrorResponse
        final DataMap errorData = (DataMap) inputErrors.get(key);
        if (errorData != null) {
            // The status from ErrorResponse overwrites the one in UpdateResponse. However, results and
            // errors are not expected to have overlapping key. See BatchUpdateResponseBuilder.
            updateData.put("status", errorData.get("status"));
            updateData.put("error", errorData);
        }
        mergedResults.put(key, updateData);
    }
    final DataMap responseData = new DataMap();
    responseData.put(BatchKVResponse.RESULTS, mergedResults);
    responseData.put(BatchKVResponse.ERRORS, inputErrors);
    return new BatchKVResponse<K, UpdateStatus>(responseData, _keyType, new TypeSpec<UpdateStatus>(UpdateStatus.class), _keyParts, _complexKeyType, version);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap) HashSet(java.util.HashSet)

Example 8 with UpdateStatus

use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.

the class ScatterGatherBuilder method buildRequests.

@SuppressWarnings("deprecation")
public <K> KVScatterGatherResult<K, UpdateStatus> buildRequests(BatchUpdateRequest<K, T> request, RequestContext requestContext) throws ServiceUnavailableException {
    Set<Object> idObjects = request.getObjectIds();
    Collection<K> ids = new HashSet<K>(idObjects.size());
    for (Object o : idObjects) {
        @SuppressWarnings("unchecked") K k = (K) o;
        ids.add(k);
    }
    MapKeyResult<URI, K> mapKeyResult = mapKeys(request, ids);
    @SuppressWarnings("unchecked") TypeSpec<T> valueType = (TypeSpec<T>) request.getResourceProperties().getValueType();
    Map<URI, Map<K, T>> batches = keyMapToInput(mapKeyResult, request);
    Collection<KVRequestInfo<K, UpdateStatus>> scatterGatherRequests = new ArrayList<KVRequestInfo<K, UpdateStatus>>(batches.size());
    for (Map.Entry<URI, Map<K, T>> batch : batches.entrySet()) {
        BatchUpdateRequestBuilder<K, T> builder = new BatchUpdateRequestBuilder<K, T>(request.getBaseUriTemplate(), valueType.getType(), request.getResourceSpec(), request.getRequestOptions());
        builder.inputs(batch.getValue());
        for (Map.Entry<String, Object> param : request.getQueryParamsObjects().entrySet()) {
            if (!param.getKey().equals(RestConstants.QUERY_BATCH_IDS_PARAM)) {
                builder.setParam(param.getKey(), param.getValue());
            }
        }
        for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
            builder.setHeader(header.getKey(), header.getValue());
        }
        RequestContext context = requestContext.clone();
        KeyMapper.TargetHostHints.setRequestContextTargetHost(context, batch.getKey());
        scatterGatherRequests.add(new KVRequestInfo<K, UpdateStatus>(builder.build(), context));
    }
    return new KVScatterGatherResult<K, UpdateStatus>(scatterGatherRequests, mapKeyResult.getUnmappedKeys());
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) ArrayList(java.util.ArrayList) URI(java.net.URI) RequestContext(com.linkedin.r2.message.RequestContext) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Map(java.util.Map) TypeSpec(com.linkedin.restli.common.TypeSpec)

Example 9 with UpdateStatus

use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.

the class ScatterGatherBuilder method buildRequests.

@SuppressWarnings("deprecation")
public <K> KVScatterGatherResult<K, UpdateStatus> buildRequests(BatchDeleteRequest<K, T> request, RequestContext requestContext) throws ServiceUnavailableException {
    Set<Object> idObjects = request.getObjectIds();
    Collection<K> ids = new HashSet<K>(idObjects.size());
    for (Object o : idObjects) {
        @SuppressWarnings("unchecked") K k = (K) o;
        ids.add(k);
    }
    MapKeyResult<URI, K> mapKeyResult = mapKeys(request, ids);
    Map<URI, Collection<K>> batches = mapKeyResult.getMapResult();
    Collection<KVRequestInfo<K, UpdateStatus>> scatterGatherRequests = new ArrayList<KVRequestInfo<K, UpdateStatus>>(batches.size());
    for (Map.Entry<URI, Collection<K>> batch : batches.entrySet()) {
        TypeSpec<? extends RecordTemplate> value = request.getResourceProperties().getValueType();
        @SuppressWarnings("unchecked") Class<T> valueClass = (Class<T>) ((value == null) ? null : value.getType());
        BatchDeleteRequestBuilder<K, T> builder = new BatchDeleteRequestBuilder<K, T>(request.getBaseUriTemplate(), valueClass, request.getResourceSpec(), request.getRequestOptions());
        builder.ids(batch.getValue());
        for (Map.Entry<String, Object> param : request.getQueryParamsObjects().entrySet()) {
            if (!param.getKey().equals(RestConstants.QUERY_BATCH_IDS_PARAM)) {
                builder.setParam(param.getKey(), param.getValue());
            }
        }
        for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
            builder.setHeader(header.getKey(), header.getValue());
        }
        RequestContext context = requestContext.clone();
        KeyMapper.TargetHostHints.setRequestContextTargetHost(context, batch.getKey());
        BatchRequest<BatchKVResponse<K, UpdateStatus>> build = builder.build();
        scatterGatherRequests.add(new KVRequestInfo<K, UpdateStatus>(build, context));
    }
    return new KVScatterGatherResult<K, UpdateStatus>(scatterGatherRequests, mapKeyResult.getUnmappedKeys());
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) ArrayList(java.util.ArrayList) URI(java.net.URI) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) RequestContext(com.linkedin.r2.message.RequestContext) HashSet(java.util.HashSet) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with UpdateStatus

use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchDeleteMain.

private void testBatchDeleteMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, EmptyRecord> createRequestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    String messageText = "m1";
    Message message = new Message();
    message.setMessage(messageText);
    Request<EmptyRecord> createRequest = createRequestBuilder.input(message).build();
    ResponseFuture<EmptyRecord> createFuture = getClient().sendRequest(createRequest);
    Response<EmptyRecord> createResponse = createFuture.getResponse();
    Assert.assertEquals(createResponse.getStatus(), 201);
    String messageText2 = "m2";
    message.setMessage(messageText2);
    createRequest = createRequestBuilder.input(message).build();
    createFuture = getClient().sendRequest(createRequest);
    createResponse = createFuture.getResponse();
    Assert.assertEquals(createResponse.getStatus(), 201);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(messageText, messageText);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(messageText2, messageText2);
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>();
    ids.add(key1);
    ids.add(key2);
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.ids(ids).build();
    final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
    final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
        Assert.assertEquals(resp.getValue().getStatus().intValue(), 204);
    }
    Assert.assertNotNull(response.getResults().get(key1));
    Assert.assertNotNull(response.getResults().get(key2));
    Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
    BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
    Assert.assertEquals(batchGetResponse.getResults().size(), batchGetResponse.getErrors().size());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) EmptyRecord(com.linkedin.restli.common.EmptyRecord) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Aggregations

UpdateStatus (com.linkedin.restli.common.UpdateStatus)25 HashMap (java.util.HashMap)17 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)15 Test (org.testng.annotations.Test)11 DataMap (com.linkedin.data.DataMap)10 Map (java.util.Map)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 ArrayList (java.util.ArrayList)8 CompoundKey (com.linkedin.restli.common.CompoundKey)5 EntityResponse (com.linkedin.restli.common.EntityResponse)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 BatchResponse (com.linkedin.restli.common.BatchResponse)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4 HashSet (java.util.HashSet)4 RequestContext (com.linkedin.r2.message.RequestContext)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)3 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 Message (com.linkedin.restli.examples.greetings.api.Message)3