Search in sources :

Example 1 with ByteString

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);
}
Also used : ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) Reader(com.linkedin.r2.message.stream.entitystream.Reader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 2 with ByteString

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));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString)

Example 3 with ByteString

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();
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString)

Example 4 with ByteString

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;
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VoldemortException(voldemort.VoldemortException) Version(voldemort.versioning.Version) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ByteString

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;
}
Also used : Version(voldemort.versioning.Version) ByteString(com.linkedin.data.ByteString)

Aggregations

ByteString (com.linkedin.data.ByteString)152 Test (org.testng.annotations.Test)77 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 MimeMultipart (javax.mail.internet.MimeMultipart)31 MimeBodyPart (javax.mail.internet.MimeBodyPart)26 DataMap (com.linkedin.data.DataMap)25 RestResponse (com.linkedin.r2.message.rest.RestResponse)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)22 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)22 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)21 URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 RequestContext (com.linkedin.r2.message.RequestContext)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 Callback (com.linkedin.common.callback.Callback)17 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)14 RestException (com.linkedin.r2.message.rest.RestException)12 HashMap (java.util.HashMap)12 DataList (com.linkedin.data.DataList)11