use of co.cask.http.BodyConsumer in project cdap by caskdata.
the class StreamBodyConsumerTestBase method testChunkedContent.
@Test
public void testChunkedContent() throws Exception {
int recordCount = 1000;
// Generate a file for upload
ContentInfo contentInfo = generateFile(recordCount);
final Map<String, String> contentHeaders = Maps.newHashMap();
final TestContentWriter contentWriter = new TestContentWriter();
BodyConsumer bodyConsumer = createBodyConsumer(new ContentWriterFactory() {
@Override
public StreamId getStream() {
return new StreamId("test_namespace", "test-stream");
}
@Override
public ContentWriter create(Map<String, String> headers) throws IOException {
contentHeaders.putAll(headers);
return contentWriter;
}
});
TestHttpResponder responder = new TestHttpResponder();
// Feed the file content in small chunk
sendChunks(contentInfo.getContentSupplier(), 10, bodyConsumer, responder);
// Verify the processing is completed correctly
Assert.assertTrue(contentWriter.waitForClose(5, TimeUnit.SECONDS));
Assert.assertEquals(HttpResponseStatus.OK, responder.getResponseStatus());
Assert.assertEquals(recordCount, contentWriter.getEvents());
Assert.assertTrue(contentInfo.verify(contentHeaders, new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return new ByteBufferInputStream(contentWriter.getContent().duplicate());
}
}));
}
use of co.cask.http.BodyConsumer in project cdap by caskdata.
the class DefaultStreamManager method send.
@Override
public void send(File file, String contentType) throws Exception {
String path = String.format("/v3/namespaces/%s/streams/%s/batch", streamId.getNamespaceId(), streamId.getId());
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
request.setHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
final MockResponder responder = new MockResponder();
final BodyConsumer bodyConsumer = streamHandler.batch(request, responder, streamId.getNamespaceId(), streamId.getId());
Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from stream batch load call should not be null");
ByteStreams.readBytes(Files.newInputStreamSupplier(file), new ByteProcessor<BodyConsumer>() {
@Override
public boolean processBytes(byte[] buf, int off, int len) throws IOException {
bodyConsumer.chunk(ChannelBuffers.wrappedBuffer(buf, off, len), responder);
return true;
}
@Override
public BodyConsumer getResult() {
bodyConsumer.finished(responder);
return bodyConsumer;
}
});
Preconditions.checkState(HttpResponseStatus.OK.equals(responder.getStatus()), "Failed to load events to stream %s in batch", streamId);
}
use of co.cask.http.BodyConsumer in project cdap by caskdata.
the class AppFabricClient method deployApplication.
public Location deployApplication(Id.Namespace namespace, Class<?> applicationClz, String config, @Nullable KerberosPrincipalId ownerPrincipal, File... bundleEmbeddedJars) throws Exception {
Preconditions.checkNotNull(applicationClz, "Application cannot be null.");
Location deployedJar = AppJarHelper.createDeploymentJar(locationFactory, applicationClz, bundleEmbeddedJars);
LOG.info("Created deployedJar at {}", deployedJar);
String archiveName = String.format("%s-1.0.%d.jar", applicationClz.getSimpleName(), System.currentTimeMillis());
DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, String.format("/v3/namespaces/%s/apps", namespace.getId()));
request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
request.setHeader(AbstractAppFabricHttpHandler.ARCHIVE_NAME_HEADER, archiveName);
if (config != null) {
request.setHeader(AbstractAppFabricHttpHandler.APP_CONFIG_HEADER, config);
}
String owner = null;
if (ownerPrincipal != null) {
owner = GSON.toJson(ownerPrincipal, KerberosPrincipalId.class);
request.setHeader(AbstractAppFabricHttpHandler.PRINCIPAL_HEADER, owner);
}
MockResponder mockResponder = new MockResponder();
BodyConsumer bodyConsumer = appLifecycleHttpHandler.deploy(request, mockResponder, namespace.getId(), archiveName, config, owner, true);
Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from deploy call should not be null");
try (BufferFileInputStream is = new BufferFileInputStream(deployedJar.getInputStream(), 100 * 1024)) {
byte[] chunk = is.read();
while (chunk.length > 0) {
mockResponder = new MockResponder();
bodyConsumer.chunk(ChannelBuffers.wrappedBuffer(chunk), mockResponder);
Preconditions.checkState(mockResponder.getStatus() == null, "failed to deploy app");
chunk = is.read();
}
mockResponder = new MockResponder();
bodyConsumer.finished(mockResponder);
verifyResponse(HttpResponseStatus.OK, mockResponder.getStatus(), "Failed to deploy app");
}
return deployedJar;
}
use of co.cask.http.BodyConsumer in project cdap by caskdata.
the class AppFabricClient method createApplication.
private void createApplication(ApplicationId appId, DefaultHttpRequest request, AppRequest appRequest) throws Exception {
request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
request.setContent(ChannelBuffers.wrappedBuffer(Bytes.toBytes(GSON.toJson(appRequest.getConfig()))));
MockResponder mockResponder = new MockResponder();
BodyConsumer bodyConsumer = appLifecycleHttpHandler.createAppVersion(request, mockResponder, appId.getNamespace(), appId.getApplication(), appId.getVersion());
Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from deploy call should not be null");
byte[] contents = Bytes.toBytes(GSON.toJson(appRequest));
Preconditions.checkNotNull(contents);
bodyConsumer.chunk(ChannelBuffers.wrappedBuffer(contents), mockResponder);
bodyConsumer.finished(mockResponder);
verifyResponse(HttpResponseStatus.OK, mockResponder.getStatus(), "Failed to deploy app");
}
Aggregations