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;
}
}
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);
}
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;
}
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();
}
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;
}
Aggregations