use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class RestClient method sendRestRequest.
/**
* @deprecated as this API will change to private in a future release. Please use other APIs in this class, such as
* {@link RestClient#sendRequest(com.linkedin.restli.client.Request,com.linkedin.r2.message.RequestContext, com.linkedin.common.callback.Callback)}
* to send type-bound REST requests.
*
* Sends a type-bound REST request and answers on the provided callback.
*
* @param request to send
* @param requestContext context for the request
* @param callback to call on request completion
*/
@Deprecated
public <T> void sendRestRequest(final Request<T> request, RequestContext requestContext, Callback<RestResponse> callback) {
//We need this until we remove the deprecation above since clients could attempt these:
if (request.getStreamingAttachments() != null) {
throw new UnsupportedOperationException("Cannot stream attachments using RestRequest/RestResponse!");
}
if (request.getRequestOptions() != null && request.getRequestOptions().getAcceptResponseAttachments()) {
throw new UnsupportedOperationException("Cannot expect streaming attachments using RestRequest/RestResponse!");
}
RecordTemplate input = request.getInputRecord();
ProtocolVersion protocolVersion = getProtocolVersionForService(request);
URI requestUri = RestliUriBuilderUtil.createUriBuilder(request, _uriPrefix, protocolVersion).build();
final ResourceMethod method = request.getMethod();
final String methodName = request.getMethodName();
addDisruptContext(request.getBaseUriTemplate(), method, methodName, requestContext);
sendRestRequestImpl(requestContext, requestUri, method, input != null ? RequestBodyTransformer.transform(request, protocolVersion) : null, request.getHeaders(), CookieUtil.encodeCookies(request.getCookies()), methodName, protocolVersion, request.getRequestOptions(), callback);
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class QueryParamsDataMap method fixUpComplexKeySingletonArray.
/**
* Because of backwards compatibility concerns, array fields of the key component of a
* {@link ComplexResourceKey}s in a get request will be represented in the request url in the old
* style. That is, if an array field has the name "a", and contains [1,2] the part of the url
* representing the serialized array will look like "a=1&a=2". However, if the array is a
* singleton it will just be represented by "a=1". Therefore it is not possible to distinguish
* between a single value itself and an array containing a single value.
*
* The purpose of this function is to fix up the singleton array problem by checking to see whether the given
* ComplexKey's key part has an array component, and, if so and the data for that field is NOT a dataList,
* placing the data into a dataList.
*
* @param complexResourceKey The complex key to be fixed.
*/
public static ComplexResourceKey<?, ?> fixUpComplexKeySingletonArray(ComplexResourceKey<?, ?> complexResourceKey) {
RecordTemplate key = complexResourceKey.getKey();
DataMap dataMap = key.data();
List<RecordDataSchema.Field> fields = key.schema() == null ? Collections.<RecordDataSchema.Field>emptyList() : key.schema().getFields();
for (RecordDataSchema.Field f : fields) {
DataSchema.Type type = f.getType().getType();
String fieldName = f.getName();
if (type == DataSchema.Type.ARRAY && dataMap.containsKey(fieldName)) {
Object arrayFieldValue = dataMap.get(fieldName);
if (!(arrayFieldValue instanceof DataList)) {
DataList list = new DataList();
list.add(arrayFieldValue);
ValidateDataAgainstSchema.validate(list, f.getType(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
dataMap.put(fieldName, list);
}
}
}
RecordTemplate wrappedKey = DataTemplateUtil.wrap(dataMap, key.getClass());
@SuppressWarnings("unchecked") ComplexResourceKey<?, ?> newKey = new ComplexResourceKey<RecordTemplate, RecordTemplate>(wrappedKey, complexResourceKey.getParams());
return newKey;
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class ComplexResourceKey method validateDataMap.
private static RecordTemplate validateDataMap(DataMap dataMap, TypeSpec<? extends RecordTemplate> spec) {
RecordTemplate recordTemplate = wrapWithSchema(dataMap, spec);
// Validate against the class schema with FixupMode.STRING_TO_PRIMITIVE to parse the
// strings into the
// corresponding primitive types.
ValidateDataAgainstSchema.validate(recordTemplate.data(), recordTemplate.schema(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
return recordTemplate;
}
use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.
the class TestComplexResourceKey method testKeySchema.
@Test
public void testKeySchema() {
RecordDataSchema schema = OmniRecord.schema;
TypeSpec<OmniRecord> keyType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
TypeSpec<OmniRecord> paramsType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
ComplexKeySpec<OmniRecord, OmniRecord> keySpec = new ComplexKeySpec<OmniRecord, OmniRecord>(keyType, paramsType);
DataMap data = new DataMap();
data.put("int", 1);
ComplexResourceKey<RecordTemplate, RecordTemplate> key = ComplexResourceKey.buildFromDataMap(data, keySpec);
Assert.assertEquals(key.getKey().schema(), schema);
Assert.assertEquals(key.getParams().schema(), schema);
}
use of com.linkedin.data.template.RecordTemplate 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;
}
Aggregations