Search in sources :

Example 16 with ErrorOutPut

use of com.fanap.podchat.model.ErrorOutPut in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method mainUploadImageFileMsg.

private void mainUploadImageFileMsg(LFileUpload fileUpload) {
    JsonObject jsonLog = new JsonObject();
    String description = fileUpload.getDescription();
    jsonLog.addProperty("description", description);
    ProgressHandler.sendFileMessage handler = fileUpload.getHandler();
    Integer messageType = fileUpload.getMessageType();
    jsonLog.addProperty("messageType", messageType);
    long threadId = fileUpload.getThreadId();
    jsonLog.addProperty("threadId", threadId);
    String uniqueId = fileUpload.getUniqueId();
    jsonLog.addProperty("uniqueId", uniqueId);
    String systemMetaData = fileUpload.getSystemMetaData();
    jsonLog.addProperty("systemMetaData", systemMetaData);
    long messageId = fileUpload.getMessageId();
    jsonLog.addProperty("messageId", messageId);
    String mimeType = fileUpload.getMimeType();
    jsonLog.addProperty("mimeType", mimeType);
    String methodName = fileUpload.getMethodName();
    jsonLog.addProperty("methodName", methodName);
    long fileSize = fileUpload.getFileSize();
    jsonLog.addProperty("fileSize", fileSize);
    String center = fileUpload.getCenter();
    jsonLog.addProperty("center", center);
    File file = fileUpload.getFile();
    if (chatReady) {
        if (fileServer != null) {
            RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
            FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
            try {
                showLog("UPLOAD_FILE_TO_SERVER", getJsonForLog(jsonLog));
            } catch (Exception e) {
                showErrorLog(e.getMessage());
            }
            ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {

                @Override
                public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
                    Log.i(TAG, "on progress");
                    if (handler != null) {
                        handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
                        if (log)
                            Log.i(TAG, "uniqueId " + uniqueId + " bytesSent " + progress);
                    } else {
                        if (log)
                            Log.i(TAG, "Handler is null");
                    }
                }

                @Override
                public void onError() {
                    if (log)
                        Log.i(TAG, "Error on upload");
                }

                @Override
                public void onFinish() {
                    if (log)
                        Log.i(TAG, "Finish upload");
                }
            });
            MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
            RequestBody name = RequestBody.create(MediaType.parse("text/plain"), file.getName());
            Observable<Response<FileImageUpload>> uploadObservable = fileApi.sendImageFile(body, getToken(), TOKEN_ISSUER, name);
            Subscription subscribe = uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fileUploadResponse -> {
                if (fileUploadResponse.body() != null && fileUploadResponse.isSuccessful()) {
                    boolean hasError = fileUploadResponse.body().isHasError();
                    if (hasError) {
                        String errorMessage = fileUploadResponse.body().getMessage();
                        int errorCode = fileUploadResponse.body().getErrorCode();
                        String jsonError = captureError(errorMessage, errorCode, uniqueId);
                        // listenerManager.callOnLogEvent(jsonError);
                        ErrorOutPut error = new ErrorOutPut(true, errorMessage, errorCode, uniqueId);
                        if (handler != null) {
                            handler.onError(jsonError, error);
                        }
                    } else {
                        // remove from Uploading Queue
                        removeFromUploadQueue(uniqueId);
                        ResultImageFile result = fileUploadResponse.body().getResult();
                        long imageId = result.getId();
                        String hashCode = result.getHashCode();
                        ChatResponse<ResultImageFile> chatResponse = new ChatResponse<>();
                        ResultImageFile resultImageFile = new ResultImageFile();
                        chatResponse.setUniqueId(uniqueId);
                        resultImageFile.setId(result.getId());
                        resultImageFile.setHashCode(result.getHashCode());
                        resultImageFile.setName(result.getName());
                        resultImageFile.setHeight(result.getHeight());
                        resultImageFile.setWidth(result.getWidth());
                        resultImageFile.setActualHeight(result.getActualHeight());
                        resultImageFile.setActualWidth(result.getActualWidth());
                        chatResponse.setResult(resultImageFile);
                        String imageJson = gson.toJson(chatResponse);
                        listenerManager.callOnUploadImageFile(imageJson, chatResponse);
                        if (handler != null) {
                            handler.onFinishImage(imageJson, chatResponse);
                        }
                        showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                        // if (log) Log.i(TAG, "RECEIVE_UPLOAD_IMAGE");
                        // listenerManager.callOnLogEvent(imageJson);
                        String metaJson;
                        if (!Util.isNullOrEmpty(methodName) && methodName.equals(ChatConstant.METHOD_LOCATION_MSG)) {
                            metaJson = createImageMetadata(file, hashCode, imageId, result.getActualHeight(), result.getActualWidth(), mimeType, fileSize, null, true, center);
                        } else {
                            metaJson = createImageMetadata(file, hashCode, imageId, result.getActualHeight(), result.getActualWidth(), mimeType, fileSize, null, false, null);
                        }
                        // 
                        JsonObject js = new JsonObject();
                        js.addProperty("metadata", metaJson);
                        js.addProperty("uniqueId", uniqueId);
                        // send to handler
                        if (handler != null) {
                            handler.onFinishImage(imageJson, chatResponse);
                        }
                        if (isReplyMessage(methodName)) {
                            showLog("SEND_REPLY_FILE_MESSAGE", getJsonForLog(js));
                            mainReplyMessage(description, threadId, messageId, systemMetaData, messageType, metaJson, uniqueId, null);
                        // if (log) Log.i(TAG, "SEND_REPLY_FILE_MESSAGE");
                        // listenerManager.callOnLogEvent(metaJson);
                        } else {
                            sendTextMessageWithFile(description, threadId, metaJson, systemMetaData, uniqueId, typeCode, messageType);
                        }
                    // listenerManager.callOnLogEvent(metaJson);
                    }
                }
            }, throwable -> {
                try {
                    String jsonError = captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
                    ErrorOutPut error = new ErrorOutPut(true, throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
                    if (log)
                        Log.e(TAG, jsonError);
                    if (handler != null) {
                        handler.onError(jsonError, error);
                    }
                } catch (Exception e) {
                    showErrorLog(e.getMessage());
                    onUnknownException(uniqueId, e);
                }
            });
            /*
                 * Cancel Upload request
                 * */
            initCancelUpload(uniqueId, subscribe);
        } else {
            if (log)
                Log.e(TAG, "FileServer url Is null");
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) RetrofitHelperFileServer(com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer) ResultImageFile(com.fanap.podchat.model.ResultImageFile) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) ProgressHandler(com.fanap.podchat.ProgressHandler) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) MultipartBody(okhttp3.MultipartBody) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile)

