Search in sources :

Example 1 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class RestClient method getAnnouncedVersion.

/**
   * @param properties The server properties
   * @return the announced protocol version based on percentage
   */
/*package private*/
static ProtocolVersion getAnnouncedVersion(Map<String, Object> properties) {
    if (properties == null) {
        throw new RuntimeException("No valid properties found!");
    }
    Object potentialAnnouncedVersion = properties.get(RestConstants.RESTLI_PROTOCOL_VERSION_PROPERTY);
    // if the server doesn't announce a protocol version we assume it is running the baseline version
    if (potentialAnnouncedVersion == null) {
        return AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    }
    Object potentialAnnouncedVersionPercentage = properties.get(RestConstants.RESTLI_PROTOCOL_VERSION_PERCENTAGE_PROPERTY);
    // if the server doesn't announce a protocol version percentage we assume it is running the announced version
    if (potentialAnnouncedVersionPercentage == null) {
        return new ProtocolVersion(potentialAnnouncedVersion.toString());
    }
    try {
        int announceVersionPercentage = Integer.parseInt(potentialAnnouncedVersionPercentage.toString());
        // if server announces percentage between 1 to 100 which is also below or equal to the generated probability, then we return announced version else the baseline
        return (announceVersionPercentage > 0 && announceVersionPercentage <= 100 && RANDOM_INSTANCE.nextInt(100) + 1 <= announceVersionPercentage) ? new ProtocolVersion(potentialAnnouncedVersion.toString()) : AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    } catch (NumberFormatException e) {
        // if the server announces a incorrect protocol version percentage we assume it is running the baseline version
        return AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    }
}
Also used : ProtocolVersion(com.linkedin.restli.common.ProtocolVersion)

Example 2 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class MockBatchEntityResponseFactory method createWithCustomTyperefKey.

/**
   * Creates a {@link BatchKVResponse} where the key is a typeref to a custom Java class.
   *
   * @param keyClass the custom Java class
   * @param typerefClass the typeref class (the generated class that extends {@link RecordTemplate})
   * @param valueClass class for the value
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param statuses The HTTP status codes that will be returned as part of {@link EntityResponse}s returned in {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   * @param <K>
   * @param <TK>
   * @param <V>
   * @return
   */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <K, TK, V extends RecordTemplate> BatchKVResponse<K, EntityResponse<V>> createWithCustomTyperefKey(Class<K> keyClass, Class<TK> typerefClass, Class<V> valueClass, Map<K, V> recordTemplates, Map<K, HttpStatus> statuses, Map<K, ErrorResponse> errorResponses) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    DataMap batchResponseDataMap = buildDataMap(recordTemplates, statuses, errorResponses, version);
    return new BatchEntityResponse(batchResponseDataMap, TypeSpec.forClassMaybeNull(typerefClass), TypeSpec.forClassMaybeNull(valueClass), Collections.<String, CompoundKey.TypeInfo>emptyMap(), null, version);
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) CompoundKey(com.linkedin.restli.common.CompoundKey) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap)

Example 3 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class MockBatchKVResponseFactory method createWithComplexKey.

/**
   * Create a {@link BatchKVResponse} where the key is a {@link ComplexResourceKey}
   *
   * @param valueClass the value class
   * @param keyKeyClass the class of the key part of the {@link ComplexResourceKey}
   * @param keyParamsClass the class of the params part of the {@link ComplexResourceKey}
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   *                        NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                        instance of the params class is created with no data in it.
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   *                       NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                       instance of the params class is created with no data in it.
   * @param <V>
   * @return
   */
@SuppressWarnings("rawtypes")
public static <KK extends RecordTemplate, KP extends RecordTemplate, V extends RecordTemplate> BatchKVResponse<ComplexResourceKey<KK, KP>, V> createWithComplexKey(Class<V> valueClass, Class<KK> keyKeyClass, Class<KP> keyParamsClass, Map<ComplexResourceKey<KK, KP>, V> recordTemplates, Map<ComplexResourceKey<KK, KP>, ErrorResponse> errorResponses) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    DataMap batchResponseDataMap = buildDataMap(recordTemplates, errorResponses, version);
    @SuppressWarnings("unchecked") BatchKVResponse<ComplexResourceKey<KK, KP>, V> response = (BatchKVResponse<ComplexResourceKey<KK, KP>, V>) (Object) new BatchKVResponse<ComplexResourceKey, V>(batchResponseDataMap, ComplexResourceKey.class, valueClass, null, keyKeyClass, keyParamsClass, version);
    return response;
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap)

Example 4 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class MockFailedResponseFutureBuilder method build.

/**
   * Builds the {@link ResponseFuture}
   * @return
   */
@Override
public ResponseFuture<V> build() {
    if (_errorResponse == null && getEntity() == null) {
        // Create an ErrorResponse from the status, or use the DEFAULT_HTTP_STATUS to build one
        _errorResponse = new ErrorResponse();
        _errorResponse.setStatus(getStatus());
    }
    _errorHandlingBehavior = (_errorHandlingBehavior == null) ? ErrorHandlingBehavior.FAIL_ON_ERROR : _errorHandlingBehavior;
    ProtocolVersion protocolVersion = (getProtocolVersion() == null) ? AllProtocolVersions.BASELINE_PROTOCOL_VERSION : getProtocolVersion();
    if (_errorResponse != null) {
        return buildWithErrorResponse(protocolVersion);
    }
    // _entity has been set
    return buildWithEntity();
}
Also used : ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 5 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion 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, String>(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<V>(status, headers, cookies, _entity, _restLiResponseException);
    response.setAttachmentReader(_restLiAttachmentReader);
    return response;
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) CreateResponse(com.linkedin.restli.client.response.CreateResponse) TreeMap(java.util.TreeMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) HttpCookie(java.net.HttpCookie)

Aggregations

ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)35 DataMap (com.linkedin.data.DataMap)12 Test (org.testng.annotations.Test)8 CompoundKey (com.linkedin.restli.common.CompoundKey)7 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 URI (java.net.URI)7 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 HashMap (java.util.HashMap)5 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 ResourceMethod (com.linkedin.restli.common.ResourceMethod)4 ByteString (com.linkedin.data.ByteString)3 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 IdResponse (com.linkedin.restli.common.IdResponse)3 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)3 HttpCookie (java.net.HttpCookie)3 HashSet (java.util.HashSet)3 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)2 Foo (com.linkedin.pegasus.generator.examples.Foo)2