use of com.herewhite.sdk.domain.ConvertException in project whiteboard-android by netless-io.
the class Converter method startConvertTask.
public void startConvertTask(final String url, final ConvertType type, final ConverterCallbacks callback) {
beginDate = new Date();
final Converter that = this;
poolExecutor.execute(new Runnable() {
@Override
public void run() {
final CountDownLatch latch = new CountDownLatch(1);
that.createConvertTask(url, type, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
that.status = ConverterStatus.CreateFail;
ConvertException convertE = new ConvertException(ConvertErrorCode.CreatedFail, e);
callback.onFailure(convertE);
latch.countDown();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
JsonObject json = gson.fromJson(response.body().string(), JsonObject.class);
if (response.code() == 200) {
JsonObject msg = json.getAsJsonObject("msg");
boolean succeed = msg.getAsJsonPrimitive("succeed").getAsBoolean();
if (succeed) {
that.status = ConverterStatus.Created;
that.taskId = json.getAsJsonObject("msg").get("taskUUID").getAsString();
} else {
that.status = ConverterStatus.CreateFail;
ConvertException e = new ConvertException(ConvertErrorCode.CreatedFail, gson.toJson(json));
callback.onFailure(e);
}
} else {
that.status = ConverterStatus.CreateFail;
ConvertException e = new ConvertException(ConvertErrorCode.ConvertFail, gson.toJson(json));
callback.onFailure(e);
}
latch.countDown();
}
});
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (that.status == ConverterStatus.CreateFail) {
return;
}
that.polling(taskId, type, new ConvertCallback() {
@Override
public void onConvertProgress(Double progress, ConversionInfo info) {
callback.onProgress(progress, info);
}
@Override
public void onConvertFinish(final ConversionInfo info) {
that.status = ConverterStatus.Success;
callback.onFinish(that.getPpt(info, type), info);
}
@Override
public void onConvertFailure(ConvertException e) {
// 错误时状态,各个上一级根据情况设置状态码
callback.onFailure(e);
}
});
}
});
}
use of com.herewhite.sdk.domain.ConvertException in project whiteboard-android by netless-io.
the class ConverterV5 method requestConvert.
// Step 1: 发起文档转换
private void requestConvert() {
Map<String, Object> roomSpec = new HashMap<>();
roomSpec.put("resource", resource);
roomSpec.put("type", convertType(type));
roomSpec.put("preview", preview);
if (type == ConvertType.Static) {
roomSpec.put("scale", scale);
roomSpec.put("outputFormat", convertOutputFormat(outputFormat));
roomSpec.put("pack", pack);
}
RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
Request request = new Request.Builder().url(PPT_BASE_URL).header("token", sdkToken).header("region", convertRegion(region)).header("Content-Type", "application/json").header("Accept", "application/json").post(body).build();
final Call call = client.newCall(request);
try {
Response response = call.execute();
if (response.code() >= 200 && response.code() <= 204) {
JsonObject jsonObject = (JsonObject) JsonParser.parseString(response.body().string());
taskUuid = jsonObject.get("uuid").getAsString();
type = parseType((jsonObject.get("type").getAsString()));
status = ConverterStatus.Created;
} else {
onFailure(new ConvertException(ConvertErrorCode.ConvertFail, response.body().string()));
status = ConverterStatus.CreateFail;
}
} catch (IOException e) {
onFailure(new ConvertException(ConvertErrorCode.CreatedFail, e));
status = ConverterStatus.CreateFail;
}
}
use of com.herewhite.sdk.domain.ConvertException in project whiteboard-android by netless-io.
the class ConverterV5 method startProgressLoop.
private void startProgressLoop(String token) {
long timeLimit = startTime + timeout;
try {
status = ConverterStatus.Checking;
while (System.currentTimeMillis() < timeLimit) {
if (status != ConverterStatus.Checking) {
return;
}
checkProgress(token);
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
}
}
onFailure(new ConvertException(ConvertErrorCode.CheckTimeout));
status = ConverterStatus.Timeout;
} catch (ConvertException e) {
onFailure(e);
status = ConverterStatus.Fail;
}
}
use of com.herewhite.sdk.domain.ConvertException in project whiteboard-android by netless-io.
the class ConverterV5 method checkProgress.
// Step 3: 轮询查询
private void checkProgress(String token) throws ConvertException {
Request request = new Request.Builder().url(String.format(PROGRESS_URL_FORMAT, taskUuid, convertType(type))).header("token", token).header("region", convertRegion(region)).header("Content-Type", "application/json").header("Accept", "application/json").build();
Call call = client.newCall(request);
try {
Response response = call.execute();
String body = response.body().string();
System.out.println(body);
if (response.code() == 200) {
QueryInfo queryInfo = gson.fromJson(body, QueryInfo.class);
ConversionInfo info = queryInfo.progress;
ConversionInfo.ServerConversionStatus status = queryInfo.status;
if (status == ConversionInfo.ServerConversionStatus.Fail || status == ConversionInfo.ServerConversionStatus.NotFound) {
ConvertErrorCode code = status == ConversionInfo.ServerConversionStatus.Fail ? ConvertErrorCode.ConvertFail : ConvertErrorCode.NotFound;
onFailure(new ConvertException(code, queryInfo.failedReason));
this.status = ConverterStatus.Fail;
} else if (status == ConversionInfo.ServerConversionStatus.Finished) {
onFinish(getPpt(info, type), info);
this.status = ConverterStatus.Success;
} else {
onProgress(info.getConvertedPercentage(), queryInfo.progress);
}
} else {
throw new ConvertException(ConvertErrorCode.ConvertFail, body);
}
} catch (IOException e) {
throw new ConvertException(ConvertErrorCode.CheckFail, e);
}
}
use of com.herewhite.sdk.domain.ConvertException in project whiteboard-android by netless-io.
the class ProjectorQuery method checkProgress.
private void checkProgress() throws ConvertException {
Request request = new Request.Builder().url(String.format(QUERY_URL_FORMAT, taskUuid)).header("token", taskToken).header("region", convertRegion(region)).header("Content-Type", "application/json").header("Accept", "application/json").build();
Call call = client.newCall(request);
try {
Response response = call.execute();
String body = response.body().string();
System.out.println("response is " + body);
if (response.code() == 200) {
QueryResponse queryResponse = gson.fromJson(body, QueryResponse.class);
ConversionStatus status = queryResponse.status;
if (status == ConversionStatus.Fail) {
onFailure(new ConvertException(ConvertErrorCode.ConvertFail, queryResponse.errorMessage));
this.status = ConverterStatus.Fail;
} else if (status == ConversionStatus.Finished) {
onFinish(queryResponse);
this.status = ConverterStatus.Success;
} else {
onProgress(queryResponse.convertedPercentage, queryResponse);
}
} else {
throw new ConvertException(ConvertErrorCode.ConvertFail, body);
}
} catch (IOException e) {
throw new ConvertException(ConvertErrorCode.CheckFail, e);
}
}
Aggregations