use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method updateStatusInstantiation.
@DataProvider(name = "updateStatusInstantiation")
public Object[][] updateStatusInstantiation() {
Map<String, BatchResponseEnvelope.BatchResponseEntry> normal = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
UpdateStatus foo = new UpdateStatus();
// should be overwritten
foo.setStatus(500);
//should be preserved
foo.data().put("foo", "bar");
normal.put("key", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, foo));
UpdateStatus normalStatus = new UpdateStatus();
normalStatus.setStatus(200);
normalStatus.data().put("foo", "bar");
Map<String, BatchResponseEnvelope.BatchResponseEntry> missing = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
missing.put("key", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, (RecordTemplate) null));
UpdateStatus missingStatus = new UpdateStatus();
missingStatus.setStatus(200);
Map<String, BatchResponseEnvelope.BatchResponseEntry> mismatch = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
mismatch.put("key", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, new AnyRecord(new DataMap())));
UpdateStatus mismatchedStatus = new UpdateStatus();
mismatchedStatus.setStatus(200);
RestLiResponseDataImpl batchGetNormal = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchGetNormal.setResponseEnvelope(new BatchGetResponseEnvelope(normal, batchGetNormal));
RestLiResponseDataImpl batchGetMissing = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchGetMissing.setResponseEnvelope(new BatchGetResponseEnvelope(missing, batchGetNormal));
RestLiResponseDataImpl batchGetMismatch = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchGetMismatch.setResponseEnvelope(new BatchGetResponseEnvelope(mismatch, batchGetNormal));
RestLiResponseDataImpl batchUpdateNormal = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchUpdateNormal.setResponseEnvelope(new BatchUpdateResponseEnvelope(normal, batchUpdateNormal));
RestLiResponseDataImpl batchUpdateMissing = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchUpdateMissing.setResponseEnvelope(new BatchUpdateResponseEnvelope(missing, batchUpdateMissing));
RestLiResponseDataImpl batchUpdateMismatch = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchUpdateMismatch.setResponseEnvelope(new BatchUpdateResponseEnvelope(mismatch, batchUpdateMismatch));
RestLiResponseDataImpl batchPartialUpdateNormal = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchPartialUpdateNormal.setResponseEnvelope(new BatchPartialUpdateResponseEnvelope(normal, batchPartialUpdateNormal));
RestLiResponseDataImpl batchPartialUpdateMissing = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchPartialUpdateMissing.setResponseEnvelope(new BatchPartialUpdateResponseEnvelope(missing, batchPartialUpdateMissing));
RestLiResponseDataImpl batchPartialUpdateMismatch = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchPartialUpdateMismatch.setResponseEnvelope(new BatchPartialUpdateResponseEnvelope(mismatch, batchPartialUpdateMismatch));
RestLiResponseDataImpl batchDeleteNormal = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchDeleteNormal.setResponseEnvelope(new BatchDeleteResponseEnvelope(normal, batchDeleteNormal));
RestLiResponseDataImpl batchDeleteMissing = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchDeleteMissing.setResponseEnvelope(new BatchDeleteResponseEnvelope(missing, batchDeleteMissing));
RestLiResponseDataImpl batchDeleteMismatch = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
batchDeleteMismatch.setResponseEnvelope(new BatchDeleteResponseEnvelope(mismatch, batchDeleteMismatch));
return new Object[][] { { batchGetNormal, normalStatus }, { batchGetMissing, missingStatus }, { batchGetMismatch, mismatchedStatus }, { batchUpdateNormal, normalStatus }, { batchUpdateMissing, missingStatus }, { batchUpdateMismatch, mismatchedStatus }, { batchPartialUpdateNormal, normalStatus }, { batchPartialUpdateMissing, missingStatus }, { batchPartialUpdateMismatch, mismatchedStatus }, { batchDeleteNormal, normalStatus }, { batchDeleteMissing, missingStatus }, { batchDeleteMismatch, mismatchedStatus } };
}
use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.
the class GetResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
final RecordTemplate record;
final HttpStatus status;
if (result instanceof GetResult) {
final GetResult<?> getResult = (GetResult<?>) result;
record = getResult.getValue();
status = getResult.getStatus();
} else {
record = (RecordTemplate) result;
status = HttpStatus.S_200_OK;
}
final ResourceContext resourceContext = routingResult.getContext();
final DataMap data = RestUtils.projectFields(record.data(), resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
responseData.setResponseEnvelope(new GetResponseEnvelope(new AnyRecord(data), responseData));
return responseData;
}
use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.
the class BatchGetResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
// Build the EntityResponse for each key from the merged map with mask from routingResult.
Map<Object, EntityResponse<RecordTemplate>> entityBatchResponse = buildEntityResponse(routingResult, responses);
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
@SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(entityBatchResponse, protocolVersion);
builder.entity(response);
return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.
the class BatchUpdateResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
Map<Object, UpdateStatus> mergedResults = new HashMap<Object, UpdateStatus>();
final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
generateResultEntityResponse(routingResult, responses, mergedResults);
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
@SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(mergedResults, protocolVersion);
return builder.entity(response).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.
the class CreateResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
CreateResponse createResponse = (CreateResponse) result;
if (createResponse.hasError()) {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
return responseData;
}
Object id = null;
if (createResponse.hasId()) {
id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
uribuilder.path(stringKey);
if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
// add altkey param to location URI
uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
}
headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
}
//Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
if (createResponse.getStatus() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
CreateResponseEnvelope responseEnvelope;
if (createResponse instanceof CreateKVResponse) {
final ResourceContext resourceContext = routingResult.getContext();
DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
} else //Instance of idResponse
{
IdResponse<?> idResponse = new IdResponse<Object>(id);
responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
}
responseData.setResponseEnvelope(responseEnvelope);
return responseData;
}
Aggregations