Ticket #22497: 22497.patch

File 22497.patch, 9.4 KB (added by taylor.smock, 3 years ago)
  • src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    Subject: [PATCH] Fix #22497: Add a setting opposite to proxy.exceptions
    ---
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java
    a b  
    1919import java.util.EnumMap;
    2020import java.util.Map;
    2121import java.util.Optional;
     22import java.util.ArrayList;
    2223
    2324import javax.swing.BorderFactory;
    2425import javax.swing.Box;
     
    3132import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
    3233import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3334import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     35import org.openstreetmap.josm.gui.widgets.EditableList;
    3436import org.openstreetmap.josm.io.DefaultProxySelector;
    3537import org.openstreetmap.josm.io.ProxyPolicy;
    3638import org.openstreetmap.josm.io.auth.CredentialsAgent;
     
    6466    private final JosmTextField tfProxySocksPort = new JosmTextField(5);
    6567    private final JosmTextField tfProxyHttpUser = new JosmTextField(20);
    6668    private final JosmPasswordField tfProxyHttpPassword = new JosmPasswordField(20);
     69    private final EditableList tfExceptionHosts = new EditableList(tr("No proxy for"));
     70    private final EditableList tfIncludeHosts = new EditableList(tr("Proxy only for"));
    6771
    6872    private JPanel pnlHttpProxyConfigurationPanel;
    6973    private JPanel pnlSocksProxyConfigurationPanel;
     74    private JPanel pnlExceptionIncludesHostsProxyConfigurationPanel;
    7075
    7176    /**
    7277     * Builds the panel for the HTTP proxy configuration
     
    179184        return pnl;
    180185    }
    181186
     187    protected final JPanel buildExceptionIncludesHostsProxyConfigurationPanel() {
     188        JPanel pnl = new AutoSizePanel();
     189        GridBagConstraints gc = new GridBagConstraints();
     190        gc.anchor = GridBagConstraints.LINE_START;
     191        gc.insets = new Insets(5, 5, 0, 0);
     192        gc.weightx = 0.0;
     193        pnl.add(new JLabel(tr("No proxy for (hosts):")), gc);
     194
     195        gc.gridx = 1;
     196        gc.weightx = 0.0;
     197        gc.fill = GridBagConstraints.NONE;
     198        pnl.add(new JLabel(tr("Proxy only for (hosts):")), gc);
     199
     200        gc.gridy = 1;
     201        gc.gridx = 0;
     202        gc.weightx = 1.0;
     203        gc.fill = HORIZONTAL;
     204        tfExceptionHosts.setMinimumSize(tfExceptionHosts.getPreferredSize());
     205        pnl.add(tfExceptionHosts, gc);
     206
     207        gc.gridx = 1;
     208        gc.weightx = 1.0;
     209        gc.fill = HORIZONTAL;
     210        tfIncludeHosts.setMinimumSize(tfIncludeHosts.getPreferredSize());
     211        pnl.add(tfIncludeHosts, gc);
     212
     213        // add an extra spacer, otherwise the layout is broken
     214        gc.gridy = 2;
     215        gc.gridx = 0;
     216        gc.gridwidth = 2;
     217        gc.fill = GridBagConstraints.BOTH;
     218        gc.weightx = 1.0;
     219        gc.weighty = 1.0;
     220        pnl.add(new JPanel(), gc);
     221        return pnl;
     222    }
     223
    182224    protected final JPanel buildProxySettingsPanel() {
    183225        JPanel pnl = new JPanel(new GridBagLayout());
    184226
     
    217259
    218260        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
    219261
     262        // the panel with the exception and includes hosts
     263        pnlExceptionIncludesHostsProxyConfigurationPanel = buildExceptionIncludesHostsProxyConfigurationPanel();
     264        pnl.add(pnlExceptionIncludesHostsProxyConfigurationPanel, GBC.eop().fill(HORIZONTAL));
     265
     266        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
     267
    220268        return pnl;
    221269    }
    222270
     
    238286        tfProxyHttpPort.setText(pref.get(DefaultProxySelector.PROXY_HTTP_PORT, ""));
    239287        tfProxySocksHost.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_HOST, ""));
    240288        tfProxySocksPort.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_PORT, ""));
     289        tfExceptionHosts.setItems(pref.getList(DefaultProxySelector.PROXY_EXCEPTIONS, new ArrayList<String>()));
     290        tfIncludeHosts.setItems(pref.getList(DefaultProxySelector.PROXY_INCLUDES, new ArrayList<String>()));
    241291
    242292        if (pp == ProxyPolicy.USE_SYSTEM_SETTINGS && !DefaultProxySelector.willJvmRetrieveSystemProxies()) {
    243293            Logging.warn(tr("JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. " +
     
    277327        }
    278328
    279329        rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
     330
     331        boolean proxyEnabled = !rbProxyPolicy.get(ProxyPolicy.NO_PROXY).isSelected();
     332        for (Component c : pnlExceptionIncludesHostsProxyConfigurationPanel.getComponents()) {
     333            c.setEnabled(proxyEnabled);
     334        }
    280335    }
    281336
    282337    class ProxyPolicyChangeListener implements ItemListener {
     
    311366        pref.put(DefaultProxySelector.PROXY_HTTP_PORT, tfProxyHttpPort.getText());
    312367        pref.put(DefaultProxySelector.PROXY_SOCKS_HOST, tfProxySocksHost.getText());
    313368        pref.put(DefaultProxySelector.PROXY_SOCKS_PORT, tfProxySocksPort.getText());
     369        pref.putList(DefaultProxySelector.PROXY_EXCEPTIONS, tfExceptionHosts.getItems());
     370        pref.putList(DefaultProxySelector.PROXY_INCLUDES, tfIncludeHosts.getItems());
     371
    314372
    315373        // update the proxy selector
    316374        ProxySelector selector = ProxySelector.getDefault();
  • src/org/openstreetmap/josm/io/DefaultProxySelector.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/io/DefaultProxySelector.java b/src/org/openstreetmap/josm/io/DefaultProxySelector.java
    a b  
    4343    public static final String PROXY_PASS = "proxy.pass";
    4444    /** Property key for proxy exceptions list */
    4545    public static final String PROXY_EXCEPTIONS = "proxy.exceptions";
     46    /**
     47     * Property key for hosts that should be proxied (if this is set, only specified hosts should be proxied)
     48     * @since xxx
     49     */
     50    public static final String PROXY_INCLUDES = "proxy.includes.hosts";
    4651
    4752    private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
    4853
     
    8691    private final Set<String> errorResources = new HashSet<>();
    8792    private final Set<String> errorMessages = new HashSet<>();
    8893    private Set<String> proxyExceptions;
     94    private Set<String> proxyIncludes;
    8995
    9096    /**
    9197     * A typical example is:
     
    161167            }
    162168        }
    163169        proxyExceptions = new HashSet<>(
    164             Config.getPref().getList(PROXY_EXCEPTIONS,
    165                     Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
     170                Config.getPref().getList(PROXY_EXCEPTIONS,
     171                        Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
    166172        );
     173        proxyIncludes = new HashSet<>(
     174                Config.getPref().getList(PROXY_INCLUDES,
     175                        Collections.emptyList())
     176        );
    167177    }
    168178
    169179    @Override
     
    214224
    215225    @Override
    216226    public List<Proxy> select(URI uri) {
    217         if (uri != null && proxyExceptions.contains(uri.getHost())) {
     227        // This is specified in ProxySelector#select, "@throws IllegalArgumentException if the argument is null"
     228        if (uri == null) {
     229            throw new IllegalArgumentException("URI cannot be null");
     230        }
     231        if (proxyExceptions.contains(uri.getHost())) {
    218232            return NO_PROXY_LIST;
    219233        }
    220         switch(proxyPolicy) {
     234        switch (proxyPolicy) {
    221235        case USE_SYSTEM_SETTINGS:
    222236            if (!jvmWillUseSystemProxies) {
    223                 Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. "+
     237                Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. " +
    224238                        "The property ''java.net.useSystemProxies'' was missing at startup time.  Will not use a proxy."));
    225239                return NO_PROXY_LIST;
    226240            }
     241            if (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost())) {
     242                return NO_PROXY_LIST;
     243            }
    227244            // delegate to the former proxy selector
    228245            return delegate.select(uri);
    229246        case NO_PROXY:
    230247            return NO_PROXY_LIST;
    231248        case USE_HTTP_PROXY:
    232             if (httpProxySocketAddress == null)
     249            if (httpProxySocketAddress == null || (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost()))) {
    233250                return NO_PROXY_LIST;
     251            }
    234252            return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
    235253        case USE_SOCKS_PROXY:
    236             if (socksProxySocketAddress == null)
     254            if (socksProxySocketAddress == null || (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost()))) {
    237255                return NO_PROXY_LIST;
     256            }
    238257            return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
    239258        }
    240259        // should not happen
    241         return null;
     260        return Collections.emptyList();
    242261    }
    243262}