Ignore:
Timestamp:
2014-08-30T12:58:39+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #10441 - wrong parsing of changesets list in API URL test task + fix EDT violations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java

    r7082 r7466  
    55
    66import java.awt.Component;
    7 import java.io.BufferedReader;
    87import java.io.IOException;
    9 import java.io.InputStreamReader;
    108import java.net.HttpURLConnection;
    119import java.net.MalformedURLException;
    1210import java.net.URL;
    13 import java.nio.charset.StandardCharsets;
    1411
    1512import javax.swing.JOptionPane;
     
    1916import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2017import org.openstreetmap.josm.gui.help.HelpUtil;
     18import org.openstreetmap.josm.io.IllegalDataException;
     19import org.openstreetmap.josm.io.OsmChangesetParser;
    2120import org.openstreetmap.josm.io.OsmTransferException;
    2221import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    3332 * to "https://x.y.y/api/0/capabilities" or "https://x.y.y/a/capabilities" with valid capabilities. If we get
    3433 * valid capabilities with an URL we therefore can't be sure that the base URL is valid API URL.
    35  *
     34 * @since 2745
    3635 */
    37 public class ApiUrlTestTask extends PleaseWaitRunnable{
    38 
    39     private String url;
     36public class ApiUrlTestTask extends PleaseWaitRunnable {
     37
     38    private final String url;
    4039    private boolean canceled;
    4140    private boolean success;
    42     private Component parent;
     41    private final Component parent;
    4342    private HttpURLConnection connection;
    4443
    4544    /**
    46      * Creates the task
     45     * Constructs a new {@code ApiUrlTestTask}.
    4746     *
    4847     * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog is displayed
     
    5857
    5958    protected void alertInvalidUrl(String url) {
    60         HelpAwareOptionPane.showOptionDialog(
     59        HelpAwareOptionPane.showMessageDialogInEDT(
    6160                parent,
    6261                tr("<html>"
     
    7372
    7473    protected void alertInvalidChangesetUrl(String url) {
    75         HelpAwareOptionPane.showOptionDialog(
     74        HelpAwareOptionPane.showMessageDialogInEDT(
    7675                parent,
    7776                tr("<html>"
     
    8988
    9089    protected void alertConnectionFailed() {
    91         HelpAwareOptionPane.showOptionDialog(
     90        HelpAwareOptionPane.showMessageDialogInEDT(
    9291                parent,
    9392                tr("<html>"
     
    105104
    106105    protected void alertInvalidServerResult(int retCode) {
    107         HelpAwareOptionPane.showOptionDialog(
     106        HelpAwareOptionPane.showMessageDialogInEDT(
    108107                parent,
    109108                tr("<html>"
     
    122121
    123122    protected void alertInvalidChangesetList() {
    124         HelpAwareOptionPane.showOptionDialog(
     123        HelpAwareOptionPane.showMessageDialogInEDT(
    125124                parent,
    126125                tr("<html>"
     
    194193                return;
    195194            }
    196             StringBuilder changesets = new StringBuilder();
    197             try (BufferedReader bin = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
    198                 String line;
    199                 while ((line = bin.readLine()) != null) {
    200                     changesets.append(line).append("\n");
     195
     196            try {
     197                OsmChangesetParser.parse(connection.getInputStream(), progressMonitor.createSubTaskMonitor(1, true));
     198            } catch (IllegalDataException e) {
     199                if (e.getCause() instanceof IOException) {
     200                    throw (IOException) e.getCause();
     201                } else {
     202                    Main.warn(e.getMessage());
     203                    alertInvalidChangesetList();
     204                    return;
    201205                }
    202             }
    203             if (! (changesets.toString().contains("<osm") && changesets.toString().contains("</osm>"))) {
    204                 // heuristic: if there isn't an opening and closing "<osm>" tag in the returned content,
    205                 // then we didn't get a list of changesets in return. Could be replaced by explicitly parsing
    206                 // the result but currently not worth the effort.
    207                 alertInvalidChangesetList();
    208                 return;
    209206            }
    210207            success = true;
Note: See TracChangeset for help on using the changeset viewer.