Example 17 with ErrorOutPut

use of com.fanap.podchat.model.ErrorOutPut in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method captureError.

protected String captureError(PodChatException exception) {
    ErrorOutPut error = new ErrorOutPut(true, exception.getMessage(), exception.getCode(), exception.getUniqueId());
    String jsonError = gson.toJson(error);
    listenerManager.callOnError(jsonError, error);
    showErrorLog(jsonError);
    if (sentryLog) {
        SentryEvent event = new SentryEvent(exception);
        event.setEnvironment("PODCHAT");
        event.setLevel(SentryLevel.ERROR);
        event.setTag("FROM_SDK", "PODCHAT");
        event.setExtra("CACHE ENABLED", " >>> " + cache);
        Sentry.captureEvent(event, error);
    }
    return jsonError;
}
Also used : SentryEvent(io.sentry.core.SentryEvent) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut)

Example 18 with ErrorOutPut

use of com.fanap.podchat.model.ErrorOutPut in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method mainUploadFileMessage.

private void mainUploadFileMessage(LFileUpload lFileUpload) {
    JsonObject jsonLog = new JsonObject();
    String description = lFileUpload.getDescription();
    jsonLog.addProperty("description", description);
    Integer messageType = lFileUpload.getMessageType();
    jsonLog.addProperty("messageType", messageType);
    long threadId = lFileUpload.getThreadId();
    jsonLog.addProperty("threadId", threadId);
    String uniqueId = lFileUpload.getUniqueId();
    jsonLog.addProperty("uniqueId", uniqueId);
    String systemMetadata = lFileUpload.getSystemMetaData();
    jsonLog.addProperty("systemMetadata", systemMetadata);
    long messageId = lFileUpload.getMessageId();
    jsonLog.addProperty("messageId", messageId);
    String mimeType = lFileUpload.getMimeType();
    jsonLog.addProperty("mimeType", mimeType);
    File file = lFileUpload.getFile();
    ProgressHandler.sendFileMessage handler = lFileUpload.getHandler();
    long file_size = lFileUpload.getFileSize();
    jsonLog.addProperty("file_size", file_size);
    String methodName = lFileUpload.getMethodName();
    jsonLog.addProperty("methodName", methodName);
    jsonLog.addProperty("name", file.getName());
    showLog("UPLOADING_FILE_TO_SERVER", getJsonForLog(jsonLog));
    if (chatReady) {
        if (getFileServer() != null) {
            RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
            FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
            RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
            ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {

                @Override
                public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
                    if (handler != null)
                        handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
                }
            });
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
            Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(body, getToken(), TOKEN_ISSUER, name);
            Subscription subscription = uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fileUploadResponse -> {
                if (fileUploadResponse.isSuccessful() && fileUploadResponse.body() != null) {
                    boolean error = fileUploadResponse.body().isHasError();
                    if (error) {
                        String errorMessage = fileUploadResponse.body().getMessage();
                        if (log)
                            Log.e(TAG, errorMessage);
                        if (handler != null) {
                            String jsonError = captureError(ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                            ErrorOutPut errorOutPut = new ErrorOutPut(true, ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                            handler.onError(jsonError, errorOutPut);
                        }
                    } else {
                        // remove from Uploading Queue
                        removeFromUploadQueue(uniqueId);
                        ResultFile result = fileUploadResponse.body().getResult();
                        if (result != null) {
                            long fileId = result.getId();
                            String hashCode = result.getHashCode();
                            ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
                            chatResponse.setResult(result);
                            chatResponse.setUniqueId(uniqueId);
                            result.setSize(file_size);
                            String json = gson.toJson(chatResponse);
                            showLog("FILE_UPLOADED_TO_SERVER", json);
                            listenerManager.callOnUploadFile(json, chatResponse);
                            if (handler != null) {
                                handler.onFinishFile(json, chatResponse);
                            }
                            String jsonMeta = createFileMetadata(file, hashCode, fileId, mimeType, file_size, "");
                            if (!Util.isNullOrEmpty(methodName) && methodName.equals(ChatConstant.METHOD_REPLY_MSG)) {
                                showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                                mainReplyMessage(description, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                            } else {
                                sendTextMessageWithFile(description, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                            }
                        }
                    }
                }
            }, throwable -> {
                if (log)
                    Log.e(TAG, throwable.getMessage());
            });
            /*
                 * Cancel Uploading progress
                 * */
            initCancelUpload(uniqueId, subscription);
        } else {
            if (log)
                Log.e(TAG, "FileServer url Is null");
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    // listenerManager.callOnLogEvent(jsonError);
    }
}
Also used : ProgressHandler(com.fanap.podchat.ProgressHandler) JsonObject(com.google.gson.JsonObject) ResultFile(com.fanap.podchat.model.ResultFile) RetrofitHelperFileServer(com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) MultipartBody(okhttp3.MultipartBody) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody)

Example 19 with ErrorOutPut

use of com.fanap.podchat.model.ErrorOutPut in project pod-chat-android-sdk by FanapSoft.

the class ChatTest method uploadProgressImage.

@Test
@MediumTest
public void uploadProgressImage() {
    sleep(3000);
    Uri uri = Uri.parse("content://media/external/images/media/781");
    presenter.uploadImageProgress(appContext, activity, uri, new ProgressHandler.onProgress() {

        @Override
        public void onProgressUpdate(int progress) {
            Mockito.anyInt();
        }

        @Override
        public void onFinish(String imageJson, ChatResponse<ResultImageFile> chatResponse) {
        }

        @Override
        public void onError(String jsonError, ErrorOutPut error) {
        }
    });
}
Also used : ResultImageFile(com.fanap.podchat.model.ResultImageFile) ProgressHandler(com.fanap.podchat.ProgressHandler) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Uri(android.net.Uri) FlakyTest(android.support.test.filters.FlakyTest) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test) LargeTest(android.support.test.filters.LargeTest) MediumTest(android.support.test.filters.MediumTest)

