HttpHeaders headers = response.getHeaders();
URI taskURI = headers.getLocation();
String[] taskURIs = taskURI.toString().split("/");
String taskId = taskURIs[taskURIs.length - 1];
TaskRead taskRead;
int oldProgress = 0;
Status oldTaskStatus = null;
Status taskStatus = null;
int progress = 0;
do {
ResponseEntity<TaskRead> taskResponse =
restGetById(Constants.REST_PATH_TASK, taskId, TaskRead.class,
false);
//task will not return exception as it has status
taskRead = taskResponse.getBody();
progress = (int) (taskRead.getProgress() * 100);
taskStatus = taskRead.getStatus();
//fix cluster deletion exception
Type taskType = taskRead.getType();
if ((taskType == Type.DELETE) && (taskStatus == TaskRead.Status.COMPLETED)) {
clearScreen();
System.out.println(taskStatus + " " + progress + "%\n");
break;
}
if ((prettyOutput != null && prettyOutput.length > 0 && (taskRead.getType() == Type.VHM ? prettyOutput[0]
.isRefresh(true) : prettyOutput[0].isRefresh(false)))
|| oldTaskStatus != taskStatus
|| oldProgress != progress) {
//clear screen and show progress every few seconds
clearScreen();
//output completed task summary first in the case there are several related tasks
if (prettyOutput != null && prettyOutput.length > 0
&& prettyOutput[0].getCompletedTaskSummary() != null) {
for (String summary : prettyOutput[0]
.getCompletedTaskSummary()) {
System.out.println(summary + "\n");
}
}
System.out.println(taskStatus + " " + progress + "%\n");
if (prettyOutput != null && prettyOutput.length > 0) {
// print call back customize the detailed output case by case
prettyOutput[0].prettyOutput();
}
if (oldTaskStatus != taskStatus || oldProgress != progress) {
oldTaskStatus = taskStatus;
oldProgress = progress;
if (taskRead.getProgressMessage() != null) {
System.out.println(taskRead.getProgressMessage());
}
}
}
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException ex) {
//ignore
}
} while (taskStatus != TaskRead.Status.COMPLETED
&& taskStatus != TaskRead.Status.FAILED
&& taskStatus != TaskRead.Status.ABANDONED
&& taskStatus != TaskRead.Status.STOPPED);
String errorMsg = taskRead.getErrorMessage();
if (!taskRead.getStatus().equals(TaskRead.Status.COMPLETED)) {
throw new CliRestException(errorMsg);
} else { //completed
if (taskRead.getType().equals(Type.VHM)) {
logger.info("task type is vhm");
Thread.sleep(5*1000);
if (prettyOutput != null && prettyOutput.length > 0
&& prettyOutput[0].isRefresh(true)) {
//clear screen and show progress every few seconds