Search in sources :

Example 6 with ConvertException

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);
                }
            });
        }
    });
}
Also used : Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) Response(okhttp3.Response) Callback(okhttp3.Callback) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo)

Example 7 with ConvertException

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;
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) HashMap(java.util.HashMap) Request(okhttp3.Request) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 8 with ConvertException

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;
    }
}
Also used : ConvertException(com.herewhite.sdk.domain.ConvertException)

Example 9 with ConvertException

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);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) ConvertErrorCode(com.herewhite.sdk.domain.ConvertErrorCode) Request(okhttp3.Request) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) IOException(java.io.IOException)

Example 10 with ConvertException

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);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

ConvertException (com.herewhite.sdk.domain.ConvertException)13 ConversionInfo (com.herewhite.sdk.domain.ConversionInfo)9 IOException (java.io.IOException)6 ConverterCallbacks (com.herewhite.sdk.ConverterCallbacks)5 ConvertedFiles (com.herewhite.sdk.domain.ConvertedFiles)5 Call (okhttp3.Call)5 Response (okhttp3.Response)5 Converter (com.herewhite.sdk.Converter)4 Request (okhttp3.Request)4 JsonObject (com.google.gson.JsonObject)3 MapBuilder (com.herewhite.demo.utils.MapBuilder)3 ConverterV5 (com.herewhite.sdk.converter.ConverterV5)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ConvertErrorCode (com.herewhite.sdk.domain.ConvertErrorCode)2 Date (java.util.Date)2 Callback (okhttp3.Callback)2 HashMap (java.util.HashMap)1 RequestBody (okhttp3.RequestBody)1