public static <T> EntityStream<T> emptyStream() { return newEntityStream(new Writer<T>() { private WriteHandle<? super T> _wh; @Override public void onInit(WriteHandle<? super T> wh) { _wh = wh; } @Override public void onWritePossible() { _wh.done(); } @Override public void onAbort(Throwable e) { // do nothing } }); }
public static EntityStream emptyStream() { return EntityStreamAdapters.fromGenericEntityStream(com.linkedin.entitystream.EntityStreams.emptyStream()); }
@Override protected StreamResponse convertResponse(RestLiResponse restLiResponse) throws Exception { StreamResponseBuilder responseBuilder = new StreamResponseBuilder() .setHeaders(restLiResponse.getHeaders()) .setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())) .setStatus(restLiResponse.getStatus().getCode()); EntityStream<ByteString> entityStream = _context.getResponseEntityStream(); if (entityStream != null) { // Unstructured data response // Content-Type is required if (restLiResponse.getHeaders().get(RestConstants.HEADER_CONTENT_TYPE) == null) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Type is missing."); } } else { entityStream = EntityStreams.emptyStream(); } return responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream)); }
@Test public void testWrite() throws ExecutionException, InterruptedException { String singleton = "singleton"; Writer<Object> singletonWriter = new SingletonWriter<>(singleton); EntityStream<Object> singletonStream = EntityStreams.newEntityStream(singletonWriter); CollectingReader<Object, ?, Set<Object>> reader = new CollectingReader<>(Collectors.toSet()); singletonStream.setReader(reader); Assert.assertEquals(reader.getResult().toCompletableFuture().get(), Collections.singleton(singleton)); } }
@Override protected StreamResponse convertResponse(RestLiResponse restLiResponse) throws Exception { StreamResponseBuilder responseBuilder = new StreamResponseBuilder() .setHeaders(restLiResponse.getHeaders()) .setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())) .setStatus(restLiResponse.getStatus().getCode()); EntityStream<ByteString> entityStream; if (restLiResponse.hasData()) { responseBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, _contentType.getHeaderKey()); entityStream = _contentType.getStreamCodec().encodeMap(restLiResponse.getDataMap()); } else { entityStream = EntityStreams.emptyStream(); } return responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream)); }
@Test(dataProvider = "data") public void testWrite(int chunkSize) throws InterruptedException, ExecutionException, TimeoutException { Writer<ByteString> writer = new ChunkedByteStringWriter(DATA, chunkSize); EntityStream<ByteString> stream = EntityStreams.newEntityStream(writer); CollectingReader<ByteString, ?, ChunkedByteStringCollector.Result> reader = new CollectingReader<>(new ChunkedByteStringCollector()); stream.setReader(reader); ChunkedByteStringCollector.Result result = reader.getResult().toCompletableFuture().get(); Assert.assertEquals(result.data, DATA); Assert.assertEquals(result.chunkCount, (DATA.length - 1) / chunkSize + 1); } }
@Override public EntityStream<ByteString> encodeMap(DataMap map) { return EntityStreams.newEntityStream(new JacksonSmileDataEncoder(_smileFactory, map, _bufferSize)); }
@Override public EntityStream<ByteString> encodeList(DataList list) { JacksonJsonDataEncoder encoder = new JacksonJsonDataEncoder(list, _bufferSize); return EntityStreams.newEntityStream(encoder); } }
@Override public EntityStream<ByteString> encodeMap(DataMap map) { JacksonJsonDataEncoder encoder = new JacksonJsonDataEncoder(map, _bufferSize); return EntityStreams.newEntityStream(encoder); }
@Override public EntityStream<ByteString> encodeList(DataList list) { return EntityStreams.newEntityStream(new JacksonSmileDataEncoder(_smileFactory, list, _bufferSize)); } }
/** * The method to create a new EntityStream with a writer for the stream * * @param writer the writer for the stream who would provide the data * @return an instance of EntityStream */ public static EntityStream newEntityStream(Writer writer) { return EntityStreamAdapters.fromGenericEntityStream( com.linkedin.entitystream.EntityStreams.newEntityStream(EntityStreamAdapters.toGenericWriter(writer))); } }
@Override public void get(String key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) { if (key.equals("callbackError")) { callback.onError(new NoPermissionException("missing access permission")); return; } Writer<ByteString> writer = chooseGreetingWriter(key); String contentType; if (key.equals("goodNullContentType")) { contentType = null; } else { contentType = MIME_TYPE; } UnstructuredDataReactiveResult result = new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), contentType); callback.onSuccess(result); }
@Override public void get(CompoundKey key, @CallbackParam Callback<UnstructuredDataReactiveResult> callback) { Writer<ByteString> writer = new SingletonWriter<>(ByteString.copy(UNSTRUCTURED_DATA_BYTES)); callback.onSuccess(new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), MIME_TYPE)); }
@Override public void get(@CallbackParam Callback<UnstructuredDataReactiveResult> callback) { Writer<ByteString> writer = new SingletonWriter<>(ByteString.copy(UNSTRUCTURED_DATA_BYTES)); callback.onSuccess(new UnstructuredDataReactiveResult(EntityStreams.newEntityStream(writer), MIME_TYPE)); }
context.setResponseEntityStream(EntityStreams.newEntityStream(new ByteArrayOutputStreamWriter(out)));