use of com.linkedin.restli.internal.client.ResponseImpl in project parseq by linkedin.
the class GetRequestGroup method unbatchResponse.
private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException {
final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity();
final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
if (errorResponse != null) {
throw new RestLiResponseException(errorResponse);
}
final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id);
if (entityResponse != null) {
final RT entityResult = entityResponse.getEntity();
if (entityResult != null) {
return new ResponseImpl<>(batchResponse, entityResult);
}
}
LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id);
throw NOT_FOUND_EXCEPTION;
}
use of com.linkedin.restli.internal.client.ResponseImpl in project rest.li by linkedin.
the class MockResponseBuilder method build.
/**
* Builds a {@link Response} that has been constructed using the setters in this class.
*
* @return the constructed {@link Response}
*/
public Response<V> build() {
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
if (_headers != null) {
headers.putAll(_headers);
}
ProtocolVersion protocolVersion = (_protocolVersion == null) ? AllProtocolVersions.BASELINE_PROTOCOL_VERSION : _protocolVersion;
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
int status = (_status == 0) ? DEFAULT_HTTP_STATUS : _status;
if (_entity instanceof CreateResponse || _entity instanceof IdResponse) {
final K id;
if (_entity instanceof CreateResponse) {
@SuppressWarnings("unchecked") final CreateResponse<K> createResponse = (CreateResponse<K>) _entity;
id = createResponse.getId();
} else {
@SuppressWarnings("unchecked") final IdResponse<K> idResponse = (IdResponse<K>) _entity;
id = idResponse.getId();
}
headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForBody(id, false, protocolVersion));
}
List<HttpCookie> cookies = _cookies == null ? Collections.<HttpCookie>emptyList() : _cookies;
final ResponseImpl<V> response = new ResponseImpl<>(status, headers, cookies, _entity, _restLiResponseException);
response.setAttachmentReader(_restLiAttachmentReader);
return response;
}
use of com.linkedin.restli.internal.client.ResponseImpl in project rest.li by linkedin.
the class BatchGetRequestUtil method unbatchResponse.
/**
* Extract the get response for this resource out of an auto-batched batch response.
* This is pure rest.li logic, and it complements the auto-batching logic in BatchGetRequestBuilder.
* @throws com.linkedin.r2.RemoteInvocationException if the server returned an error response for this resource,
* or if it returned neither a result nor an error.
*/
public static <T extends RecordTemplate> Response<T> unbatchResponse(Request<BatchResponse<T>> request, Response<BatchResponse<T>> batchResponse, Object id) throws RemoteInvocationException {
final BatchResponse<T> batchEntity = batchResponse.getEntity();
final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
if (errorResponse != null) {
throw new RestLiResponseException(errorResponse);
}
final T entityResult = batchEntity.getResults().get(id);
if (entityResult == null) {
throw new RestLiDecodingException("No result or error for base URI " + request.getBaseUriTemplate() + ", id " + id + ". Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", null);
}
return new ResponseImpl<>(batchResponse, entityResult);
}
use of com.linkedin.restli.internal.client.ResponseImpl in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method createBatchUpdateEntityResponse.
private static Response<BatchKVResponse<Long, UpdateEntityStatus<TestRecord>>> createBatchUpdateEntityResponse(ProtocolVersion version, Set<Long> resultKeys, Set<Long> errorKeys) {
DataMap resultMap = new DataMap();
DataMap errorMap = new DataMap();
for (Long id : resultKeys) {
resultMap.put(id.toString(), new UpdateEntityStatus<>(HttpStatus.S_200_OK.getCode(), new TestRecord().setId(id)).data());
}
for (Long id : errorKeys) {
ErrorResponse err = new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode());
errorMap.put(id.toString(), err.data());
}
DataMap responseMap = new DataMap();
responseMap.put(BatchResponse.RESULTS, resultMap);
responseMap.put(BatchResponse.ERRORS, errorMap);
DataMap mergedMap = ResponseDecoderUtil.mergeUpdateStatusResponseData(responseMap);
BatchUpdateEntityResponse<Long, TestRecord> response = new BatchUpdateEntityResponse<>(mergedMap, new TypeSpec<>(Long.class), new TypeSpec<>(TestRecord.class), Collections.emptyMap(), null, version);
return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
use of com.linkedin.restli.internal.client.ResponseImpl in project parseq by linkedin.
the class GetRequestGroup method doExecuteBatchGet.
@SuppressWarnings({ "rawtypes", "unchecked" })
private <K, RT extends RecordTemplate> void doExecuteBatchGet(final Client client, final Batch<RestRequestBatchKey, Response<Object>> batch, final Set<Object> ids, final Set<PathSpec> fields, Function<Request<?>, RequestContext> requestContextProvider) {
final BatchGetEntityRequestBuilder<K, RT> builder = new BatchGetEntityRequestBuilder<>(_baseUriTemplate, _resourceSpec, _requestOptions);
builder.setHeaders(_headers);
builder.setCookies(_cookies);
_queryParams.forEach((key, value) -> builder.setParam(key, value));
_pathKeys.forEach((key, value) -> builder.pathKey(key, value));
builder.ids((Set<K>) ids);
if (fields != null && !fields.isEmpty()) {
builder.fields(fields.toArray(new PathSpec[fields.size()]));
}
final BatchGetEntityRequest<K, RT> batchGet = builder.build();
client.sendRequest(batchGet, requestContextProvider.apply(batchGet), new Callback<Response<BatchKVResponse<K, EntityResponse<RT>>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch) {
final ProtocolVersion version = ProtocolVersionUtil.extractProtocolVersion(responseToBatch.getHeaders());
batch.entries().stream().forEach(entry -> {
try {
RestRequestBatchKey rrbk = entry.getKey();
Request request = rrbk.getRequest();
if (request instanceof GetRequest) {
successGet((GetRequest) request, responseToBatch, batchGet, entry, version);
} else if (request instanceof BatchGetKVRequest) {
successBatchGetKV((BatchGetKVRequest) request, responseToBatch, entry, version);
} else if (request instanceof BatchGetRequest) {
successBatchGet((BatchGetRequest) request, responseToBatch, entry, version);
} else if (request instanceof BatchGetEntityRequest) {
successBatchGetEntity((BatchGetEntityRequest) request, responseToBatch, entry, version);
} else {
entry.getValue().getPromise().fail(unsupportedGetRequestType(request));
}
} catch (RemoteInvocationException e) {
entry.getValue().getPromise().fail(e);
}
});
}
@SuppressWarnings({ "deprecation" })
private void successBatchGetEntity(BatchGetEntityRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
BatchKVResponse br = new BatchEntityResponse<>(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
Response rsp = new ResponseImpl(responseToBatch, br);
entry.getValue().getPromise().done(rsp);
}
private void successBatchGet(BatchGetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
BatchResponse br = new BatchResponse<>(dm, request.getResponseDecoder().getEntityClass());
Response rsp = new ResponseImpl(responseToBatch, br);
entry.getValue().getPromise().done(rsp);
}
@SuppressWarnings({ "deprecation" })
private void successBatchGetKV(BatchGetKVRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
BatchKVResponse br = new BatchKVResponse(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
Response rsp = new ResponseImpl(responseToBatch, br);
entry.getValue().getPromise().done(rsp);
}
@SuppressWarnings({ "deprecation" })
private void successGet(GetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, final BatchGetEntityRequest<K, RT> batchGet, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) throws RemoteInvocationException {
String idString = BatchResponse.keyToString(request.getObjectId(), version);
Object id = ResponseUtils.convertKey(idString, request.getResourceSpec().getKeyType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
Response rsp = unbatchResponse(batchGet, responseToBatch, id);
entry.getValue().getPromise().done(rsp);
}
@Override
public void onError(Throwable e) {
batch.failAll(e);
}
});
}
Aggregations