From c146f46495dfe344aadc6365b4fd21575b374d1f Mon Sep 17 00:00:00 2001 From: Raman Prasad Date: Wed, 13 May 2015 14:18:27 -0400 Subject: [PATCH] Upload limiter for Sword and Web UI, including Dropbox --- .../edu/harvard/iq/dataverse/DatasetPage.java | 56 ++++++++++++++----- .../datadeposit/SwordConfigurationImpl.java | 22 ++------ .../settings/SettingsServiceBean.java | 8 ++- src/main/webapp/dataset.xhtml | 8 ++- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java index bb04440167c..d4d322adf3e 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java @@ -183,6 +183,26 @@ public enum DisplayMode { private DataFile selectedDownloadFile; + private Integer maxFileUploadSizeInBytes = null; + + + /* + Save the setting locally so db isn't hit repeatedly + + This may be "null", signifying unlimited download size + */ + public Integer getMaxFileUploadSizeInBytes(){ + return this.maxFileUploadSizeInBytes; + } + + public boolean isUnlimitedUploadFileSize(){ + + if (this.maxFileUploadSizeInBytes == null){ + return true; + } + return false; + } + public DataFile getSelectedDownloadFile() { return selectedDownloadFile; } @@ -487,6 +507,7 @@ public String getDropBoxSelection() { public String getDropBoxKey() { // Site-specific DropBox application registration key is configured // via a JVM option under glassfish. + String configuredDropBoxKey = System.getProperty("dataverse.dropbox.key"); if (configuredDropBoxKey != null) { return configuredDropBoxKey; @@ -939,7 +960,8 @@ private void loadMapLayerMetadataLookup() { public String init() { // System.out.println("_YE_OLDE_QUERY_COUNTER_"); // for debug purposes String nonNullDefaultIfKeyNotFound = ""; - + this.maxFileUploadSizeInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes); + guestbookResponse = new GuestbookResponse(); protocol = settingsService.getValueForKey(SettingsServiceBean.Key.Protocol, nonNullDefaultIfKeyNotFound); authority = settingsService.getValueForKey(SettingsServiceBean.Key.Authority, nonNullDefaultIfKeyNotFound); @@ -1994,18 +2016,21 @@ private HttpClient getClient() { return new HttpClient(); } + public boolean showFileUploadFileComponent(){ + + if ((this.editMode == this.editMode.FILE) || (this.editMode == this.editMode.CREATE)){ + return true; + } + return false; + } + + public void handleDropBoxUpload(ActionEvent e) { // Read JSON object from the output of the DropBox Chooser: JsonReader dbJsonReader = Json.createReader(new StringReader(dropBoxSelection)); JsonArray dbArray = dbJsonReader.readArray(); dbJsonReader.close(); - - Integer maxUploadInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes); - if (maxUploadInBytes == null){ - maxUploadInBytes = -1; - } - - + for (int i = 0; i < dbArray.size(); i++) { JsonObject dbObject = dbArray.getJsonObject(i); @@ -2015,11 +2040,16 @@ public void handleDropBoxUpload(ActionEvent e) { int fileSize = dbObject.getInt("bytes"); logger.fine("DropBox url: " + fileLink + ", filename: " + fileName + ", size: " + fileSize); - - // If the file is too big, skip this upload - // - if ((maxUploadInBytes > -1)&&(fileSize > maxUploadInBytes)){ - continue; // continue and add error mesage + + /* ---------------------------- + If the file is too big: + - Add error mesage + - Go to the next file + // ---------------------------- */ + if ((!this.isUnlimitedUploadFileSize())&&(fileSize > this.getMaxFileUploadSizeInBytes())){ + String warningMessage = "Dropbox file \"" + fileName + "\" exceeded the limit of " + fileSize + " bytes and was not uploaded."; + FacesContext.getCurrentInstance().addMessage(e.getComponent().getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "upload failure", warningMessage)); + continue; // skip to next file, and add error mesage } DataFile dFile = null; diff --git a/src/main/java/edu/harvard/iq/dataverse/api/datadeposit/SwordConfigurationImpl.java b/src/main/java/edu/harvard/iq/dataverse/api/datadeposit/SwordConfigurationImpl.java index afdcc9c3b03..29c0960844f 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/datadeposit/SwordConfigurationImpl.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/datadeposit/SwordConfigurationImpl.java @@ -123,29 +123,15 @@ public String getTempDirectory() { @Override public int getMaxUploadSize() { + int unlimited = -1; - + Integer maxUploadInBytes = settingsService.getValueForKeyAsInt(SettingsServiceBean.Key.MaxFileUploadSizeInBytes); if (maxUploadInBytes == null){ - return unlimited; + return unlimited; } - + return maxUploadInBytes; - /* - String maxUploadInBytes = settingsService.getValueForKey(SettingsServiceBean.Key.MaxFileUploadSizeInBytes); - if (maxUploadInBytes != null) { - try { - int maxUploadSizeInBytes = Integer.parseInt(maxUploadInBytes); - return maxUploadSizeInBytes; - } catch (NumberFormatException ex) { - logger.info("Could not convert " + maxUploadInBytes + " from setting " + SettingsServiceBean.Key.MaxFileUploadSizeInBytes + " to int. Setting Data Deposit API max upload size limit to unlimited."); - return unlimited; - } - } else { - logger.fine("Setting " + SettingsServiceBean.Key.MaxFileUploadSizeInBytes + " is undefined. Setting Data Deposit API max upload size limit to unlimited."); - return unlimited; - } - */ } @Override diff --git a/src/main/java/edu/harvard/iq/dataverse/settings/SettingsServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/settings/SettingsServiceBean.java index 7a1e191db93..460fedc60de 100644 --- a/src/main/java/edu/harvard/iq/dataverse/settings/SettingsServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/settings/SettingsServiceBean.java @@ -8,11 +8,13 @@ import java.util.HashSet; import java.util.Set; import java.util.TreeSet; +import java.util.logging.Logger; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import javax.ws.rs.NotFoundException; /** * Service bean accessing a persistent hash map, used as settings in the application. @@ -22,6 +24,8 @@ @Named public class SettingsServiceBean { + private static final Logger logger = Logger.getLogger(SettingsServiceBean.class.getCanonicalName()); + /** * Some convenient keys for the settings. Note that the setting's * name is really a {@code String}, but it's good to have the compiler look @@ -174,6 +178,7 @@ public String getValueForKey( Key key ) { * Attempt to convert the value to an integer * - Applicable for keys such as MaxFileUploadSizeInBytes * + * On failure (key not found or string not convertible to an int), returns null * @param key * @return */ @@ -189,7 +194,7 @@ public Integer getValueForKeyAsInt(Key key){ int valAsInt = Integer.parseInt(val); return valAsInt; } catch (NumberFormatException ex) { - //logger.info("Could not convert " + key + " from setting " + key.toString() + " to int."); + logger.warning("Incorrect setting. Could not convert \"" + val + "\" from setting " + key.toString() + " to int."); return null; } @@ -257,4 +262,5 @@ public Set listAll() { return new HashSet<>(em.createNamedQuery("Setting.findAll", Setting.class).getResultList()); } + } diff --git a/src/main/webapp/dataset.xhtml b/src/main/webapp/dataset.xhtml index 109c9b744eb..ae37eb58131 100644 --- a/src/main/webapp/dataset.xhtml +++ b/src/main/webapp/dataset.xhtml @@ -450,7 +450,7 @@ - +

- + oncomplete="javascript:bind_bsui_components();uploadWidgetDropMsg();" onstart="javascript:uploadWidgetDropRemoveMsg();" + sizeLimit="#{DatasetPage.getMaxFileUploadSizeInBytes()}" invalidSizeMessage="File exceeds our limits. Please contact support. " />