예제 #1
0
 /** Cancel upload process and show an error message to the user. */
 private void cancelUpload(String msg) {
   successful = false;
   uploadFinished();
   if (fileInput instanceof IDragAndDropFileInput) {
     ((IDragAndDropFileInput) fileInput).reset();
   }
   statusWidget.setStatus(IUploadStatus.Status.ERROR);
   statusWidget.setError(msg);
 }
예제 #2
0
        public void onSubmit(SubmitEvent event) {

          if (!finished && uploading) {
            uploading = false;
            statusWidget.setStatus(IUploadStatus.Status.CANCELED);
            return;
          }

          if (!autoSubmit && fileQueue.size() > 0) {
            statusWidget.setError(i18nStrs.uploaderActiveUpload());
            event.cancel();
            return;
          }

          if (anyFileIsRepeated(true)) {
            statusWidget.setStatus(IUploadStatus.Status.REPEATED);
            successful = true;
            event.cancel();
            uploadFinished();
            return;
          }

          if (getFileName().isEmpty() || !validateAll(basenames)) {
            event.cancel();
            return;
          }

          if (session == null) {
            event.cancel();
            // Sends a request to the server in order to get the session
            // When the response with the session comes, it re-submits the form.
            session = Session.createSession(servletPath, onSessionReceivedCallback);
            return;
          }

          if (blobstore && !receivedBlobPath) {
            event.cancel();
            // Sends a request to the server in order to get the blobstore path.
            // When the response with the blobstore path comes, it re-submits the form.
            session.sendRequest(
                "blobstore", onBlobstoreReceivedCallback, PARAM_BLOBSTORE + "=true");
            return;
          }
          receivedBlobPath = false;

          addToQueue();
          uploading = true;
          finished = false;
          serverRawResponse = null;
          serverMessage = new ServerMessage();

          statusWidget.setVisible(true);
          updateStatusTimer.squeduleStart();
          statusWidget.setStatus(IUploadStatus.Status.INPROGRESS);
          lastData = now();
        }
예제 #3
0
  /** 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();
    }
  }
예제 #4
0
 public void setServerMessage(ServerMessage msg) {
   serverMessage = msg;
   successful = true;
   statusWidget.setFileNames(msg.getUploadedFileNames());
   uploadFinished();
 }
예제 #5
0
  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) {
      }
    }
  }