use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestServerTimeout method testServerTimeout.
@Test
public void testServerTimeout() throws Exception {
final StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger status = new AtomicInteger(-1);
_client.streamRequest(request, new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
status.set(result.getStatus());
result.getEntityStream().setReader(new Reader() {
private ReadHandle _rh;
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(Integer.MAX_VALUE);
}
@Override
public void onDataAvailable(ByteString data) {
// do nothing
}
@Override
public void onDone() {
// server would close the connection if TimeoutException, and netty would end the chunked transferring
// with an empty chunk
latch.countDown();
}
@Override
public void onError(Throwable e) {
latch.countDown();
}
});
}
});
// server should timeout so await should return true
Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
Assert.assertEquals(status.get(), RestStatus.OK);
}
use of com.linkedin.data.ByteString in project incubator-gobblin by apache.
the class HttpTestUtils method assertEqual.
public static void assertEqual(RestRequestBuilder actual, RestRequestBuilder expect) throws IOException {
// Check entity
ByteString actualEntity = actual.getEntity();
ByteString expectedEntity = expect.getEntity();
if (actualEntity == null) {
Assert.assertTrue(expectedEntity == null);
} else {
Assert.assertEquals(actualEntity.length(), expectedEntity.length());
Assert.assertEquals(actualEntity.asString(StandardCharsets.UTF_8), expectedEntity.asString(StandardCharsets.UTF_8));
}
// Check request
RestRequest actualRequest = actual.build();
RestRequest expectedRequest = expect.build();
Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod());
Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString());
Map<String, String> actualHeaders = actualRequest.getHeaders();
Map<String, String> expectedHeaders = expectedRequest.getHeaders();
Assert.assertEquals(actualHeaders.size(), expectedHeaders.size());
for (String key : actualHeaders.keySet()) {
Assert.assertEquals(actualHeaders.get(key), expectedHeaders.get(key));
}
}
use of com.linkedin.data.ByteString in project incubator-gobblin by apache.
the class R2Request method toString.
@Override
public String toString() {
RestRequest request = getRawRequest();
StringBuilder outBuffer = new StringBuilder();
String endl = "\n";
outBuffer.append("R2Request Info").append(endl);
outBuffer.append("type: RestRequest").append(endl);
outBuffer.append("uri: ").append(request.getURI().toString()).append(endl);
outBuffer.append("headers: ");
request.getHeaders().forEach((k, v) -> outBuffer.append("[").append(k).append(":").append(v).append("] "));
outBuffer.append(endl);
ByteString entity = request.getEntity();
if (entity != null) {
outBuffer.append("body: ").append(entity.asString(Charset.defaultCharset())).append(endl);
}
return outBuffer.toString();
}
use of com.linkedin.data.ByteString 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.data.ByteString in project voldemort by voldemort.
the class R2Store method parseGetVersionResponse.
private List<Version> parseGetVersionResponse(ByteString entity) {
byte[] bytes = new byte[entity.length()];
entity.copyBytes(bytes, 0);
String vectorClockListStr = new String(bytes);
List<Version> vectorClockList = RestUtils.deserializeVectorClocks(vectorClockListStr);
return vectorClockList;
}
Aggregations