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);
}
}
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;
}
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);
}
}
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) {
}
});
}
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) {
}
});
}
Aggregations