Example 20 with ErrorOutPut

use of com.fanap.podchat.model.ErrorOutPut in project pod-chat-android-sdk by FanapSoft.

the class ChatTestIntegration method uploadProgressImage.

@Test
@MediumTest
public void uploadProgressImage() {
    sleep(3000);
    Uri uri = Uri.parse("content://media/external/images/media/781");
    presenter.uploadImageProgress(appContext, activity, uri, new ProgressHandler.onProgress() {

        @Override
        public void onProgressUpdate(int progress) {
            Mockito.anyInt();
        }

        @Override
        public void onFinish(String imageJson, ChatResponse<ResultImageFile> chatResponse) {
        }

        @Override
        public void onError(String jsonError, ErrorOutPut error) {
        }
    });
}
Also used : ResultImageFile(com.fanap.podchat.model.ResultImageFile) ProgressHandler(com.fanap.podchat.ProgressHandler) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Uri(android.net.Uri) FlakyTest(android.support.test.filters.FlakyTest) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test) LargeTest(android.support.test.filters.LargeTest) MediumTest(android.support.test.filters.MediumTest)

Aggregations

ErrorOutPut (com.fanap.podchat.model.ErrorOutPut)26 ResultImageFile (com.fanap.podchat.model.ResultImageFile)16 ChatResponse (com.fanap.podchat.model.ChatResponse)12 ResultFile (com.fanap.podchat.model.ResultFile)12 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)12 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)12 PodChatException (com.fanap.podchat.util.PodChatException)12 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)11 MetaDataFile (com.fanap.podchat.model.MetaDataFile)11 RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)11 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)11 File (java.io.File)11 Subscription (rx.Subscription)11 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)10 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SentryException (io.sentry.core.protocol.SentryException)10 IOException (java.io.IOException)10 JSONException (org.json.JSONException)10 UploadToPodSpaceResult (com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult)9 ProgressHandler (com.fanap.podchat.ProgressHandler)7