use of co.cask.cdap.internal.app.BufferFileInputStream 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;
}
Aggregations