Ticket #24970: patch-Fix-Sonar-issues-in-ApiUrlTestTask.patch

File patch-Fix-Sonar-issues-in-ApiUrlTestTask.patch, 3.6 KB (added by DanProgs <Abenteurer.2901@…>, 4 days ago)
  • src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
    index 9681092..ab63828 100644
     
    77import java.io.IOException;
    88import java.net.HttpURLConnection;
    99import java.net.MalformedURLException;
     10import java.net.URI;
    1011import java.net.URL;
    1112
    1213import javax.swing.JOptionPane;
    … …  
    3738    private boolean success;
    3839    private final Component parent;
    3940    private HttpClient connection;
     41   
     42    private static final String CONNECTION_FAILED_MSG = "Connection to API failed";
    4043
    4144    /**
    4245     * Constructs a new {@code ApiUrlTestTask}.
    … …  
    9396                        url,
    9497                        url
    9598                ),
    96                 tr("Connection to API failed"),
     99                tr(CONNECTION_FAILED_MSG),
    97100                JOptionPane.ERROR_MESSAGE,
    98101                HelpUtil.ht("/Preferences/Connection#ConnectionToAPIFailed")
    99102        );
    … …  
    110113                        retCode,
    111114                        url
    112115                ),
    113                 tr("Connection to API failed"),
     116                tr(CONNECTION_FAILED_MSG),
    114117                JOptionPane.ERROR_MESSAGE,
    115118                HelpUtil.ht("/Preferences/Connection#InvalidServerResult")
    116119        );
    … …  
    126129                        + "</html>",
    127130                        url
    128131                ),
    129                 tr("Connection to API failed"),
     132                tr(CONNECTION_FAILED_MSG),
    130133                JOptionPane.ERROR_MESSAGE,
    131134                HelpUtil.ht("/Preferences/Connection#InvalidSettings")
    132135        );
    … …  
    151154    protected void realRun() throws SAXException, IOException, OsmTransferException {
    152155        try {
    153156            try {
    154                 new URL(url);
    155             } catch (MalformedURLException e) {
     157                URI.create(url).toURL();
     158            } catch (IllegalArgumentException | MalformedURLException e) {
    156159                alertInvalidUrl(url);
    157160                return;
    158161            }
    159             URL capabilitiesUrl;
    160             String getCapabilitiesUrl = url + "/0.6/capabilities";
     162            final URL capabilitiesUrl;
     163            final String getCapabilitiesUrl = url + "/0.6/capabilities";
    161164            try {
    162                 capabilitiesUrl = new URL(getCapabilitiesUrl);
    163             } catch (MalformedURLException e) {
     165                capabilitiesUrl = URI.create(getCapabilitiesUrl).toURL();
     166            } catch (IllegalArgumentException | MalformedURLException e) {
    164167                alertInvalidCapabilitiesUrl(getCapabilitiesUrl);
    165168                return;
    166169            }
    … …  
    170173                connection.connect();
    171174            }
    172175
    173             if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) {
    174                 alertInvalidServerResult(connection.getResponse().getResponseCode());
     176            final int responseCode = connection.getResponse().getResponseCode();
     177            if (responseCode  != HttpURLConnection.HTTP_OK) {
     178                alertInvalidServerResult(responseCode );
    175179                return;
    176180            }
    177181
    … …  
    184188            }
    185189            success = true;
    186190        } catch (IOException e) {
    187             if (canceled)
    188                 // ignore exceptions
     191            if (canceled) {
     192                // ignore exceptions
    189193                return;
     194            }
    190195            Logging.error(e);
    191196            alertConnectionFailed();
    192197        }