Search in sources :

Example 41 with RecordTemplate

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);
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) ByteString(com.linkedin.data.ByteString) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 42 with RecordTemplate

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;
}
Also used : ByteString(com.linkedin.data.ByteString) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 43 with RecordTemplate

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;
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions)

Example 44 with 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);
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 45 with RecordTemplate

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;
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) GetResult(com.linkedin.restli.server.GetResult) HttpStatus(com.linkedin.restli.common.HttpStatus) RecordTemplate(com.linkedin.data.template.RecordTemplate) DataMap(com.linkedin.data.DataMap)

Aggregations

RecordTemplate (com.linkedin.data.template.RecordTemplate)59 DataMap (com.linkedin.data.DataMap)23 Test (org.testng.annotations.Test)23 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)9 DataList (com.linkedin.data.DataList)8 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)8 ArrayList (java.util.ArrayList)8 PathSpec (com.linkedin.data.schema.PathSpec)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)7 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)7 BeforeTest (org.testng.annotations.BeforeTest)7 ByteString (com.linkedin.data.ByteString)6 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)6 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)6 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)6 RoutingException (com.linkedin.restli.server.RoutingException)6