diff --git a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettings.java b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettings.java index e4949dee5..3a40d7e2b 100644 --- a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettings.java +++ b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettings.java @@ -19,7 +19,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; @@ -34,40 +35,32 @@ import org.jxmpp.jid.util.JidUtil; * Bean whose properties are the various preference settings for file transfer. */ public class FileTransferSettings { - private List extensions = new ArrayList<>(); - private List JIDs = new ArrayList<>(); + private Set extensions = Set.of(); + private Set JIDs = Set.of(); private int kb; - private boolean checkSize = false; - String cannedRejectionMessage; + private boolean checkSize; + private String cannedRejectionMessage; private static final File BACKING_STORE = new File(Spark.getSparkUserHome(), "/transferguard.properties"); /** - * Returns a {@link List} of strings - one for each blocked file extension. Strings are in the form *.{extension}. + * Returns a {@link Set} of strings - one for each blocked file extension. Strings are in the form *.{extension}. */ - public List getBlockedExtensions(){ + public Set getBlockedExtensions(){ return extensions; } - /** - * Sets the {@link List} of blocked file extensions. - * @param extensions the {@link List} of blocked file extensions. - */ - public void setBlockedExtensions(List extensions){ + public void setBlockedExtensions(Set extensions){ this.extensions = extensions; } /** - * Returns a {@link List} of blocked JIDs. File transfers from users with those JIDs will be automaticlly rejected. + * Returns a {@link Set} of blocked JIDs. File transfers from users with those JIDs will be automatically rejected. */ - public List getBlockedJIDs() { + public Set getBlockedJIDs() { return JIDs; } - /** - * Sets the {@link List} of blocked JIDs. - * @param JIDs the {@link List} of blocked JIDs. - */ - public void setBlockedJIDS(List JIDs){ + public void setBlockedJIDS(Set JIDs){ this.JIDs = JIDs; } @@ -79,10 +72,6 @@ public class FileTransferSettings { return kb; } - /** - * Sets the maximum file size in kilobytes for file transfers. - * @param kb the maximum file size in kilobytes for file transfers. - */ public void setMaxFileSize(int kb){ this.kb = kb; } @@ -94,11 +83,6 @@ public class FileTransferSettings { return checkSize; } - /** - * If set to true, files larger than the maximum file size as returned by {@link #getMaxFileSize} - * will not be accepted. - * @param checkSize true if size should be checked. - */ public void setCheckFileSize(boolean checkSize){ this.checkSize = checkSize; } @@ -111,11 +95,6 @@ public class FileTransferSettings { return cannedRejectionMessage; } - /** - * Sets the text of a canned message sent to requestors whose file transfers were automatically rejected. If set - * to null or an empty string, no message will be sent. - * @param cannedRejectionMessage the canned message text. - */ public void setCannedRejectionMessage(String cannedRejectionMessage) { this.cannedRejectionMessage = cannedRejectionMessage; } @@ -138,9 +117,9 @@ public class FileTransferSettings { String users = props.getProperty("jids"); if (users != null) { - List jidStrings = convertSettingsStringToList(users); + Set jidStrings = convertSettingsStringToList(users); Set jidSet = JidUtil.entityBareJidSetFrom(jidStrings); - this.JIDs = new ArrayList<>(jidSet); + this.JIDs = jidSet; } String ignore = props.getProperty("checkFileSize"); @@ -170,6 +149,8 @@ public class FileTransferSettings { props.setProperty("maxSize", Integer.toString(kb)); if (cannedRejectionMessage != null) { props.setProperty("cannedResponse", cannedRejectionMessage); + } else { + props.remove("cannedResponse"); } props.store(new FileOutputStream(BACKING_STORE), null); } catch (IOException ioe) { @@ -178,33 +159,21 @@ public class FileTransferSettings { } /** - * Converts a list of strings to a single comma separated string - * @param settings the {@link List} of strings. + * Converts a list of strings to a single comma-separated string */ - public static String convertSettingsListToString(List settings) { - StringBuilder buffer = new StringBuilder(); - boolean first = true; - for (CharSequence cs : settings) { - if (!first) { - buffer.append(','); - } else { - first = false; - } - buffer.append(cs); - } - return buffer.toString(); + public static String convertSettingsListToString(Collection settings) { + return String.join(",", settings); } /** - * Converts the supplied string to a {@link List} of strings. The input is split - * with the tokensL: ',' ':' '\n' '\t' '\r' and ' '. - * @param settings the string to convert. + * Converts the supplied string to a {@link List} of strings in lower case. + * The input is split with the tokens: ',' ':' '\n' '\t' '\r' and ' '. */ - public static List convertSettingsStringToList(String settings) { - List list = new ArrayList<>(); + public static Set convertSettingsStringToList(String settings) { + HashSet list = new HashSet<>(); StringTokenizer tokenizer = new StringTokenizer(settings, ",;\n\t\r "); while (tokenizer.hasMoreTokens()) { - list.add(tokenizer.nextToken()); + list.add(tokenizer.nextToken().toLowerCase()); } return list; } diff --git a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettingsPlugin.java b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettingsPlugin.java index 7fff49f0b..13ad27137 100644 --- a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettingsPlugin.java +++ b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/FileTransferSettingsPlugin.java @@ -26,8 +26,10 @@ import org.jivesoftware.spark.plugin.Plugin; import org.jivesoftware.spark.preference.PreferenceManager; import org.jivesoftware.spark.util.log.Log; +import static org.apache.commons.lang3.StringUtils.isBlank; + /** - * Spark plugin which allows configuration of allowed file sizes, types, and senders for file transfer. + * Spark plugin that allows configuration of allowed file sizes, types, and senders for file transfer. * Transfer requests which don't meet the configured preferences are automatically rejected. */ public class FileTransferSettingsPlugin implements Plugin { @@ -46,7 +48,7 @@ public class FileTransferSettingsPlugin implements Plugin { request.reject(); String responseMessage = settings.getCannedRejectionMessage(); - if (responseMessage != null && !responseMessage.isEmpty()) { + if (!isBlank(responseMessage)) { Message message = StanzaBuilder.buildMessage() .to(request.getRequestor()) .setBody(responseMessage) @@ -97,8 +99,8 @@ public class FileTransferSettingsPlugin implements Plugin { } /** - * Strips the extension off the supplied filename and prepends an asterisk. For example 'bad.doc' would return - * '*.doc'. + * Strips the extension off the supplied filename, lowercase and prepends an asterisk. + * For example, 'bad.DOC' would return '*.doc'. * * @param filename to return the extension for. * @return the extension. @@ -106,7 +108,7 @@ public class FileTransferSettingsPlugin implements Plugin { private String getFileExtensionFromName(String filename) { int dotIdx = filename.lastIndexOf("."); if (dotIdx > 0 && dotIdx < (filename.length() - 1)) { - return "*" + filename.substring( dotIdx ); + return "*" + filename.substring( dotIdx ).toLowerCase(); } return null; } diff --git a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TGuardRes.java b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TGuardRes.java index 0a5738a94..3506206c3 100644 --- a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TGuardRes.java +++ b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TGuardRes.java @@ -5,26 +5,28 @@ import java.util.ResourceBundle; import org.jivesoftware.spark.util.log.Log; +import javax.swing.Icon; +import javax.swing.ImageIcon; + /** - * Use for Transferguard Ressource Internationalization. + * Use for TransferGuard Ressource Internationalization. * - * @author tim.jentz + * @author Tim Jentz */ public class TGuardRes { private static final PropertyResourceBundle prb = (PropertyResourceBundle) ResourceBundle.getBundle("i18n/transferguard_i18n"); - + private static ClassLoader cl = TGuardRes.class.getClassLoader(); + static final Icon TRANSFERGUARD_ICON = new ImageIcon(cl.getResource("/images/transferguard/guard.png")); + private TGuardRes() { - } - public static String getString(String propertyName) { - try { - return prb.getString(propertyName); - } catch (Exception e) { - Log.error(e); - return propertyName; - } + static String getString(String propertyName) { + try { + return prb.getString(propertyName); + } catch (Exception e) { + Log.error(e); + return propertyName; + } } - - } diff --git a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPanel.java b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPanel.java index 52c53af42..015575a35 100644 --- a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPanel.java +++ b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPanel.java @@ -19,14 +19,26 @@ import org.jivesoftware.spark.component.VerticalFlowLayout; import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.util.JidUtil; -import javax.swing.*; -import java.awt.*; -import javax.swing.border.*; +import javax.swing.BorderFactory; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.JTextArea; +import javax.swing.UIManager; +import javax.swing.border.EmptyBorder; +import javax.swing.border.TitledBorder; -import java.util.ArrayList; -import java.util.List; +import java.awt.BorderLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; import java.util.Set; +import static java.awt.GridBagConstraints.HORIZONTAL; +import static java.awt.GridBagConstraints.NONE; +import static java.awt.GridBagConstraints.WEST; + /** * UI for the file transfer preferences. It displays all the various preference settings for editing. */ @@ -46,7 +58,8 @@ public class TransferSettingsPanel extends JPanel { /** * Populates all the gui controls with values from the supplied {@link FileTransferSettings}. - * @param settings the {@link FileTransferSettings} to populate the gui from. + * + * @param settings the {@link FileTransferSettings} to populate the gui from. */ public void applySettings(FileTransferSettings settings) { pnlTypes.setBlockedTypes(settings.getBlockedExtensions()); @@ -58,6 +71,7 @@ public class TransferSettingsPanel extends JPanel { /** * Populates the supplied {@link FileTransferSettings} from the values in the gui controls. + * * @param settings the {@link FileTransferSettings} to populate. */ public void storeSettings(FileTransferSettings settings) { @@ -69,51 +83,52 @@ public class TransferSettingsPanel extends JPanel { } private static class BlockedTypesPanel extends JPanel { - private final JTextArea txtBlockedTypes = new JTextArea(2, 0); + private final JTextArea txtBlockedTypes = new JTextArea(2, 0); + BlockedTypesPanel() { txtBlockedTypes.setBorder(UIManager.getLookAndFeelDefaults().getBorder("TextField.border")); - txtBlockedTypes.setToolTipText(TGuardRes.getString("guard.settings.tooltips.blockedtypes")); setLayout(new BorderLayout()); - setBorder(BorderFactory.createCompoundBorder(new TitledBorder(TGuardRes.getString("guard.settings.title.extensions")), - new EmptyBorder(2,4,4,4))); + TitledBorder outsideBorder = new TitledBorder(TGuardRes.getString("guard.settings.title.extensions")); + setBorder(BorderFactory.createCompoundBorder(outsideBorder, new EmptyBorder(2, 4, 4, 4))); add(txtBlockedTypes, BorderLayout.CENTER); } - public void setBlockedTypes(List types) { + public void setBlockedTypes(Set types) { txtBlockedTypes.setText(FileTransferSettings.convertSettingsListToString(types)); } - public List getBlockedTypes() { + public Set getBlockedTypes() { return FileTransferSettings.convertSettingsStringToList(txtBlockedTypes.getText()); } } private static class BlockedPeoplePanel extends JPanel { - private final JTextArea txtBlockedPeople = new JTextArea(2, 0); + private final JTextArea txtBlockedPeople = new JTextArea(2, 0); BlockedPeoplePanel() { txtBlockedPeople.setBorder(UIManager.getLookAndFeelDefaults().getBorder("TextField.border")); txtBlockedPeople.setToolTipText(TGuardRes.getString("guard.settings.tooltips.blockedperson")); setLayout(new BorderLayout()); - setBorder(BorderFactory.createCompoundBorder(new TitledBorder(TGuardRes.getString("guard.settings.title.person")), - new EmptyBorder(2,4,4,4))); + TitledBorder outsideBorder = new TitledBorder(TGuardRes.getString("guard.settings.title.person")); + setBorder(BorderFactory.createCompoundBorder(outsideBorder, new EmptyBorder(2, 4, 4, 4))); add(txtBlockedPeople, BorderLayout.CENTER); } - public void setBlockedPeople(List people) { + public void setBlockedPeople(Set people) { txtBlockedPeople.setText(FileTransferSettings.convertSettingsListToString(people)); } - public List getBlockedPeople() { - List jidStrings = FileTransferSettings.convertSettingsStringToList(txtBlockedPeople.getText()); + public Set getBlockedPeople() { + Set jidStrings = FileTransferSettings.convertSettingsStringToList(txtBlockedPeople.getText()); Set jidSet = JidUtil.entityBareJidSetFrom(jidStrings); - return new ArrayList<>(jidSet); + return jidSet; } } private static class FileSizePanel extends JPanel { - private final JSpinner spinMaxSize = new JSpinner(); + private final JLabel spinMaxSizeLabel = new JLabel(TGuardRes.getString(("guard.settings.label.maxsize"))); + private final JSpinner spinMaxSize = new JSpinner(); private final JCheckBox chkMaxEnabled = new JCheckBox(TGuardRes.getString("guard.settings.limitcheck")); FileSizePanel() { @@ -122,19 +137,14 @@ public class TransferSettingsPanel extends JPanel { add(chkMaxEnabled); JPanel pnlSpinner = new JPanel(new GridBagLayout()); - pnlSpinner.add(new JLabel(TGuardRes.getString(("guard.settings.label.maxsize"))), - new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,2), 0, 0)); - pnlSpinner.add(spinMaxSize, - new GridBagConstraints(1, 0, 1, 1, 0.25, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0)); - pnlSpinner.add(new JPanel(), - new GridBagConstraints(2, 0, 1, 1, 0.75, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0)); + pnlSpinner.add(spinMaxSizeLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, WEST, NONE, new Insets(0, 0, 0, 2), 0, 0)); + pnlSpinner.add(spinMaxSize, new GridBagConstraints(1, 0, 1, 1, 0.25, 0, WEST, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); + pnlSpinner.add(new JPanel(), new GridBagConstraints(2, 0, 1, 1, 0.75, 0, WEST, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); add(pnlSpinner); - chkMaxEnabled.addActionListener( evnt -> { - if (spinMaxSize != null) { - spinMaxSize.setEnabled(chkMaxEnabled.isSelected()); - } - } ); + chkMaxEnabled.addActionListener(e -> { + spinMaxSize.setEnabled(chkMaxEnabled.isSelected()); + }); } public void setMaxFileSize(int kb) { @@ -155,14 +165,14 @@ public class TransferSettingsPanel extends JPanel { } private static class CannedResponsePanel extends JPanel { - private final JTextArea txtMessage = new JTextArea(2, 0); + private final JTextArea txtMessage = new JTextArea(2, 0); CannedResponsePanel() { txtMessage.setBorder(UIManager.getLookAndFeelDefaults().getBorder("TextField.border")); txtMessage.setToolTipText(TGuardRes.getString(("guard.settings.tooltips.textarea"))); setLayout(new BorderLayout()); - setBorder(BorderFactory.createCompoundBorder(new TitledBorder(TGuardRes.getString(("guard.settings.title.rejectresponse"))), - new EmptyBorder(2,4,4,4))); + TitledBorder outsideBorder = new TitledBorder(TGuardRes.getString(("guard.settings.title.rejectresponse"))); + setBorder(BorderFactory.createCompoundBorder(outsideBorder, new EmptyBorder(2, 4, 4, 4))); add(txtMessage, BorderLayout.CENTER); } diff --git a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPreference.java b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPreference.java index 2b0f13d39..85ceeaa27 100644 --- a/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPreference.java +++ b/plugins/transferguard/src/main/java/org/jivesoftware/spark/plugins/transfersettings/TransferSettingsPreference.java @@ -18,11 +18,11 @@ package org.jivesoftware.spark.plugins.transfersettings; import org.jivesoftware.spark.preference.Preference; import javax.swing.Icon; -import javax.swing.ImageIcon; import javax.swing.JComponent; /** - * Preference object for file transfer settings. Used by Spark to show the associated UI and commit changes to preference settings. + * Preference object for file transfer settings. + * Used by Spark to show the associated UI and commit changes to preference settings. */ public class TransferSettingsPreference implements Preference { static final String NAMESPACE = "transferSettings"; @@ -54,7 +54,7 @@ public class TransferSettingsPreference implements Preference { @Override public Icon getIcon() { - return new ImageIcon(getClass().getResource("/images/guard.png")); + return TGuardRes.TRANSFERGUARD_ICON; } @Override diff --git a/plugins/transferguard/src/main/resources/changelog.html b/plugins/transferguard/src/main/resources/changelog.html index ae3fc93fd..07183bfe8 100644 --- a/plugins/transferguard/src/main/resources/changelog.html +++ b/plugins/transferguard/src/main/resources/changelog.html @@ -43,24 +43,22 @@

TransferGuard Spark plug Changelog

- +

1.3 -- July 26, 2026

+
    +
  • SPARK-2099: TransferGuard case-insensitive blocking by file extension
  • +

1.0.2 -- August 15th, 2013

    -
  • Updated polnish language file.
  • +
  • Updated Polish language file.
-

1.0.1 -- September 19th, 2006

    -
  • Moving over to be Spark 2.0 compatible.
  • +
  • Moving over to be Spark 2.0 compatible.
-

1.0.0 -- February 9th, 2006

    -
  • Initial release.
  • +
  • Initial release.
- - - diff --git a/plugins/transferguard/src/main/resources/images/knight.png b/plugins/transferguard/src/main/resources/images/knight.png deleted file mode 100644 index e6dbcfe83..000000000 Binary files a/plugins/transferguard/src/main/resources/images/knight.png and /dev/null differ diff --git a/plugins/transferguard/src/main/resources/images/guard.png b/plugins/transferguard/src/main/resources/images/transferguard/guard.png similarity index 100% rename from plugins/transferguard/src/main/resources/images/guard.png rename to plugins/transferguard/src/main/resources/images/transferguard/guard.png