use of com.linkedin.r2.message.rest.RestResponse in project rest.li by linkedin.
the class RewriteClientTest method testEscapingHelper.
private void testEscapingHelper(String hostUri, String serviceName, String path) {
URI uri = URI.create(hostUri);
TestClient wrappedClient = new TestClient();
RewriteClient client = new RewriteClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
assertEquals(client.getWrappedClient(), wrappedClient);
RestRequest restRequest = new RestRequestBuilder(URI.create("d2://" + serviceName + path)).build();
Map<String, String> restWireAttrs = new HashMap<String, String>();
TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<RestResponse>();
client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.restRequest.getHeaders(), restRequest.getHeaders());
assertEquals(wrappedClient.restRequest.getEntity(), restRequest.getEntity());
assertEquals(wrappedClient.restRequest.getMethod(), restRequest.getMethod());
assertEquals(wrappedClient.restRequest.getURI(), URI.create(hostUri + path));
}
use of com.linkedin.r2.message.rest.RestResponse in project voldemort by voldemort.
the class R2Store method put.
@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transform) throws VoldemortException {
RestResponse response = null;
try {
byte[] payload = value.getValue();
// Create the REST request with this byte array
String base64Key = RestUtils.encodeVoldemortKey(key.get());
RestRequestBuilder rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key));
// Create a HTTP POST request
rb.setMethod(POST);
rb.setEntity(payload);
rb.setHeader(CONTENT_TYPE, "binary");
rb.setHeader(CONTENT_LENGTH, "" + payload.length);
String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.PUT_OP_CODE));
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis()));
if (this.routingTypeCode != null) {
rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode);
}
if (this.zoneId != INVALID_ZONE_ID) {
rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId));
}
// Serialize the Vector clock
VectorClock vc = (VectorClock) value.getVersion();
// doing the put.
if (vc != null) {
String serializedVC = null;
if (!vc.getEntries().isEmpty()) {
serializedVC = RestUtils.getSerializedVectorClock(vc);
}
if (serializedVC != null && serializedVC.length() > 0) {
rb.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, serializedVC);
}
}
RestRequest request = rb.build();
Future<RestResponse> f = client.restRequest(request);
// This will block
response = f.get();
String serializedUpdatedVC = response.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK);
if (serializedUpdatedVC == null || serializedUpdatedVC.length() == 0) {
if (logger.isDebugEnabled()) {
logger.debug("Received empty vector clock in the response");
}
} else {
VectorClock updatedVC = RestUtils.deserializeVectorClock(serializedUpdatedVC);
VectorClock originalVC = (VectorClock) value.getVersion();
originalVC.copyFromVectorClock(updatedVC);
}
final ByteString entity = response.getEntity();
if (entity == null) {
if (logger.isDebugEnabled()) {
logger.debug("Empty response !");
}
}
} catch (ExecutionException e) {
if (e.getCause() instanceof RestException) {
RestException exception = (RestException) e.getCause();
if (logger.isDebugEnabled()) {
logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
}
int httpErrorStatus = exception.getResponse().getStatus();
if (httpErrorStatus == BAD_REQUEST.getCode()) {
throw new VoldemortException("Bad request: " + e.getMessage(), e);
} else if (httpErrorStatus == PRECONDITION_FAILED.getCode()) {
throw new ObsoleteVersionException(e.getMessage());
} else if (httpErrorStatus == REQUEST_TIMEOUT.getCode() || httpErrorStatus == INTERNAL_SERVER_ERROR.getCode()) {
throw new InsufficientOperationalNodesException(e.getMessage());
}
}
throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
} catch (InterruptedException e) {
if (logger.isDebugEnabled()) {
logger.debug("Operation interrupted : " + e.getMessage());
}
throw new VoldemortException("Unknown Voldemort exception: " + e.getMessage());
} catch (URISyntaxException e) {
throw new VoldemortException("Illegal HTTP URL" + e.getMessage());
}
}
use of com.linkedin.r2.message.rest.RestResponse in project voldemort by voldemort.
the class R2Store method getVersions.
@Override
public List<Version> getVersions(ByteArray key) {
List<Version> resultList = new ArrayList<Version>();
String base64Key = RestUtils.encodeVoldemortKey(key.get());
RestRequestBuilder rb = null;
try {
rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key));
String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_VERSION_OP_CODE));
rb.setHeader(RestMessageHeaders.X_VOLD_GET_VERSION, "true");
RestResponse response = fetchGetResponse(rb, timeoutStr);
final ByteString entity = response.getEntity();
if (entity != null) {
resultList = parseGetVersionResponse(entity);
} else {
if (logger.isDebugEnabled()) {
logger.debug("Did not get any response!");
}
}
} catch (ExecutionException e) {
if (e.getCause() instanceof RestException) {
RestException exception = (RestException) e.getCause();
if (logger.isDebugEnabled()) {
logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
}
} else {
throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
}
} catch (InterruptedException e) {
if (logger.isDebugEnabled()) {
logger.debug("Operation interrupted : " + e.getMessage(), e);
}
throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e);
} catch (URISyntaxException e) {
throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
}
return resultList;
}
use of com.linkedin.r2.message.rest.RestResponse in project voldemort by voldemort.
the class R2Store method getAll.
@Override
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys, Map<ByteArray, byte[]> transforms) throws VoldemortException {
Map<ByteArray, List<Versioned<byte[]>>> resultMap = new HashMap<ByteArray, List<Versioned<byte[]>>>();
int numberOfKeys = 0;
try {
Iterator<ByteArray> it = keys.iterator();
StringBuilder keyArgs = null;
while (it.hasNext()) {
ByteArray key = it.next();
String base64Key = RestUtils.encodeVoldemortKey(key.get());
if (keyArgs == null) {
keyArgs = new StringBuilder();
keyArgs.append(base64Key);
} else {
keyArgs.append("," + base64Key);
}
numberOfKeys++;
}
// TODO a common way to handle getAll with any number of keys
if (numberOfKeys == 1) {
List<Versioned<byte[]>> resultList = new ArrayList<Versioned<byte[]>>();
it = keys.iterator();
ByteArray key = it.next();
byte[] singleKeyTransforms = null;
if (transforms != null) {
singleKeyTransforms = transforms.get(key);
}
resultList = this.get(key, singleKeyTransforms);
resultMap.put(key, resultList);
} else {
RestRequestBuilder rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + keyArgs.toString()));
rb.setMethod(GET);
rb.setHeader("Accept", MULTIPART_CONTENT_TYPE);
String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_ALL_OP_CODE));
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis()));
if (this.routingTypeCode != null) {
rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode);
}
if (this.zoneId != INVALID_ZONE_ID) {
rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId));
}
RestRequest request = rb.build();
Future<RestResponse> f = client.restRequest(request);
// This will block
RestResponse response = f.get();
// Parse the response
final ByteString entity = response.getEntity();
String contentType = response.getHeader(CONTENT_TYPE);
if (entity != null) {
if (contentType.equalsIgnoreCase(MULTIPART_CONTENT_TYPE)) {
resultMap = parseGetAllResults(entity);
} else {
if (logger.isDebugEnabled()) {
logger.debug("Did not receive a multipart response");
}
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Did not get any response!");
}
}
}
} catch (ExecutionException e) {
if (e.getCause() instanceof RestException) {
RestException exception = (RestException) e.getCause();
if (logger.isDebugEnabled()) {
logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
}
} else {
throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
}
} catch (InterruptedException e) {
if (logger.isDebugEnabled()) {
logger.debug("Operation interrupted : " + e.getMessage(), e);
}
throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e);
} catch (URISyntaxException e) {
throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
}
return resultMap;
}
use of com.linkedin.r2.message.rest.RestResponse in project voldemort by voldemort.
the class CoordinatorAdminClient method putStoreClientConfigString.
public boolean putStoreClientConfigString(String storeClientConfigAvro, String coordinatorUrl) {
String responseMessage = null;
Boolean success = false;
try {
// Create the REST request
StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl).append(URL_SEPARATOR).append(STORE_CLIENT_CONFIG_OPS).append(URL_SEPARATOR);
RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
byte[] payload = storeClientConfigAvro.getBytes("UTF-8");
// Create a HTTP POST request
requestBuilder.setMethod(requestType.POST.toString());
requestBuilder.setEntity(payload);
requestBuilder.setHeader(CONTENT_TYPE, "binary");
requestBuilder.setHeader(CONTENT_LENGTH, "" + payload.length);
String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.PUT_OP_CODE));
requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
requestBuilder = setCommonRequestHeader(requestBuilder);
RestRequest request = requestBuilder.build();
Future<RestResponse> future = client.restRequest(request);
// This will block
RestResponse response = future.get();
final ByteString entity = response.getEntity();
if (entity == null) {
if (logger.isDebugEnabled()) {
logger.debug("Empty response !");
}
responseMessage = "Received empty response from " + coordinatorUrl;
} else {
responseMessage = entity.asString("UTF-8");
success = true;
}
} catch (Exception e) {
if (e.getCause() instanceof RestException) {
responseMessage = ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8");
} else {
responseMessage = "An exception other than RestException happens!";
}
handleRequestAndResponseException(e);
} finally {
System.out.println(responseMessage);
}
return success;
}
Aggregations