Changeset 2641 in josm


Ignore:
Timestamp:
Dec 16, 2009 6:58:04 PM (3 years ago)
Author:
Gubaer
Message:

new: supports system defined proxies if JOSM is started with -Djava.net.useSystemProxies=true
fixed #1641: JOSM doesn't allow for setting HTTP proxy user/password distrinct from OSM server user/password
fixed #2865: SOCKS Proxy Support
fixed #4182: Proxy Authentication

Location:
trunk
Files:
9 added
14 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r2477 r2641  
    2222import org.openstreetmap.josm.io.OsmServerLocationReader; 
    2323import org.openstreetmap.josm.io.OsmServerReader; 
     24import org.openstreetmap.josm.io.OsmTransferCancelledException; 
    2425import org.openstreetmap.josm.io.OsmTransferException; 
    2526import org.xml.sax.SAXException; 
     
    9495                    return; 
    9596                } 
    96                 if (e instanceof OsmTransferException) { 
     97                if (e instanceof OsmTransferCancelledException) { 
     98                    setCanceled(true); 
     99                    return; 
     100                } else if (e instanceof OsmTransferException) { 
    97101                    rememberException(e); 
    98102                } else { 
  • trunk/src/org/openstreetmap/josm/actions/upload/ApiPreconditionCheckerHook.java

    r2598 r2641  
    1717import org.openstreetmap.josm.io.OsmApi; 
    1818import org.openstreetmap.josm.io.OsmApiInitializationException; 
     19import org.openstreetmap.josm.io.OsmTransferCancelledException; 
    1920 
    2021public class ApiPreconditionCheckerHook implements UploadHook { 
     
    3839                    return false; 
    3940            } 
     41        } catch(OsmTransferCancelledException e){ 
     42            return false; 
    4043        } catch (OsmApiInitializationException e) { 
    4144            ExceptionDialogUtil.explainOsmTransferException(e); 
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r2620 r2641  
    2626import java.util.Map.Entry; 
    2727import java.util.concurrent.CopyOnWriteArrayList; 
     28import java.util.logging.Logger; 
    2829import java.util.regex.Matcher; 
    2930import java.util.regex.Pattern; 
     
    3233 
    3334import org.openstreetmap.josm.Main; 
    34 import org.openstreetmap.josm.gui.preferences.ProxyPreferences; 
    3535import org.openstreetmap.josm.tools.ColorHelper; 
    3636 
     
    4444 */ 
    4545public class Preferences { 
     46    static private final Logger logger = Logger.getLogger(Preferences.class.getName()); 
    4647 
    4748    /** 
     
    731732    public void updateSystemProperties() { 
    732733        Properties sysProp = System.getProperties(); 
    733         if (getBoolean(ProxyPreferences.PROXY_ENABLE)) { 
    734             sysProp.put("proxySet", "true"); 
    735             sysProp.put("http.proxyHost", get(ProxyPreferences.PROXY_HOST)); 
    736             sysProp.put("proxyPort", get(ProxyPreferences.PROXY_PORT)); 
    737             if (!getBoolean(ProxyPreferences.PROXY_ANONYMOUS)) { 
    738                 sysProp.put("proxyUser", get(ProxyPreferences.PROXY_USER)); 
    739                 sysProp.put("proxyPassword", get(ProxyPreferences.PROXY_PASS)); 
    740             } 
    741  
    742         } 
    743734        sysProp.put("http.agent", Version.getInstance().getAgentString()); 
    744735        System.setProperties(sysProp); 
  • trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java

    r2512 r2641  
    2525 
    2626import org.openstreetmap.josm.Main; 
    27 import org.openstreetmap.josm.data.Preferences.Bookmark; 
    2827import org.openstreetmap.josm.io.OsmConnection; 
     28import org.openstreetmap.josm.io.OsmTransferException; 
    2929import org.openstreetmap.josm.io.XmlWriter; 
    3030import org.openstreetmap.josm.tools.Base64; 
     
    6666                return b.toString(); 
    6767            } catch (IOException e) { 
     68                e.printStackTrace(); 
     69            } catch(OsmTransferException e) { 
    6870                e.printStackTrace(); 
    6971            } 
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r2358 r2641  
    99import java.awt.event.WindowAdapter; 
    1010import java.awt.event.WindowEvent; 
     11import java.net.Authenticator; 
     12import java.net.ProxySelector; 
    1113import java.util.Arrays; 
    1214import java.util.Collection; 
     
    1921 
    2022import org.openstreetmap.josm.Main; 
     23import org.openstreetmap.josm.io.DefaultProxySelector; 
     24import org.openstreetmap.josm.io.auth.CredentialsManagerFactory; 
     25import org.openstreetmap.josm.io.auth.DefaultAuthenticator; 
    2126import org.openstreetmap.josm.plugins.PluginHandler; 
    2227import org.openstreetmap.josm.tools.BugReportExceptionHandler; 
     
    97102        } 
    98103        Main.pref.updateSystemProperties(); 
     104 
     105        Authenticator.setDefault( 
     106                new DefaultAuthenticator( 
     107                        CredentialsManagerFactory.getCredentialManager() 
     108                ) 
     109        ); 
     110        ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault())); 
    99111 
    100112        if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) { 
  • trunk/src/org/openstreetmap/josm/gui/io/UploadPrimitivesTask.java

    r2613 r2641  
    2929import org.openstreetmap.josm.io.OsmApiPrimitiveGoneException; 
    3030import org.openstreetmap.josm.io.OsmServerWriter; 
     31import org.openstreetmap.josm.io.OsmTransferCancelledException; 
    3132import org.openstreetmap.josm.io.OsmTransferException; 
    3233import org.openstreetmap.josm.tools.ImageProvider; 
     
    239240                    // 
    240241                    break; 
     242                } catch(OsmTransferCancelledException e) { 
     243                    uploadCancelled = true; 
     244                    return; 
    241245                } catch(OsmApiPrimitiveGoneException e) { 
    242246                    // try to recover from  410 Gone 
  • trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java

    r2535 r2641  
    4444    // some common tabs 
    4545    public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program.")); 
    46     public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),true); 
     46    public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),false); 
    4747    public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation.")); 
    4848    public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers.")); 
  • trunk/src/org/openstreetmap/josm/gui/preferences/ProxyPreferences.java

    r1742 r2641  
    44import static org.openstreetmap.josm.tools.I18n.tr; 
    55 
    6 import java.awt.event.ActionEvent; 
    7 import java.awt.event.ActionListener; 
    8  
    9 import javax.swing.Box; 
    10 import javax.swing.JCheckBox; 
     6import java.awt.Dimension; 
     7import java.awt.GridBagConstraints; 
     8import java.awt.GridBagLayout; 
     9import java.awt.Insets; 
     10import java.awt.event.ItemEvent; 
     11import java.awt.event.ItemListener; 
     12import java.net.ProxySelector; 
     13import java.util.HashMap; 
     14import java.util.Map; 
     15 
     16import javax.swing.ButtonGroup; 
    1117import javax.swing.JLabel; 
     18import javax.swing.JPanel; 
    1219import javax.swing.JPasswordField; 
     20import javax.swing.JRadioButton; 
    1321import javax.swing.JSeparator; 
    1422import javax.swing.JTextField; 
     
    1624 
    1725import org.openstreetmap.josm.Main; 
     26import org.openstreetmap.josm.gui.JMultilineLabel; 
     27import org.openstreetmap.josm.io.DefaultProxySelector; 
    1828import org.openstreetmap.josm.tools.GBC; 
    1929 
    2030public class ProxyPreferences implements PreferenceSetting { 
     31 
     32 
    2133 
    2234    public static class Factory implements PreferenceSettingFactory { 
     
    2638    } 
    2739 
    28     public static final String PROXY_ENABLE = "proxy.enable"; 
    29     public static final String PROXY_HOST = "proxy.host"; 
    30     public static final String PROXY_PORT = "proxy.port"; 
    31     public static final String PROXY_ANONYMOUS = "proxy.anonymous"; 
     40    public enum ProxyPolicy { 
     41        NO_PROXY("no-proxy"), 
     42        USE_SYSTEM_SETTINGS("use-system-settings"), 
     43        USE_HTTP_PROXY("use-http-proxy"), 
     44        USE_SOCKS_PROXY("use-socks-proxy"); 
     45 
     46        private String policyName; 
     47        ProxyPolicy(String policyName) { 
     48            this.policyName = policyName; 
     49        } 
     50 
     51        public String getName() { 
     52            return policyName; 
     53        } 
     54 
     55        static public ProxyPolicy fromName(String policyName) { 
     56            if (policyName == null) return null; 
     57            policyName = policyName.trim().toLowerCase(); 
     58            for(ProxyPolicy pp: values()) { 
     59                if (pp.getName().equals(policyName)) 
     60                    return pp; 
     61            } 
     62            return null; 
     63        } 
     64    } 
     65 
     66    public static final String PROXY_POLICY = "proxy.policy"; 
     67    public static final String PROXY_HTTP_HOST = "proxy.http.host"; 
     68    public static final String PROXY_HTTP_PORT = "proxy.http.port"; 
     69    public static final String PROXY_SOCKS_HOST = "proxy.socks.host"; 
     70    public static final String PROXY_SOCKS_PORT = "proxy.socks.port"; 
    3271    public static final String PROXY_USER = "proxy.user"; 
    3372    public static final String PROXY_PASS = "proxy.pass"; 
    3473 
    35     private JCheckBox proxyEnable = new JCheckBox(tr("Enable proxy server")); 
    36     private JTextField proxyHost = new JTextField(20); 
    37     private JTextField proxyPort = new JTextField(5); 
    38     private JCheckBox proxyAnonymous = new JCheckBox(tr("Anonymous")); 
    39     private JTextField proxyUser = new JTextField(20); 
    40     private JPasswordField proxyPass = new JPasswordField(20); 
     74    private ButtonGroup bgProxyPolicy; 
     75    private Map<ProxyPolicy, JRadioButton> rbProxyPolicy; 
     76    private JTextField tfProxyHttpHost; 
     77    private JTextField tfProxyHttpPort; 
     78    private JTextField tfProxySocksHost; 
     79    private JTextField tfProxySocksPort; 
     80    private JTextField tfProxyHttpUser; 
     81    private JPasswordField tfProxyHttpPassword; 
     82 
     83    protected JPanel buildHttpProxyConfigurationPanel() { 
     84        JPanel pnl = new JPanel(new GridBagLayout()) { 
     85            @Override 
     86            public Dimension getMinimumSize() { 
     87                return getPreferredSize(); 
     88            } 
     89        }; 
     90        GridBagConstraints gc = new GridBagConstraints(); 
     91 
     92        gc.anchor = GridBagConstraints.WEST; 
     93        gc.insets = new Insets(5,5,0,0); 
     94        pnl.add(new JLabel("Host:"), gc); 
     95 
     96        gc.gridx = 1; 
     97        pnl.add(tfProxyHttpHost = new JTextField(20),gc); 
     98 
     99        gc.gridy = 1; 
     100        gc.gridx = 0; 
     101        pnl.add(new JLabel("Port:"), gc); 
     102 
     103        gc.gridx = 1; 
     104        gc.weightx = 0.0; 
     105        pnl.add(tfProxyHttpPort = new JTextField(5),gc); 
     106 
     107        gc.gridy = 2; 
     108        gc.gridx = 0; 
     109        gc.gridwidth = 2; 
     110        gc.fill = GridBagConstraints.BOTH; 
     111        gc.weightx = 1.0; 
     112        gc.weighty = 1.0; 
     113        pnl.add(new JMultilineLabel(tr("Please enter a username and a password if your proxy requires authentication.")), gc); 
     114 
     115        gc.gridy = 3; 
     116        gc.gridx = 0; 
     117        gc.gridwidth = 1; 
     118        gc.weightx = 0.0; 
     119        gc.fill = GridBagConstraints.NONE; 
     120        pnl.add(new JLabel("User:"), gc); 
     121 
     122        gc.gridy = 3; 
     123        gc.gridx = 1; 
     124        pnl.add(tfProxyHttpUser = new JTextField(20),gc); 
     125 
     126        gc.gridy = 4; 
     127        gc.gridx = 0; 
     128        pnl.add(new JLabel("Password:"), gc); 
     129 
     130        gc.gridx = 1; 
     131        pnl.add(tfProxyHttpPassword = new JPasswordField(20),gc); 
     132        return pnl; 
     133    } 
     134 
     135    protected JPanel buildSocksProxyConfigurationPanel() { 
     136        JPanel pnl = new JPanel(new GridBagLayout()) { 
     137            @Override 
     138            public Dimension getMinimumSize() { 
     139                return getPreferredSize(); 
     140            } 
     141        }; 
     142        GridBagConstraints gc = new GridBagConstraints(); 
     143        gc.anchor = GridBagConstraints.WEST; 
     144        gc.insets = new Insets(5,5,0,0); 
     145        pnl.add(new JLabel("Host:"), gc); 
     146 
     147        gc.gridx = 1; 
     148        pnl.add(tfProxySocksHost = new JTextField(20),gc); 
     149 
     150        gc.gridy = 1; 
     151        gc.gridx = 0; 
     152        pnl.add(new JLabel("Port:"), gc); 
     153 
     154        gc.gridx = 1; 
     155        pnl.add(tfProxySocksPort = new JTextField(5),gc); 
     156 
     157        // add an extra spacer, otherwise the layout is broken 
     158        gc.gridy = 2; 
     159        gc.gridx = 0; 
     160        gc.gridwidth = 2; 
     161        gc.fill = GridBagConstraints.BOTH; 
     162        gc.weightx = 1.0; 
     163        gc.weighty = 1.0; 
     164        pnl.add(new JPanel(), gc); 
     165        return pnl; 
     166    } 
     167 
     168    protected JPanel buildProxySettingsPanel() { 
     169        JPanel pnl = new JPanel(new GridBagLayout()); 
     170        GridBagConstraints gc = new GridBagConstraints(); 
     171 
     172        bgProxyPolicy = new ButtonGroup(); 
     173        rbProxyPolicy = new HashMap<ProxyPolicy, JRadioButton>(); 
     174        ProxyPolicyChangeListener policyChangeListener = new ProxyPolicyChangeListener(); 
     175        for (ProxyPolicy pp: ProxyPolicy.values()) { 
     176            rbProxyPolicy.put(pp, new JRadioButton()); 
     177            bgProxyPolicy.add(rbProxyPolicy.get(pp)); 
     178            rbProxyPolicy.get(pp).addItemListener(policyChangeListener); 
     179        } 
     180        gc.gridx = 0; 
     181        gc.gridy = 0; 
     182        gc.fill = GridBagConstraints.NONE; 
     183        gc.anchor = GridBagConstraints.FIRST_LINE_START; 
     184        pnl.add(rbProxyPolicy.get(ProxyPolicy.NO_PROXY),gc); 
     185        gc.gridx = 1; 
     186        gc.fill = GridBagConstraints.HORIZONTAL; 
     187        gc.weightx = 1.0; 
     188        pnl.add(new JLabel(tr("No proxy")), gc); 
     189 
     190        gc.gridx = 0; 
     191        gc.gridy = 1; 
     192        gc.fill = GridBagConstraints.NONE; 
     193        gc.anchor = GridBagConstraints.FIRST_LINE_START; 
     194        pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS),gc); 
     195        gc.gridx = 1; 
     196        gc.fill = GridBagConstraints.HORIZONTAL; 
     197        gc.weightx = 1.0; 
     198        gc.weighty = 0.0; 
     199        String msg; 
     200        if (DefaultProxySelector.willJvmRetrieveSystemProxies()) { 
     201            msg = tr("Use standard system settings"); 
     202        } else { 
     203            msg = tr("Use standard system settings (disabled. Start JOSM with <tt>-Djava.net.useSystemProxies=true</tt> to enable)"); 
     204        } 
     205        pnl.add(new JMultilineLabel("<html>" + msg + "</html>"), gc); 
     206 
     207        gc.gridx = 0; 
     208        gc.gridy = 2; 
     209        gc.fill = GridBagConstraints.NONE; 
     210        gc.anchor = GridBagConstraints.FIRST_LINE_START; 
     211        pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY),gc); 
     212        gc.gridx = 1; 
     213        gc.fill = GridBagConstraints.HORIZONTAL; 
     214        gc.weightx = 1.0; 
     215        pnl.add(new JLabel(tr("Manually configure a HTTP proxy")),gc); 
     216 
     217        gc.gridx = 1; 
     218        gc.gridy = 3; 
     219        gc.fill = GridBagConstraints.HORIZONTAL; 
     220        gc.weightx = 1.0; 
     221        gc.weighty = 0.0; 
     222        pnl.add(buildHttpProxyConfigurationPanel(),gc); 
     223 
     224        gc.gridx = 0; 
     225        gc.gridy = 4; 
     226        gc.fill = GridBagConstraints.NONE; 
     227        gc.anchor = GridBagConstraints.FIRST_LINE_START; 
     228        pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY),gc); 
     229        gc.gridx = 1; 
     230        gc.fill = GridBagConstraints.HORIZONTAL; 
     231        gc.weightx = 1.0; 
     232        pnl.add(new JLabel(tr("Use a SOCKS proxy")),gc); 
     233 
     234        gc.gridx = 1; 
     235        gc.gridy = 5; 
     236        gc.fill = GridBagConstraints.BOTH; 
     237        gc.anchor = GridBagConstraints.WEST; 
     238        gc.weightx = 1.0; 
     239        gc.weighty = 0.0; 
     240        pnl.add(buildSocksProxyConfigurationPanel(),gc); 
     241 
     242        return pnl; 
     243    } 
     244 
     245    protected void initFromPreferences() { 
     246        String policy = Main.pref.get(PROXY_POLICY, null); 
     247        ProxyPolicy pp = ProxyPolicy.fromName(policy); 
     248        pp = pp == null? ProxyPolicy.NO_PROXY: pp; 
     249        rbProxyPolicy.get(pp).setSelected(true); 
     250        String value = Main.pref.get("proxy.host", null); 
     251        if (value != null) { 
     252            // legacy support 
     253            tfProxyHttpHost.setText(value); 
     254            Main.pref.put("proxy.host", null); 
     255        } else { 
     256            tfProxyHttpHost.setText(Main.pref.get(PROXY_HTTP_HOST, "")); 
     257        } 
     258        value = Main.pref.get("proxy.port", null); 
     259        if (value != null) { 
     260            // legacy support 
     261            tfProxyHttpPort.setText(value); 
     262            Main.pref.put("proxy.port", null); 
     263        } else { 
     264            tfProxyHttpPort.setText(Main.pref.get(PROXY_HTTP_PORT, "")); 
     265        } 
     266        tfProxySocksHost.setText(Main.pref.get(PROXY_SOCKS_HOST, "")); 
     267        tfProxySocksPort.setText(Main.pref.get(PROXY_SOCKS_PORT, "")); 
     268        tfProxyHttpUser.setText(Main.pref.get(PROXY_USER, "")); 
     269        tfProxyHttpPassword.setText(Main.pref.get(PROXY_PASS, "")); 
     270 
     271        if (pp.equals(ProxyPolicy.USE_SYSTEM_SETTINGS) && ! DefaultProxySelector.willJvmRetrieveSystemProxies()) { 
     272            System.err.println(tr("Warning: JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. Resetting preferences to ''No proxy''")); 
     273            pp = ProxyPolicy.NO_PROXY; 
     274            rbProxyPolicy.get(pp).setSelected(true); 
     275        } 
     276    } 
     277 
     278    protected void updateEnabledState() { 
     279        tfProxyHttpHost.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected()); 
     280        tfProxyHttpPort.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected()); 
     281        tfProxyHttpUser.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected()); 
     282        tfProxyHttpPassword.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected()); 
     283        tfProxySocksHost.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected()); 
     284        tfProxySocksPort.setEnabled(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected()); 
     285 
     286        rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies()); 
     287    } 
     288 
     289    class ProxyPolicyChangeListener implements ItemListener { 
     290        public void itemStateChanged(ItemEvent arg0) { 
     291            updateEnabledState(); 
     292        } 
     293    } 
    41294 
    42295    public void addGui(PreferenceDialog gui) { 
    43         proxyEnable.setSelected(Main.pref.getBoolean(PROXY_ENABLE)); 
    44         proxyEnable.addActionListener(new ActionListener(){ 
    45             public void actionPerformed(ActionEvent e) { 
    46                 proxyHost.setEnabled(proxyEnable.isSelected()); 
    47                 proxyPort.setEnabled(proxyEnable.isSelected()); 
    48                 proxyAnonymous.setEnabled(proxyEnable.isSelected()); 
    49                 proxyUser.setEnabled(proxyEnable.isSelected() && !proxyAnonymous.isSelected()); 
    50                 proxyPass.setEnabled(proxyEnable.isSelected() && !proxyAnonymous.isSelected()); 
    51             } 
    52         }); 
    53         proxyHost.setEnabled(Main.pref.getBoolean(PROXY_ENABLE)); 
    54         proxyHost.setText(Main.pref.get(PROXY_HOST)); 
    55         proxyPort.setEnabled(Main.pref.getBoolean(PROXY_ENABLE)); 
    56         proxyPort.setText(Main.pref.get(PROXY_PORT)); 
    57         proxyAnonymous.setEnabled(Main.pref.getBoolean(PROXY_ENABLE)); 
    58         proxyAnonymous.setSelected(Main.pref.getBoolean(PROXY_ANONYMOUS)); 
    59         proxyAnonymous.addActionListener(new ActionListener(){ 
    60             public void actionPerformed(ActionEvent e) { 
    61                 proxyUser.setEnabled(proxyEnable.isSelected() && !proxyAnonymous.isSelected()); 
    62                 proxyPass.setEnabled(proxyEnable.isSelected() && !proxyAnonymous.isSelected()); 
    63             } 
    64         }); 
    65         proxyUser.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS))); 
    66         proxyUser.setText(Main.pref.get(PROXY_USER)); 
    67         proxyPass.setEnabled(Main.pref.getBoolean(PROXY_ENABLE) && (Main.pref.getBoolean(PROXY_ANONYMOUS))); 
    68         proxyPass.setText(Main.pref.get(PROXY_USER)); 
    69  
    70296        gui.connection.add(new JSeparator(SwingConstants.HORIZONTAL), GBC.eol().fill(GBC.HORIZONTAL)); 
    71297        gui.connection.add(new JLabel(tr("Proxy Settings")), GBC.eol()); 
    72         gui.connection.add(proxyEnable, GBC.eol().insets(20, 0, 0, 0)); 
    73         gui.connection.add(new JLabel(tr("Proxy server host")), GBC.std()); 
    74         gui.connection.add(proxyHost, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    75         gui.connection.add(new JLabel(tr("Proxy server port")), GBC.std()); 
    76         gui.connection.add(proxyPort, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    77         gui.connection.add(proxyAnonymous, GBC.eop().insets(20, 0, 0, 0)); 
    78         gui.connection.add(new JLabel(tr("Proxy server username")), GBC.std()); 
    79         gui.connection.add(proxyUser, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    80         gui.connection.add(new JLabel(tr("Proxy server password")), GBC.std()); 
    81         gui.connection.add(proxyPass, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    82  
    83         gui.connection.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL)); 
     298        gui.connection.add(buildProxySettingsPanel(), GBC.eol().insets(20,10,0,0)); 
     299 
     300        initFromPreferences(); 
     301        updateEnabledState(); 
    84302    } 
    85303 
    86304    public boolean ok() { 
    87         Main.pref.put(PROXY_ENABLE, proxyEnable.isSelected()); 
    88         Main.pref.put(PROXY_HOST, proxyHost.getText()); 
    89         Main.pref.put(PROXY_PORT, proxyPort.getText()); 
    90         Main.pref.put(PROXY_ANONYMOUS, proxyAnonymous.isSelected()); 
    91         Main.pref.put(PROXY_USER, proxyUser.getText()); 
    92         Main.pref.put(PROXY_PASS, new String(proxyPass.getPassword())); 
     305        ProxyPolicy policy = null; 
     306        for (ProxyPolicy pp: ProxyPolicy.values()) { 
     307            if (rbProxyPolicy.get(pp).isSelected()) { 
     308                policy = pp; 
     309                break; 
     310            } 
     311        } 
     312        if (policy == null) { 
     313            policy = ProxyPolicy.NO_PROXY; 
     314        } 
     315        Main.pref.put(PROXY_POLICY, policy.getName()); 
     316        Main.pref.put(PROXY_HTTP_HOST, tfProxyHttpHost.getText()); 
     317        Main.pref.put(PROXY_HTTP_PORT, tfProxyHttpPort.getText()); 
     318        Main.pref.put(PROXY_SOCKS_HOST, tfProxySocksHost.getText()); 
     319        Main.pref.put(PROXY_SOCKS_PORT, tfProxySocksPort.getText()); 
     320        Main.pref.put(PROXY_USER, tfProxyHttpUser.getText()); 
     321        Main.pref.put(PROXY_PASS, String.valueOf(tfProxyHttpPassword.getPassword())); 
     322 
     323        // remove these legacy property keys 
     324        Main.pref.put("proxy.anonymous", null); 
     325        Main.pref.put("proxy.enable", null); 
     326 
     327        // update the proxy selector 
     328        ProxySelector selector = ProxySelector.getDefault(); 
     329        if (selector instanceof DefaultProxySelector) { 
     330            ((DefaultProxySelector)selector).initFromPreferences(); 
     331        } 
    93332        return false; 
    94333    } 
  • trunk/src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java

    r2017 r2641  
    22package org.openstreetmap.josm.gui.preferences; 
    33 
    4 import org.openstreetmap.josm.io.CredentialsManager; 
    5 import org.openstreetmap.josm.io.OsmConnection; 
     4import static org.openstreetmap.josm.tools.I18n.tr; 
     5 
     6import java.awt.Font; 
     7import java.net.PasswordAuthentication; 
     8import java.net.Authenticator.RequestorType; 
     9 
     10import javax.swing.JLabel; 
     11import javax.swing.JPasswordField; 
     12import javax.swing.JTextField; 
     13 
     14import org.openstreetmap.josm.Main; 
     15import org.openstreetmap.josm.io.auth.CredentialsManager; 
     16import org.openstreetmap.josm.io.auth.CredentialsManagerException; 
     17import org.openstreetmap.josm.io.auth.CredentialsManagerFactory; 
     18import org.openstreetmap.josm.tools.GBC; 
    619 
    720public class ServerAccessPreference implements PreferenceSetting { 
     
    1427 
    1528    /** 
    16      * Provide username and password input editfields. 
    17      * Store the values if user hits OK. 
     29     * Editfield for the Base url to the REST API from OSM. 
    1830     */ 
    19     private CredentialsManager.PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions(); 
     31    final private JTextField osmDataServerURL = new JTextField(20); 
     32    /** 
     33     * Editfield for the username to the OSM account. 
     34     */ 
     35    final private JTextField osmDataUsername = new JTextField(20); 
     36    /** 
     37     * Passwordfield for the userpassword of the REST API. 
     38     */ 
     39    final private JPasswordField osmDataPassword = new JPasswordField(20); 
    2040 
    2141    public void addGui(PreferenceDialog gui) { 
    22         credentialsPA.addPreferenceOptions(gui.connection); 
     42        CredentialsManager cm = CredentialsManagerFactory.getCredentialManager(); 
     43        String oldServerURL = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api"); 
     44        String oldUsername; 
     45        String oldPassword; 
     46        try { 
     47            PasswordAuthentication credentials =  cm.lookup(RequestorType.SERVER); 
     48            oldUsername = (credentials == null | credentials.getUserName() == null) ? "" : credentials.getUserName(); 
     49            oldPassword = (credentials == null | credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword()); 
     50        } catch(CredentialsManagerException e) { 
     51            e.printStackTrace(); 
     52            oldUsername = ""; 
     53            oldPassword = ""; 
     54        } 
     55 
     56        osmDataServerURL.setText(oldServerURL); 
     57        osmDataUsername.setText(oldUsername); 
     58        osmDataPassword.setText(oldPassword); 
     59        osmDataServerURL.setToolTipText(tr("The base URL for the OSM server (REST API)")); 
     60        osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account.")); 
     61        osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password.")); 
     62 
     63        gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std()); 
     64        gui.connection.add(osmDataServerURL, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     65        gui.connection.add(new JLabel(tr("OSM username (e-mail)")), GBC.std()); 
     66        gui.connection.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
     67        gui.connection.add(new JLabel(tr("OSM password")), GBC.std()); 
     68        gui.connection.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0)); 
     69        JLabel warning = new JLabel(tr("<html>" + 
     70                "WARNING: The password is stored in plain text in the preferences file.<br>" + 
     71                "The password is transferred in plain text to the server, encoded in the URL.<br>" + 
     72        "<b>Do not use a valuable Password.</b></html>")); 
     73        warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 
     74        gui.connection.add(warning, GBC.eop().fill(GBC.HORIZONTAL)); 
    2375    } 
    2476 
    2577    public boolean ok() { 
    26         credentialsPA.preferencesChanged(); 
     78        CredentialsManager cm = CredentialsManagerFactory.getCredentialManager(); 
     79        Main.pref.put("osm-server.url", osmDataServerURL.getText()); 
     80        try { 
     81            cm.store(RequestorType.SERVER, new PasswordAuthentication( 
     82                    osmDataUsername.getText(), 
     83                    osmDataPassword.getPassword() 
     84            )); 
     85        } catch(CredentialsManagerException e) { 
     86            // FIXME: Message dialog with an error message? 
     87            e.printStackTrace(); 
     88        } 
    2789        return false; 
    2890    } 
  • trunk/src/org/openstreetmap/josm/io/MultiPartFormOutputStream.java

    r2626 r2641  
    9292        this.out = new DataOutputStream(os); 
    9393        this.boundary = boundary; 
    94         initAuthentication(); 
    9594    } 
    9695 
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r2604 r2641  
    2323import java.util.HashMap; 
    2424 
     25import javax.xml.parsers.ParserConfigurationException; 
    2526import javax.xml.parsers.SAXParserFactory; 
    2627 
     
    147148     * @exception OsmApiInitializationException thrown, if an exception occurs 
    148149     */ 
    149     public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException { 
     150    public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException, OsmTransferCancelledException { 
    150151        if (initialized) 
    151152            return; 
    152153        cancel = false; 
    153         initAuthentication(); 
    154154        try { 
    155155            String s = sendRequest("GET", "capabilities", null,monitor, false); 
     
    169169            osmWriter.setVersion(version); 
    170170            initialized = true; 
    171         } catch (Exception ex) { 
     171        } catch(IOException e) { 
    172172            initialized = false; 
    173             throw new OsmApiInitializationException(ex); 
     173            throw new OsmApiInitializationException(e); 
     174        } catch(SAXException e) { 
     175            initialized = false; 
     176            throw new OsmApiInitializationException(e); 
     177        } catch(ParserConfigurationException e) { 
     178            initialized = false; 
     179            throw new OsmApiInitializationException(e); 
     180        } catch(OsmTransferCancelledException e){ 
     181            throw e; 
     182        } catch(OsmTransferException e) { 
     183            initialized = false; 
     184            throw new OsmApiInitializationException(e); 
    174185        } 
    175186    } 
     
    436447                monitor.setCustomText(tr("Starting retry {0} of {1} in {2} seconds ...", getMaxRetries() - retry,getMaxRetries(), 10-i)); 
    437448            } 
    438             if (cancel || isAuthCancelled()) 
     449            if (cancel) 
    439450                throw new OsmTransferCancelledException(); 
    440451            try { 
     
    563574                case HttpURLConnection.HTTP_GONE: 
    564575                    throw new OsmApiPrimitiveGoneException(errorHeader, errorBody); 
     576                case HttpURLConnection.HTTP_UNAUTHORIZED: 
     577                case HttpURLConnection.HTTP_PROXY_AUTH: 
     578                    // if we get here with HTTP_UNAUTHORIZED or HTTP_PROXY_AUTH the user canceled the 
     579                    // username/password dialog. Throw an OsmTransferCancelledException. 
     580                    // 
     581                    throw new OsmTransferCancelledException(); 
    565582                case HttpURLConnection.HTTP_CONFLICT: 
    566583                    if (ChangesetClosedException.errorHeaderMatchesPattern(errorHeader)) 
     
    583600                } 
    584601                throw new OsmTransferException(e); 
     602            } catch(IOException e){ 
     603                throw new OsmTransferException(e); 
     604            } catch(OsmTransferCancelledException e){ 
     605                throw e; 
    585606            } catch(OsmTransferException e) { 
    586607                throw e; 
    587             } catch (Exception e) { 
    588                 throw new OsmTransferException(e); 
    589608            } 
    590609        } 
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r2512 r2641  
    22package org.openstreetmap.josm.io; 
    33 
    4 import static org.openstreetmap.josm.tools.I18n.tr; 
    5  
    6 import java.awt.Font; 
    7 import java.awt.GridBagLayout; 
    8 import java.net.Authenticator; 
    94import java.net.HttpURLConnection; 
    10 import java.net.PasswordAuthentication; 
     5import java.net.Authenticator.RequestorType; 
    116import java.nio.ByteBuffer; 
    127import java.nio.CharBuffer; 
     
    1611import java.util.logging.Logger; 
    1712 
    18 import javax.swing.JCheckBox; 
    19 import javax.swing.JLabel; 
    20 import javax.swing.JPanel; 
    21 import javax.swing.JPasswordField; 
    22 import javax.swing.JTextField; 
    23  
    24 import org.openstreetmap.josm.Main; 
    25 import org.openstreetmap.josm.gui.ExtendedDialog; 
     13import org.openstreetmap.josm.io.auth.CredentialsManagerException; 
     14import org.openstreetmap.josm.io.auth.CredentialsManagerFactory; 
     15import org.openstreetmap.josm.io.auth.CredentialsManagerResponse; 
    2616import org.openstreetmap.josm.tools.Base64; 
    27 import org.openstreetmap.josm.tools.GBC; 
    2817 
    2918/** 
     
    3827    protected boolean cancel = false; 
    3928    protected HttpURLConnection activeConnection; 
    40     /** 
    41      * Handles password storage and some related gui-components. 
    42      * It can be set by a plugin. This may happen at startup and 
    43      * by changing the preferences. 
    44      * Syncronize on this object to get or set a consistent 
    45      * username/password pair. 
    46      */ 
    47     public static CredentialsManager credentialsManager = new PlainCredentialsManager(); 
    48  
    49     private static OsmAuth authentication = new OsmAuth(); 
    5029 
    5130    /** 
     
    5332     */ 
    5433    static { 
    55         // TODO: current authentication handling is sub-optimal in that it seems to use the same authenticator for 
    56         // any kind of request. HTTP requests executed by plugins, e.g. to password-protected WMS servers, 
    57         // will use the same username/password which is undesirable. 
    5834        try { 
    5935            HttpURLConnection.setFollowRedirects(true); 
    60             Authenticator.setDefault(authentication); 
    6136        } catch (SecurityException e) { 
     37            e.printStackTrace(); 
    6238        } 
    63     } 
    64  
    65     /** 
    66      * The authentication class handling the login requests. 
    67      */ 
    68     public static class OsmAuth extends Authenticator { 
    69         /** 
    70          * Set to true, when the autenticator tried the password once. 
    71          */ 
    72         public boolean passwordtried = false; 
    73         /** 
    74          * Whether the user cancelled the password dialog 
    75          */ 
    76         public boolean authCancelled = false; 
    77         @Override protected PasswordAuthentication getPasswordAuthentication() { 
    78             return credentialsManager.getPasswordAuthentication(this); 
    79         } 
    80     } 
    81  
    82     /** 
    83      * Must be called before each connection attemp to initialize the authentication. 
    84      */ 
    85     protected final void initAuthentication() { 
    86         authentication.authCancelled = false; 
    87         authentication.passwordtried = false; 
    88     } 
    89  
    90     /** 
    91      * @return Whether the connection was cancelled. 
    92      */ 
    93     protected final boolean isAuthCancelled() { 
    94         return authentication.authCancelled; 
    9539    } 
    9640 
     
    11559    } 
    11660 
    117     protected void addAuth(HttpURLConnection con) throws CharacterCodingException { 
     61    protected void addAuth(HttpURLConnection con) throws OsmTransferException { 
    11862        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); 
    119         String auth; 
     63        CredentialsManagerResponse response; 
     64        String token; 
    12065        try { 
    121             synchronized (credentialsManager) { 
    122                 auth = credentialsManager.lookup(CredentialsManager.Key.USERNAME) + ":" + 
    123                 credentialsManager.lookup(CredentialsManager.Key.PASSWORD); 
     66            synchronized (CredentialsManagerFactory.getCredentialManager()) { 
     67                response = CredentialsManagerFactory.getCredentialManager().getCredentials(RequestorType.SERVER, false /* don't know yet whether the credentials will succeed */); 
    12468            } 
    125         } catch (CredentialsManager.CMException e) { 
    126             auth = ":"; 
     69        } catch (CredentialsManagerException e) { 
     70            throw new OsmTransferException(e); 
    12771        } 
    128         ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth)); 
    129         con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes)); 
     72        if (response == null) { 
     73            token = ":"; 
     74        } else if (response.isCanceled()) { 
     75            cancel = true; 
     76            return; 
     77        } else { 
     78            String username= response.getUsername() == null ? "" : response.getUsername(); 
     79            String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword()); 
     80            token = username + ":" + password; 
     81            try { 
     82                ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token)); 
     83                con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes)); 
     84            } catch(CharacterCodingException e) { 
     85                throw new OsmTransferException(e); 
     86            } 
     87        } 
    13088    } 
    13189 
     
    13997        return cancel; 
    14098    } 
    141     /** 
    142      * Default implementation of the CredentialsManager interface. 
    143      * Saves passwords in plain text file. 
    144      */ 
    145     public static class PlainCredentialsManager implements CredentialsManager { 
    146         public String lookup(CredentialsManager.Key key) throws CMException { 
    147             String secret = Main.pref.get("osm-server." + key.toString(), null); 
    148             if (secret == null) throw new CredentialsManager.NoContentException(); 
    149             return secret; 
    150         } 
    151         public void store(CredentialsManager.Key key, String secret) { 
    152             Main.pref.put("osm-server." + key.toString(), secret); 
    153         } 
    154         public PasswordAuthentication getPasswordAuthentication(OsmAuth caller) { 
    155             String username, password; 
    156             try { 
    157                 username = lookup(Key.USERNAME); 
    158             } catch (CMException e) { 
    159                 username = ""; 
    160             } 
    161             try { 
    162                 password = lookup(Key.PASSWORD); 
    163             } catch (CMException e) { 
    164                 password = ""; 
    165             } 
    166             if (caller.passwordtried || username.equals("") || password.equals("")) { 
    167                 JPanel p = new JPanel(new GridBagLayout()); 
    168                 if (!username.equals("") && !password.equals("")) { 
    169                     p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop()); 
    170                 } 
    171                 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0)); 
    172                 JTextField usernameField = new JTextField(username, 20); 
    173                 p.add(usernameField, GBC.eol()); 
    174                 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0)); 
    175                 JPasswordField passwordField = new JPasswordField(password, 20); 
    176                 p.add(passwordField, GBC.eol()); 
    177                 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted.")); 
    178                 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 
    179                 p.add(warning, GBC.eop()); 
    180  
    181                 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), 
    182                         !username.equals("") && !password.equals("")); 
    183                 p.add(savePassword, GBC.eop()); 
    184  
    185                 ExtendedDialog dialog = new ExtendedDialog( 
    186                         Main.parent, 
    187                         tr("Enter Password"), 
    188                         new String[] {tr("Login"), tr("Cancel")} 
    189                 ); 
    190                 dialog.setContent(p); 
    191                 dialog.setButtonIcons( new String[] {"ok.png", "cancel.png"}); 
    192                 dialog.showDialog(); 
    193  
    194                 if (dialog.getValue() != 1) { 
    195                     caller.authCancelled = true; 
    196                     return null; 
    197                 } 
    198                 username = usernameField.getText(); 
    199                 password = String.valueOf(passwordField.getPassword()); 
    200                 if (savePassword.isSelected()) { 
    201                     store(Key.USERNAME, username); 
    202                     store(Key.PASSWORD, password); 
    203                 } 
    204                 if (username.equals("")) 
    205                     return null; 
    206             } 
    207             caller.passwordtried = true; 
    208             return new PasswordAuthentication(username, password.toCharArray()); 
    209         } 
    210         public PreferenceAdditions newPreferenceAdditions() { 
    211             return new PreferenceAdditions() { 
    212                 /** 
    213                  * Editfield for the Base url to the REST API from OSM. 
    214                  */ 
    215                 final private JTextField osmDataServerURL = new JTextField(20); 
    216                 /** 
    217                  * Editfield for the username to the OSM account. 
    218                  */ 
    219                 final private JTextField osmDataUsername = new JTextField(20); 
    220                 /** 
    221                  * Passwordfield for the userpassword of the REST API. 
    222                  */ 
    223                 final private JPasswordField osmDataPassword = new JPasswordField(20); 
    224  
    225                 private String oldServerURL = ""; 
    226                 private String oldUsername = ""; 
    227                 private String oldPassword = ""; 
    228  
    229                 public void addPreferenceOptions(JPanel panel) { 
    230                     try { 
    231                         oldServerURL = lookup(Key.OSM_SERVER_URL); // result is not null (see CredentialsManager) 
    232                     } catch (CMException e) { 
    233                         oldServerURL = ""; 
    234                     } 
    235                     if (oldServerURL.equals("")) { 
    236                         oldServerURL = "http://api.openstreetmap.org/api"; 
    237                     } 
    238                     try { 
    239                         oldUsername = lookup(Key.USERNAME); 
    240                     } catch (CMException e) { 
    241                         oldUsername = ""; 
    242                     } 
    243                     try { 
    244                         oldPassword = lookup(Key.PASSWORD); 
    245                     } catch (CMException e) { 
    246                         oldPassword = ""; 
    247                     } 
    248                     osmDataServerURL.setText(oldServerURL); 
    249                     osmDataUsername.setText(oldUsername); 
    250                     osmDataPassword.setText(oldPassword); 
    251                     osmDataServerURL.setToolTipText(tr("The base URL for the OSM server (REST API)")); 
    252                     osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account.")); 
    253                     osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password.")); 
    254                     panel.add(new JLabel(tr("Base Server URL")), GBC.std()); 
    255                     panel.add(osmDataServerURL, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    256                     panel.add(new JLabel(tr("OSM username (e-mail)")), GBC.std()); 
    257                     panel.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5)); 
    258                     panel.add(new JLabel(tr("OSM password")), GBC.std()); 
    259                     panel.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0)); 
    260                     JLabel warning = new JLabel(tr("<html>" + 
    261                             "WARNING: The password is stored in plain text in the preferences file.<br>" + 
    262                             "The password is transferred in plain text to the server, encoded in the URL.<br>" + 
    263                     "<b>Do not use a valuable Password.</b></html>")); 
    264                     warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 
    265                     panel.add(warning, GBC.eop().fill(GBC.HORIZONTAL)); 
    266                 } 
    267                 public void preferencesChanged() { 
    268                     String newServerURL = osmDataServerURL.getText(); 
    269                     String newUsername = osmDataUsername.getText(); 
    270                     String newPassword = String.valueOf(osmDataPassword.getPassword()); 
    271                     if (!oldServerURL.equals(newServerURL)) { 
    272                         store(Key.OSM_SERVER_URL, newServerURL); 
    273                     } 
    274                     if (!oldUsername.equals(newUsername)) { 
    275                         store(Key.USERNAME, newUsername); 
    276                     } 
    277                     if (!oldPassword.equals(newPassword)) { 
    278                         store(Key.PASSWORD, newPassword); 
    279                     } 
    280                 } 
    281             }; 
    282         } 
    283     } 
    28499} 
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r2512 r2641  
    1010import java.net.MalformedURLException; 
    1111import java.net.URL; 
    12 import java.nio.charset.CharacterCodingException; 
     12import java.util.logging.Logger; 
    1313import java.util.zip.GZIPInputStream; 
    1414import java.util.zip.Inflater; 
     
    2929 */ 
    3030public abstract class OsmServerReader extends OsmConnection { 
    31  
     31    static private final Logger logger = Logger.getLogger(OsmServerReader.class.getName()); 
    3232    private OsmApi api = OsmApi.getOsmApi(); 
    3333    private boolean doAuthenticate = false; 
     
    6868            } 
    6969 
    70             try { 
    71                 if (doAuthenticate) { 
    72                     addAuth(activeConnection); 
    73                 } 
    74             } catch(CharacterCodingException e) { 
    75                 System.err.println(tr("Error: failed to add authentication credentials to the connection.")); 
    76                 throw new OsmTransferException(e); 
     70            if (doAuthenticate) { 
     71                addAuth(activeConnection); 
    7772            } 
     73            if (cancel) 
     74                throw new OsmTransferCancelledException(); 
    7875            if (Main.pref.getBoolean("osm-server.use-compression", true)) { 
    7976                activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); 
     
    8683                activeConnection.connect(); 
    8784            } catch (Exception e) { 
     85                e.printStackTrace(); 
    8886                throw new OsmTransferException(tr("Couldn't connect to the OSM server. Please check your internet connection."), e); 
    8987            } 
    9088            try { 
    91                 if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 
     89                if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 
    9290                    throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null); 
     91 
     92                if (activeConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH) 
     93                    throw new OsmTransferCancelledException(); 
    9394 
    9495                if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java

    r2628 r2641  
    11// License: GPL. For details, see LICENSE file. 
    2 package org.openstreetmap.josm.io; 
     2package org.openstreetmap.josm.io.auth; 
    33 
    4 import org.openstreetmap.josm.io.OsmConnection.OsmAuth; 
     4import java.net.PasswordAuthentication; 
     5import java.net.Authenticator.RequestorType; 
    56 
    67/** 
    7  * Manages how username and password are stored. In addition all 
    8  * username/password-related user interaction is encapsulated here. 
     8 * A CredentialManager manages two credentials: 
     9 * <ul> 
     10 *   <li>the credential for {@see RequestorType#SERVER} which is equal to the OSM API credentials 
     11 *   in JOSM</li> 
     12 *   <li>the credential for {@see RequestorType#PROXY} which is equal to the credentials for an 
     13 *   optional HTTP proxy server a user may use</li> 
     14 *  </ul> 
    915 */ 
    1016public interface CredentialsManager { 
    11     enum Key { 
    12         OSM_SERVER_URL("url"), 
    13         USERNAME("username"), 
    14         PASSWORD("password"); 
    15         final private String pname; 
    16         private Key(String name) { 
    17             pname = name; 
    18         } 
    19         @Override public String toString() { 
    20             return pname; 
    21         } 
    22     }; 
    2317 
    2418    /** 
    25      * Should throw or return non-null, possibly empty String. 
     19     * Looks up the credentials for a given type. 
     20     *  
     21     * @param the type of service. {@see RequestorType#SERVER} for the OSM API server, {@see RequestorType#PROXY} 
     22     * for a proxy server 
     23     * @return the credentials 
     24     * @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface 
    2625     */ 
    27     public String lookup(Key key) throws CMException; 
     26    public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException; 
    2827 
    2928    /** 
    30      * May silently fail to store. 
     29     * Saves the credentials in <code>credentials</code> for the given service type. 
     30     *  
     31     * @param the type of service. {@see RequestorType#SERVER} for the OSM API server, {@see RequestorType#PROXY} 
     32     * for a proxy server 
     33     * @param credentials the credentials 
     34     * @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface 
    3135     */ 
    32     public void store(Key key, String secret) throws CMException; 
     36    public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException; 
    3337 
    3438    /** 
    35      * If authentication using the stored credentials fails, this method is 
    36      * called to promt for new username/password. 
     39     *  
     40     * @param requestorType  the type of service. {@see RequestorType#SERVER} for the OSM API server, {@see RequestorType#PROXY} 
     41     * for a proxy server 
     42     * @param noSuccessWithLastResponse true, if the last request with the supplied credentials failed; false otherwise. 
     43     * If true, implementations of this interface are adviced prompt user for new credentials. 
     44     * @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface 
     45 
    3746     */ 
    38     public java.net.PasswordAuthentication getPasswordAuthentication(OsmAuth caller); 
    39  
    40     /** 
    41      * Credentials-related preference gui. 
    42      */ 
    43     public interface PreferenceAdditions { 
    44         public void addPreferenceOptions(javax.swing.JPanel panel); 
    45         public void preferencesChanged(); 
    46     } 
    47     public PreferenceAdditions newPreferenceAdditions(); 
    48  
    49     public class CMException extends Exception { 
    50         public CMException() {super();} 
    51         public CMException(String message, Throwable cause) {super(message, cause);} 
    52         public CMException(String message) {super(message);} 
    53         public CMException(Throwable cause) {super(cause);} 
    54     } 
    55     public class NoContentException extends CMException { 
    56         public NoContentException() {super();} 
    57         public NoContentException(String message, Throwable cause) {super(message, cause);} 
    58         public NoContentException(String message) {super(message);} 
    59         public NoContentException(Throwable cause) {super(cause);} 
    60     } 
     47    public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException; 
    6148} 
  • trunk/test/functional/org/openstreetmap/josm/io/UploadStrategySelectionPanelTest.java

    r2600 r2641  
    5555    } 
    5656 
    57     public static void main(String args[]) throws OsmApiInitializationException { 
     57    public static void main(String args[]) throws OsmApiInitializationException, OsmTransferCancelledException{ 
    5858        JOSMFixture josmFixture = JOSMFixture.createFunctionalTestFixture(); 
    5959        OsmApi.getOsmApi().initialize(NullProgressMonitor.INSTANCE); 
Note: See TracChangeset for help on using the changeset viewer.