use of com.linkedin.restli.common.HttpStatus in project rest.li by linkedin.
the class MockBatchEntityResponseFactory method buildDataMap.
private static <K, V extends RecordTemplate> DataMap buildDataMap(Map<K, V> recordTemplates, Map<K, HttpStatus> statuses, Map<K, ErrorResponse> errorResponses, ProtocolVersion version) {
Set<K> mergedKeys = new HashSet<K>();
mergedKeys.addAll(recordTemplates.keySet());
mergedKeys.addAll(statuses.keySet());
mergedKeys.addAll(errorResponses.keySet());
DataMap batchResponseDataMap = new DataMap();
DataMap rawBatchData = new DataMap();
for (K key : mergedKeys) {
DataMap entityResponseData = new DataMap();
RecordTemplate recordTemplate = recordTemplates.get(key);
if (recordTemplate != null) {
entityResponseData.put(EntityResponse.ENTITY, recordTemplate.data());
}
HttpStatus status = statuses.get(key);
if (status != null) {
entityResponseData.put(EntityResponse.STATUS, status.getCode());
}
ErrorResponse errorResponse = errorResponses.get(key);
if (errorResponse != null) {
entityResponseData.put(EntityResponse.ERROR, errorResponse.data());
}
String stringKey = URIParamUtils.encodeKeyForBody(key, false, version);
rawBatchData.put(stringKey, entityResponseData);
}
batchResponseDataMap.put(BatchResponse.RESULTS, rawBatchData);
DataMap rawErrorData = new DataMap();
for (Map.Entry<K, ErrorResponse> errorResponse : errorResponses.entrySet()) {
rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
}
batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
return batchResponseDataMap;
}
use of com.linkedin.restli.common.HttpStatus 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;
}
use of com.linkedin.restli.common.HttpStatus in project rest.li by linkedin.
the class TestMockBatchKVResponseFactory method compoundKeyData.
@DataProvider(name = "compoundKey")
public Object[][] compoundKeyData() {
CompoundKey c1 = buildCompoundKey("c1", 1);
CompoundKey c2 = buildCompoundKey("c2", 2);
CompoundKey c3 = buildCompoundKey("c3", 3);
Map<CompoundKey, Greeting> recordTemplates = new HashMap<CompoundKey, Greeting>();
recordTemplates.put(c1, buildGreeting(1L));
recordTemplates.put(c2, buildGreeting(2L));
Map<CompoundKey, ErrorResponse> errorResponses = new HashMap<CompoundKey, ErrorResponse>();
errorResponses.put(c3, new ErrorResponse().setMessage("3"));
Map<CompoundKey, HttpStatus> statuses = new HashMap<CompoundKey, HttpStatus>();
statuses.put(c1, HttpStatus.S_200_OK);
statuses.put(c2, HttpStatus.S_200_OK);
statuses.put(c3, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<String, CompoundKey.TypeInfo> keyParts = new HashMap<String, CompoundKey.TypeInfo>();
keyParts.put("part1", new CompoundKey.TypeInfo(String.class, String.class));
keyParts.put("part2", new CompoundKey.TypeInfo(Integer.class, Integer.class));
Map<CompoundKey, EntityResponse<Greeting>> expectedResults = new HashMap<CompoundKey, EntityResponse<Greeting>>();
expectedResults.put(c1, buildEntityResponse(recordTemplates.get(c1), HttpStatus.S_200_OK, null));
expectedResults.put(c2, buildEntityResponse(recordTemplates.get(c2), HttpStatus.S_200_OK, null));
expectedResults.put(c3, buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(c3)));
return new Object[][] { { keyParts, recordTemplates, statuses, errorResponses, expectedResults } };
}
use of com.linkedin.restli.common.HttpStatus in project rest.li by linkedin.
the class TestMockBatchKVResponseFactory method customPrimitiveTyperefKeyData.
@DataProvider(name = "customPrimitiveTyperefKey")
public Object[][] customPrimitiveTyperefKeyData() {
MyCustomString m1 = new MyCustomString("1");
MyCustomString m2 = new MyCustomString("2");
MyCustomString m3 = new MyCustomString("3");
Map<MyCustomString, Greeting> recordTemplates = new HashMap<MyCustomString, Greeting>();
Map<MyCustomString, ErrorResponse> errorResponses = new HashMap<MyCustomString, ErrorResponse>();
recordTemplates.put(m1, buildGreeting(1L));
recordTemplates.put(m2, buildGreeting(2L));
errorResponses.put(m3, new ErrorResponse().setMessage("3"));
Map<MyCustomString, HttpStatus> statuses = new HashMap<MyCustomString, HttpStatus>();
statuses.put(m1, HttpStatus.S_200_OK);
statuses.put(m2, HttpStatus.S_200_OK);
statuses.put(m3, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<MyCustomString, EntityResponse<Greeting>> expectedResults = new HashMap<MyCustomString, EntityResponse<Greeting>>();
expectedResults.put(m1, buildEntityResponse(recordTemplates.get(m1), HttpStatus.S_200_OK, null));
expectedResults.put(m2, buildEntityResponse(recordTemplates.get(m2), HttpStatus.S_200_OK, null));
expectedResults.put(m3, buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(m3)));
return new Object[][] { { recordTemplates, statuses, errorResponses, expectedResults } };
}
use of com.linkedin.restli.common.HttpStatus in project rest.li by linkedin.
the class TestMockBatchKVResponseFactory method complexKeyData.
@DataProvider(name = "complexKey")
public Object[][] complexKeyData() {
Map<ComplexResourceKey<Greeting, Greeting>, Greeting> recordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
Greeting g1 = buildGreeting(1L);
Greeting g2 = buildGreeting(2L);
Greeting g3 = buildGreeting(3L);
recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), g1);
recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), g2);
errorResponses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), new ErrorResponse().setMessage("3"));
Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<ComplexResourceKey<Greeting, Greeting>, HttpStatus>();
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), HttpStatus.S_200_OK);
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), HttpStatus.S_200_OK);
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
// Strip the parameters from complex keys in expected results and expected errors.
Map<ComplexResourceKey<Greeting, Greeting>, Greeting> expectedRecordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)));
expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)));
Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>>();
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)), HttpStatus.S_200_OK, null));
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)), HttpStatus.S_200_OK, null));
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3))));
Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
expectedErrors.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3)));
return new Object[][] { { recordTemplates, statuses, errorResponses, expectedRecordTemplates, expectedResults, expectedErrors } };
}
Aggregations