Search in sources :

Example 71 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestClientStreamCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    Executor executor = Executors.newCachedThreadPool();
    ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
    int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    FutureCallback<ByteString> callback = new FutureCallback<>();
    FullEntityReader reader = new FullEntityReader(callback);
    HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength, reader);
    clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
    ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
    Assert.assertEquals(entityRead.length(), entityLength);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) FutureCallback(com.linkedin.common.callback.FutureCallback) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 72 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class ClientCompressionFilter method onRestResponse.

/**
 *  Decompresses server response
 */
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
    Boolean decompressionOff = (Boolean) requestContext.getLocalAttr(R2Constants.RESPONSE_DECOMPRESSION_OFF);
    if (decompressionOff == null || !decompressionOff) {
        try {
            // Check for header encoding
            String compressionHeader = res.getHeader(HttpConstants.CONTENT_ENCODING);
            // Compress if necessary
            if (compressionHeader != null && res.getEntity().length() > 0) {
                EncodingType encoding = null;
                try {
                    encoding = EncodingType.get(compressionHeader.trim().toLowerCase());
                } catch (IllegalArgumentException e) {
                    throw new CompressionException(CompressionConstants.SERVER_ENCODING_ERROR + compressionHeader);
                }
                if (!encoding.hasCompressor()) {
                    throw new CompressionException(CompressionConstants.SERVER_ENCODING_ERROR + compressionHeader);
                }
                ByteString inflated = encoding.getCompressor().inflate(res.getEntity());
                Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
                headers.putAll(res.getHeaders());
                headers.remove(HttpConstants.CONTENT_ENCODING);
                headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(inflated.length()));
                res = res.builder().setEntity(inflated).setHeaders(headers).build();
            }
        } catch (CompressionException e) {
            nextFilter.onError(e, requestContext, wireAttrs);
            return;
        }
    }
    nextFilter.onResponse(res, requestContext, wireAttrs);
}
Also used : ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) TreeMap(java.util.TreeMap)

Example 73 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class StreamWriter method doWrite.

/**
 * Attempts to write to the entity stream remaining chunks are available. Method must be executed
 * by the {@link ChannelHandlerContext}'s executor.
 */
private void doWrite() {
    refreshStreamLastActiveTime();
    while (_wh.remaining() > 0) {
        if (_buffer.isEmpty()) {
            break;
        }
        ByteString data = _buffer.remove(0);
        if (data == EOF) {
            _wh.done();
            _ctx.fireUserEventTriggered(ChannelPipelineEvent.RESPONSE_COMPLETE);
            return;
        }
        _wh.write(data);
        _bufferedBytes -= data.length();
        if (!_ctx.channel().config().isAutoRead() && _bufferedBytes < BUFFER_LOW_WATER_MARK) {
            _ctx.channel().config().setAutoRead(true);
        }
        refreshStreamLastActiveTime();
    }
}
Also used : ByteString(com.linkedin.data.ByteString)

Example 74 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class ResponseUtils method buildStreamException.

/**
 * @Deprecated: Use buildStreamException(RestLiResponseException, ContentType) method instead
 */
@Deprecated
public static StreamException buildStreamException(RestLiResponseException restLiResponseException, StreamDataCodec codec) {
    RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse();
    StreamResponseBuilder responseBuilder = new StreamResponseBuilder().setHeaders(restLiResponse.getHeaders()).setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())).setStatus(restLiResponse.getStatus() == null ? HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode() : restLiResponse.getStatus().getCode());
    EntityStream<ByteString> entityStream = codec.encodeMap(restLiResponse.getDataMap());
    StreamResponse response = responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream));
    return new StreamException(response, restLiResponseException.getCause());
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 75 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestRestLiServer method testValidReactiveUnstructuredDataRequest.

@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidReactiveUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
    StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveFeedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
    final FeedDownloadResourceReactive resource = getMockResource(FeedDownloadResourceReactive.class);
    resource.get(eq(1L), anyObject());
    EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResourceReactive()).once();
    replay(resource);
    @SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
    final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
    r2Callback.onSuccess(capture(streamResponse));
    expectLastCall().once();
    replay(r2Callback);
    RequestContext requestContext = new RequestContext();
    server.handleRequest(streamRequest, requestContext, r2Callback);
    verify(resource);
    verify(r2Callback);
    assertNotNull(streamResponse);
    assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResourceReactive.CONTENT_TYPE);
    FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            fail("Error inside callback!! Failed to read response data from stream!", e);
        }

        @Override
        public void onSuccess(ByteString result) {
            assertEquals(result, FeedDownloadResourceReactive.CONTENT);
        }
    });
    streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) FeedDownloadResourceReactive(com.linkedin.restli.server.twitter.FeedDownloadResourceReactive) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

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