Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.linkedin.restli.client.BatchGetRequestBuilder Maven / Gradle / Ivy
/*
Copyright (c) 2012 LinkedIn Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.linkedin.restli.client;
import com.linkedin.data.schema.PathSpec;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.restli.common.BatchResponse;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.CompoundKey;
import com.linkedin.restli.common.ResourceSpec;
import com.linkedin.restli.common.RestConstants;
import com.linkedin.restli.common.TypeSpec;
import com.linkedin.restli.internal.client.BatchKVResponseDecoder;
import com.linkedin.restli.internal.client.BatchResponseDecoder;
import com.linkedin.restli.internal.client.RestResponseDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Builds a type-bound Request for a batch of resources.
*
* This class has been deprecated. Please use {@link BatchGetEntityRequestBuilder} instead.
*
* @param batch element type
*
* @author Eran Leshem
*/
public class BatchGetRequestBuilder extends
BatchKVRequestBuilder>
{
private final RestResponseDecoder> _decoder;
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* Base URIs of all requests must be the same
*
*
* Batching is performed along two dimensions:
*
* entity ids are accumulated
* fields are accumulated. If any of the batched requests has a null projection,
* then the resulting batch request will also have a null projection
*
*
* @param requests to batch
* @param type of entity template
* @return batching request
*/
public static BatchGetRequest batch(List> requests)
{
return batch(requests, true);
}
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* Base URIs of all requests must be the same
* if {@code batchFields} is {@code false}, then all requests must have the same projection
*
*
* Batching is performed along one or two dimensions:
*
* entity ids are always accumulated
* if {@code batchFields} is {@code true}, fields are also accumulated. If any of the batched requests has a
* null projection, then the resulting batch request will also have a null projection.
*
*
*
*
* @param requests to batch
* @param batchFields whether field batching is desired
* @param type of entity template
* @return batching request
*/
@SuppressWarnings("deprecation")
public static BatchGetRequest batch(List> requests,
boolean batchFields)
{
final BatchGetRequest firstRequest = requests.get(0);
Class> keyClass = firstRequest.getResourceSpec().getKeyClass();
throwIfClassCompoundOrComplex(keyClass, "batch", "batchKV");
final ResourceSpec firstResourceSpec = firstRequest.getResourceSpec();
final Map batchQueryParams =
getReadOnlyQueryParameters(BatchGetRequestUtil.getBatchQueryParam(requests, batchFields));
return new BatchGetRequest(getReadOnlyHeaders(firstRequest.getHeaders()),
getReadOnlyCookies(firstRequest.getCookies()),
firstRequest.getResponseDecoder(),
batchQueryParams,
firstRequest.getQueryParamClasses(),
firstResourceSpec,
firstRequest.getBaseUriTemplate(),
getReadOnlyPathKeys(firstRequest.getPathKeys()),
firstRequest.getRequestOptions());
}
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* Base URIs of all requests must be the same
*
*
* Batching is performed along two dimensions:
*
* entity ids are accumulated
* fields are accumulated. If any of the batched requests has a null projection,
* then the resulting batch request will also have a null projection
*
*
* @param requests to batch
* @param type of the key
* @param type of entity template
* @return batching request
*/
public static BatchGetKVRequest batchKV(List> requests)
{
return batchKV(requests, true);
}
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* Base URIs of all requests must be the same
* if {@code batchFields} is {@code false}, then all requests must have the same projection
*
*
* Batching is performed along one or two dimensions:
*
* entity ids are always accumulated
* if {@code batchFields} is {@code true}, fields are also accumulated. If any of the batched requests has a
* null projection, then the resulting batch request will also have a null projection.
*
*
*
*
* @param requests to batch
* @param batchFields whether field batching is desired
* @param type of the key
* @param type of entity template
* @return batching request
*/
@SuppressWarnings("deprecation")
public static BatchGetKVRequest batchKV(List> requests,
boolean batchFields)
{
final BatchGetKVRequest firstRequest = requests.get(0);
final ResourceSpec firstResourceSpec = firstRequest.getResourceSpec();
final Map batchQueryParams =
getReadOnlyQueryParameters(BatchGetRequestUtil.getBatchQueryParam(requests, batchFields));
return new BatchGetKVRequest(
getReadOnlyHeaders(firstRequest.getHeaders()),
getReadOnlyCookies(firstRequest.getCookies()),
firstRequest.getResponseDecoder(),
batchQueryParams,
Collections.>emptyMap(),
firstResourceSpec,
firstRequest.getBaseUriTemplate(),
getReadOnlyPathKeys(firstRequest.getPathKeys()),
firstRequest.getRequestOptions());
}
/**
* Converts an entity request to a batch one, for subsequent batching with other requests.
* @param request to convert
* @param type of entity template
* @return batch request
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public static BatchGetKVRequest batchKV(GetRequest request)
{
Object id = request.getObjectId();
if (id == null)
{
throw new IllegalArgumentException(
"It is not possible to create a batch get request from a get request without an id.");
}
Map queryParams = new HashMap(request.getQueryParamsObjects());
queryParams.put(RestConstants.QUERY_BATCH_IDS_PARAM,
new ArrayList(Arrays.asList(id)));
return new BatchGetKVRequest(getReadOnlyHeaders(request.getHeaders()),
getReadOnlyCookies(request.getCookies()),
new BatchKVResponseDecoder(
request.getEntityClass(),
(Class)request.getResourceProperties().getKeyType().getType(),
request.getResourceProperties().getKeyParts(),
request.getResourceProperties().getComplexKeyType() == null ?
null :
request.
getResourceProperties().
getComplexKeyType().
getKeyType().
getType(),
request.getResourceProperties().getComplexKeyType() == null ?
null :
request.
getResourceProperties().
getComplexKeyType().
getParamsType().
getType()),
getReadOnlyQueryParameters(queryParams),
request.getQueryParamClasses(),
request.getResourceSpec(),
request.getBaseUriTemplate(),
getReadOnlyPathKeys(request.getPathKeys()),
request.getRequestOptions());
}
/**
* Converts an entity request to a batch one, for subsequent batching with other requests.
* @param request to convert
* @param type of entity template
* @return batch request
*/
@SuppressWarnings("deprecation")
public static BatchGetRequest batch(GetRequest request)
{
Object id = request.getObjectId();
if (id == null)
{
throw new IllegalArgumentException(
"It is not possible to create a batch get request from a get request without an id.");
}
Class> keyClass = request.getResourceSpec().getKeyClass();
throwIfClassCompoundOrComplex(keyClass, "batch", "batchKV");
Map queryParams = new HashMap(request.getQueryParamsObjects());
queryParams.put(RestConstants.QUERY_BATCH_IDS_PARAM,
new ArrayList(Arrays.asList(id)));
return new BatchGetRequest(getReadOnlyHeaders(request.getHeaders()),
getReadOnlyCookies(request.getCookies()),
new BatchResponseDecoder(request.getEntityClass()),
getReadOnlyQueryParameters(queryParams),
Collections.>emptyMap(),
request.getResourceSpec(),
request.getBaseUriTemplate(),
getReadOnlyPathKeys(request.getPathKeys()),
request.getRequestOptions());
}
public BatchGetRequestBuilder(String baseUriTemplate,
Class modelClass,
ResourceSpec resourceSpec,
RestliRequestOptions requestOptions)
{
this(baseUriTemplate, new BatchResponseDecoder(modelClass), resourceSpec, requestOptions);
}
public BatchGetRequestBuilder(String baseUriTemplate,
RestResponseDecoder> decoder,
ResourceSpec resourceSpec,
RestliRequestOptions requestOptions)
{
super(baseUriTemplate, resourceSpec, requestOptions);
_decoder = decoder;
}
@SuppressWarnings("unchecked")
public BatchGetRequestBuilder ids(K... ids)
{
return ids(Arrays.asList(ids));
}
public BatchGetRequestBuilder ids(Collection ids)
{
addKeys(ids);
return this;
}
@Override
public BatchGetRequestBuilder setParam(String key, Object value)
{
super.setParam(key, value);
return this;
}
@Override
public BatchGetRequestBuilder setReqParam(String key, Object value)
{
super.setReqParam(key, value);
return this;
}
@Override
public BatchGetRequestBuilder addParam(String key, Object value)
{
super.addParam(key, value);
return this;
}
@Override
public BatchGetRequestBuilder addReqParam(String key, Object value)
{
super.addReqParam(key, value);
return this;
}
@Override
public BatchGetRequestBuilder setHeader(String key, String value)
{
super.setHeader(key, value);
return this;
}
@Override
public BatchGetRequestBuilder setHeaders(Map headers)
{
super.setHeaders(headers);
return this;
}
@Override
public BatchGetRequestBuilder addHeader(String name, String value)
{
super.addHeader(name, value);
return this;
}
@Override
public BatchGetRequestBuilder pathKey(String name, Object value)
{
super.pathKey(name, value);
return this;
}
/**
* Builds a GET request for this resource batch.
*
* @return a read request for the resource batch
*/
@Override
public BatchGetRequest build()
{
ensureBatchKeys();
Class> keyClass = _resourceSpec.getKeyClass();
throwIfClassCompoundOrComplex(keyClass, "build", "buildKV");
return new BatchGetRequest(buildReadOnlyHeaders(),
buildReadOnlyCookies(),
_decoder,
buildReadOnlyQueryParameters(),
Collections.>emptyMap(),
_resourceSpec,
getBaseUriTemplate(),
buildReadOnlyPathKeys(),
getRequestOptions());
}
public BatchGetKVRequest buildKV()
{
ensureBatchKeys();
//Framework code should ensure that the ResourceSpec matches the static types of these parameters
@SuppressWarnings("unchecked")
BatchKVResponseDecoder decoder =
new BatchKVResponseDecoder((TypeSpec) _resourceSpec.getValueType(),
(TypeSpec) _resourceSpec.getKeyType(),
_resourceSpec.getKeyParts(),
_resourceSpec.getComplexKeyType());
return new BatchGetKVRequest(buildReadOnlyHeaders(),
buildReadOnlyCookies(),
decoder,
buildReadOnlyQueryParameters(),
getQueryParamClasses(),
_resourceSpec,
getBaseUriTemplate(),
buildReadOnlyPathKeys(),
getRequestOptions());
}
public BatchGetRequestBuilder fields(PathSpec... fieldPaths)
{
addFields(fieldPaths);
return this;
}
private static void throwIfClassCompoundOrComplex(Class> keyClass, String currentMethod, String replacementMethod)
{
if (CompoundKey.class.isAssignableFrom(keyClass) ||
keyClass == ComplexResourceKey.class)
{
throw new UnsupportedOperationException(
"The " + currentMethod + " method cannot be used with Compound or Complex key types. " +
"Please use the " + replacementMethod + " method instead.");
}
}
}