示例#1
0
        public void onResponseReceived(Request request, Response response) {
          String text = response.getText();
          String url = null;
          Document document = null;

          String bpath = "<" + TAG_BLOBSTORE_PATH + ">";
          String sbpath = "</" + TAG_BLOBSTORE_PATH + ">";
          if (text.contains(bpath)) {
            try {
              document = XMLParser.parse(text);
              url = Utils.getXmlNodeValue(document, TAG_BLOBSTORE_PATH);
            } catch (Exception e) {
              cancelUpload(
                  i18nStrs.uploaderBlobstoreError() + "\n>>>\n" + e.getMessage() + "\n>>>>\n" + e);
              return;
            }
            if (url == null) {
              url =
                  text.replaceAll("[\r\n]+", "")
                      .replaceAll("^.*" + bpath + "\\s*", "")
                      .replaceAll("\\s*" + sbpath + ".*$", "");
            }
          }
          if (url != null && url.length() > 0 && !"null".equalsIgnoreCase(url)) {
            if (session.getServletPath().startsWith("http")) {
              url = session.getServletPath().replaceFirst("(https?://[^/]+).*", "$1") + url;
            }
            uploadForm.setAction(url);
          } else {
            uploadForm.setAction(session.getServletPath());
          }
          removeHiddens();
          if (document != null) {
            String name = Utils.getXmlNodeValue(document, TAG_BLOBSTORE_NAME);
            if (name != null) {
              fileInput.setName(name);
            }
            NodeList list = document.getElementsByTagName(TAG_BLOBSTORE_PARAM);
            for (int i = 0; i < list.getLength(); i++) {
              Node node = list.item(i);
              String value = Utils.getXmlNodeValue(node);
              if (value != null) {
                Node attribute = node.getAttributes().getNamedItem(ATTR_BLOBSTORE_PARAM_NAME);
                if (attribute != null) {
                  String paramName = attribute.getNodeValue();
                  if (paramName != null) {
                    addHidden(paramName, value);
                  }
                }
              }
            }
          }
          receivedBlobPath = true;
          uploadForm.submit();
        }
示例#2
0
 public void onResponseReceived(Request request, Response response) {
   try {
     String s = Utils.getXmlNodeValue(XMLParser.parse(response.getText()), TAG_BLOBSTORE);
     blobstore = "true".equalsIgnoreCase(s);
     // with blobstore status does not make sense
     if (blobstore) {
       updateStatusTimer.setInterval(5000);
     }
     uploadForm.setAction(session.getServletPath());
     uploadForm.submit();
   } catch (Exception e) {
     String message =
         e.getMessage().contains("error:")
             ? i18nStrs.uploaderServerUnavailable()
                 + " (3) "
                 + getServletPath()
                 + "\n\n"
                 + i18nStrs.uploaderServerError()
                 + "\nAction: "
                 + getServletPath()
                 + "\nException: "
                 + e.getMessage()
                 + response.getText()
             : i18nStrs.submitError();
     cancelUpload(message);
   }
 }
示例#3
0
 private boolean validateExtension(String filename) {
   if (filename == null || filename.length() == 0) {
     return !avoidEmptyFile;
   }
   boolean valid = Utils.validateExtension(validExtensions, filename);
   if (!valid) {
     finished = true;
     statusWidget.setError(i18nStrs.uploaderInvalidExtension() + validExtensionsMsg);
     statusWidget.setStatus(Status.INVALID);
   }
   return valid;
 }
示例#4
0
 private JavaScriptObject getDataInfo(UploadedInfo info) {
   return info == null
       ? JavaScriptObject.createObject()
       : getDataImpl(
           info.fileUrl,
           info.field,
           info.name,
           Utils.basename(info.name),
           serverRawResponse,
           info.message,
           getStatus().toString(),
           info.size);
 }
示例#5
0
 public void onChange(ChangeEvent event) {
   basenames.clear();
   for (String s : getFileNames()) {
     basenames.add(Utils.basename(s));
   }
   statusWidget.setFileNames(basenames);
   if (anyFileIsRepeated(false)) {
     statusWidget.setStatus(Status.REPEATED);
     return;
   }
   if (autoSubmit && !validateAll(basenames)) {
     return;
   }
   if (autoSubmit && fileSelected()) {
     automaticUploadTimer.scheduleRepeating(DEFAULT_AUTOUPLOAD_DELAY);
   }
   onChangeInput();
 }
示例#6
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) {
      }
    }
  }