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;
}
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);
}
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());
}
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());
}
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());
}
Aggregations