public void onSubmitComplete(SubmitCompleteEvent event) { updateStatusTimer.cancel(); onSubmitComplete = true; serverRawResponse = event.getResults(); if (serverRawResponse != null) { serverRawResponse = serverRawResponse.replaceFirst( ".*" + TAG_MSG_START + "([\\s\\S]*?)" + TAG_MSG_END + ".*", "$1"); serverRawResponse = serverRawResponse .replace(TAG_MSG_LT, "<") .replace(TAG_MSG_GT, ">") .replace("<", "<") .replaceAll(">", ">") .replaceAll(" ", " "); } try { // Parse the xml and extract UploadedInfos Document doc = XMLParser.parse(serverRawResponse); // If the server response is a valid xml parseAjaxResponse(serverRawResponse); } catch (Exception e) { log("onSubmitComplete exception parsing response (Check CORS and XML syntax): ", e); // Otherwise force an ajax request so as we have not to wait to the timer schedule // updateStatusTimer.run(); // how could the upload server response be corrupted ?? This // causes an error loop in my project with firefox } }
public void onError(Request request, Throwable exception) { waitingForResponse = false; if (exception instanceof RequestTimeoutException) { log( "GWTUpload: onStatusReceivedCallback timeout error, asking the server again.", null); } else { log("GWTUpload: onStatusReceivedCallback error: " + exception.getMessage(), exception); updateStatusTimer.cancel(); String message = removeHtmlTags(exception.getMessage()); message += "\n" + exception.getClass().getName(); message += "\n" + exception.toString(); statusWidget.setError( i18nStrs.uploaderServerUnavailable() + " (4) " + getServletPath() + "\n\n" + message); } }
public static void log(String msg, Throwable e) { if (mlog == null) { if (Window.Location.getParameter("log") != null) { mlog = new HTML(); RootPanel.get().add(mlog); log(msg, e); } else { if (logger == null) { logger = Logger.getLogger("Gwt client Uploader"); } logger.info(msg); GWT.log(msg, e); } } else { String html = (msg + "\n" + (e != null ? e.getMessage() : "")).replaceAll("\n", "<br/>"); mlog.setHTML(mlog.getHTML() + html); } }
/** Cancel the current upload process. */ public void cancel() { if (getStatus() == Status.UNINITIALIZED) { return; } if (finished && !uploading) { if (successful) { try { sendAjaxRequestToDeleteUploadedFile(); } catch (Exception e) { } } else { statusWidget.setStatus(Status.DELETED); } return; } if (canceled || getStatus() == Status.CANCELING) { return; } canceled = true; automaticUploadTimer.cancel(); if (uploading) { updateStatusTimer.cancel(); try { sendAjaxRequestToCancelCurrentUpload(); } catch (Exception e) { log("Exception cancelling request " + e.getMessage(), e); } statusWidget.setStatus(IUploadStatus.Status.CANCELING); } else { uploadFinished(); reuse(); } }
public void onError(Request request, Throwable exception) { statusWidget.setStatus(Status.DELETED); log("onCancelReceivedCallback onError: ", exception); }
public void onError(Request request, Throwable exception) { log("onCancelReceivedCallback onError: ", exception); statusWidget.setStatus(IUploadStatus.Status.CANCELED); }
private void parseAjaxResponse(String responseTxt) { if (responseTxt == null) { return; } String error = null; Document doc = null; try { doc = XMLParser.parse(responseTxt); error = Utils.getXmlNodeValue(doc, "error"); if (error == null) { // Response brings uploaded files info in either: // POST response or FINISHED status String msg = Utils.getXmlNodeValue(doc, TAG_MESSAGE); serverMessage.setMessage(msg); String fld = Utils.getXmlNodeValue(doc, TAG_FIELD); NodeList list = doc.getElementsByTagName(TAG_FILE); for (int i = 0, l = list.getLength(); i < l; i++) { UploadedInfo info = new UploadedInfo(); info.setField(getInputName() + "-" + i); info.setName(Utils.getXmlNodeValue(doc, TAG_NAME, i)); info.setCtype(Utils.getXmlNodeValue(doc, TAG_CTYPE, i)); // TODO: test info.setKey(Utils.getXmlNodeValue(doc, TAG_KEY, i)); // TODO: remove info.message = msg; String url = session.composeURL(PARAM_SHOW + "=" + info.getField()); if (info.getKey() != null) { url += "&" + PARAM_BLOBKEY + "=" + info.getKey(); } info.setFileUrl(url); String size = Utils.getXmlNodeValue(doc, TAG_SIZE, i); if (size != null) { info.setSize(Integer.parseInt(size)); } serverMessage.getUploadedInfos().add(info); } } } catch (Exception e) { if (responseTxt.toLowerCase().matches("error")) { error = i18nStrs.uploaderServerError() + "\nAction: " + getServletPath() + "\nException: " + e.getMessage() + responseTxt; } } if (error != null) { successful = false; cancelUpload(error); return; } else if (Utils.getXmlNodeValue(doc, TAG_WAIT) != null) { if (serverRawResponse != null) { log( "server response received, cancelling the upload " + getFileNames() + " " + serverRawResponse, null); successful = true; uploadFinished(); } } else if (Utils.getXmlNodeValue(doc, TAG_CANCELED) != null) { log("server response is: canceled " + getFileNames(), null); successful = false; canceled = true; uploadFinished(); return; } else if (Utils.getXmlNodeValue(doc, TAG_FINISHED) != null) { log("server response is: finished " + serverMessage.getUploadedFileNames(), null); successful = true; if (onSubmitComplete) { log("POST response from server has been received", null); uploadFinished(); } return; } else if (Utils.getXmlNodeValue(doc, TAG_PERCENT) != null) { lastData = now(); long transferredKB = Long.valueOf(Utils.getXmlNodeValue(doc, TAG_CURRENT_BYTES)) / 1024; long totalKB = Long.valueOf(Utils.getXmlNodeValue(doc, TAG_TOTAL_BYTES)) / 1024; statusWidget.setProgress(transferredKB, totalKB); log( "server response transferred " + transferredKB + "/" + totalKB + " " + getFileNames(), null); if (onSubmitComplete) { successful = true; // why suppose an error here in this case ? String msg = i18nStrs.uploaderBadServerResponse() + "\n" + serverRawResponse; if (blobstore) { msg += "\n" + i18nStrs.uploaderBlobstoreBilling(); } else { msg += "\n" + i18nStrs.uploaderBadParsing(); } msg += "\n\n" + responseTxt; log(msg, null); // keep log message anyway // statusWidget.setError(msg); // disable the user visible error message uploadFinished(); } return; } else { log("incorrect response: " + getFileNames() + " " + responseTxt, null); } if (uploadTimeout > 0 && now() - lastData > uploadTimeout) { successful = false; cancelUpload(i18nStrs.uploaderTimeout()); try { sendAjaxRequestToCancelCurrentUpload(); } catch (Exception e) { } } }