Changeset 7473 in josm for trunk/src


Ignore:
Timestamp:
2014-08-31T01:30:58+02:00 (5 years ago)
Author:
Don-vip
Message:

see #10441 - send capabilities request instead of changesets request when testing API url

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r7466 r7473  
    1111
    1212import javax.swing.JOptionPane;
     13import javax.xml.parsers.ParserConfigurationException;
    1314
    1415import org.openstreetmap.josm.Main;
     
    1617import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    1718import org.openstreetmap.josm.gui.help.HelpUtil;
    18 import org.openstreetmap.josm.io.IllegalDataException;
    19 import org.openstreetmap.josm.io.OsmChangesetParser;
     19import org.openstreetmap.josm.io.Capabilities;
    2020import org.openstreetmap.josm.io.OsmTransferException;
    2121import org.openstreetmap.josm.tools.CheckParameterUtil;
    2222import org.openstreetmap.josm.tools.Utils;
     23import org.xml.sax.InputSource;
    2324import org.xml.sax.SAXException;
    2425
    2526/**
    2627 * This is an asynchronous task for testing whether an URL points to an OSM API server.
    27  * It tries to retrieve a list of changesets from the given URL. If it succeeds, the method
     28 * It tries to retrieve capabilities from the given URL. If it succeeds, the method
    2829 * {@link #isSuccess()} replies true, otherwise false.
    29  *
    30  * Note: it fetches a list of changesets instead of the much smaller capabilities because - strangely enough -
    31  * an OSM server "https://x.y.y/api/0.6" not only responds to  "https://x.y.y/api/0.6/capabilities" but also
    32  * to "https://x.y.y/api/0/capabilities" or "https://x.y.y/a/capabilities" with valid capabilities. If we get
    33  * valid capabilities with an URL we therefore can't be sure that the base URL is valid API URL.
    3430 * @since 2745
    3531 */
     
    7167    }
    7268
    73     protected void alertInvalidChangesetUrl(String url) {
     69    protected void alertInvalidCapabilitiesUrl(String url) {
    7470        HelpAwareOptionPane.showMessageDialogInEDT(
    7571                parent,
     
    120116    }
    121117
    122     protected void alertInvalidChangesetList() {
     118    protected void alertInvalidCapabilities() {
    123119        HelpAwareOptionPane.showMessageDialogInEDT(
    124120                parent,
     
    150146
    151147    /**
    152      * Removes leading and trailing whitespace from the API URL and removes trailing
    153      * '/'.
     148     * Removes leading and trailing whitespace from the API URL and removes trailing '/'.
    154149     *
    155150     * @return the normalized API URL
     
    173168            }
    174169            URL capabilitiesUrl;
    175             String getChangesetsUrl = getNormalizedApiUrl() + "/0.6/changesets";
     170            String getCapabilitiesUrl = getNormalizedApiUrl() + "/0.6/capabilities";
    176171            try {
    177                 capabilitiesUrl = new URL(getChangesetsUrl);
     172                capabilitiesUrl = new URL(getCapabilitiesUrl);
    178173            } catch(MalformedURLException e) {
    179                 alertInvalidChangesetUrl(getChangesetsUrl);
     174                alertInvalidCapabilitiesUrl(getCapabilitiesUrl);
    180175                return;
    181176            }
     
    195190
    196191            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;
    205                 }
     192                Capabilities.CapabilitiesParser.parse(new InputSource(connection.getInputStream()));
     193            } catch (SAXException | ParserConfigurationException e) {
     194                Main.warn(e.getMessage());
     195                alertInvalidCapabilities();
     196                return;
    206197            }
    207198            success = true;
  • trunk/src/org/openstreetmap/josm/io/Capabilities.java

    r7012 r7473  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.IOException;
    67import java.util.ArrayList;
    78import java.util.Collections;
     
    1011import java.util.Map;
    1112
     13import javax.xml.parsers.ParserConfigurationException;
     14import javax.xml.parsers.SAXParserFactory;
     15
    1216import org.openstreetmap.josm.Main;
     17import org.xml.sax.Attributes;
     18import org.xml.sax.InputSource;
     19import org.xml.sax.SAXException;
     20import org.xml.sax.helpers.DefaultHandler;
    1321
    1422/**
    15  * Represents the server capabilities
     23 * Represents the OSM API server capabilities.
    1624 *
    1725 * Example capabilites document:
     
    4351public class Capabilities {
    4452
    45     private Map<String, HashMap<String,String>> capabilities;
    46     private List<String> imageryBlacklist;
     53    private final Map<String, HashMap<String,String>> capabilities;
     54    private final List<String> imageryBlacklist;
    4755
    4856    /**
     
    5058     */
    5159    public Capabilities() {
    52         clear();
    53     }
    54 
     60        capabilities = new HashMap<>();
     61        imageryBlacklist = new ArrayList<>();
     62    }
     63
     64    /**
     65     * Determines if given element and attribute are defined.
     66     *
     67     * @param element the name of the element
     68     * @param attribute the name of the attribute
     69     * @return {@code true} if defined, {@code false} otherwise
     70     */
    5571    public boolean isDefined(String element, String attribute) {
    5672        if (! capabilities.containsKey(element)) return false;
     
    6076    }
    6177
    62     public String get(String element, String attribute ) {
     78    /**
     79     * Returns the value of configuration item in the capabilities as string value.
     80     *
     81     * @param element the name of the element
     82     * @param attribute the name of the attribute
     83     * @return the value; {@code null}, if the respective configuration item does not exist
     84     */
     85    public String get(String element, String attribute) {
    6386        if (! capabilities.containsKey(element)) return null;
    6487        HashMap<String, String> e = capabilities.get(element);
     
    6891
    6992    /**
    70      * returns the value of configuration item in the capabilities as
    71      * double value
    72      *
    73      * @param element  the name of the element
    74      * @param attribute the name of the attribute
    75      * @return the value; null, if the respective configuration item doesn't exist
    76      * @throws NumberFormatException  if the value is not a valid double
     93     * Returns the value of configuration item in the capabilities as double value.
     94     *
     95     * @param element the name of the element
     96     * @param attribute the name of the attribute
     97     * @return the value; {@code null}, if the respective configuration item does not exist
     98     * @throws NumberFormatException if the value is not a valid double
    7799     */
    78100    public Double getDouble(String element, String attribute) throws NumberFormatException {
     
    82104    }
    83105
     106    /**
     107     * Returns the value of configuration item in the capabilities as long value.
     108     *
     109     * @param element the name of the element
     110     * @param attribute the name of the attribute
     111     * @return the value; {@code null}, if the respective configuration item does not exist
     112     * @throws NumberFormatException if the value is not a valid long
     113     */
    84114    public Long getLong(String element, String attribute) {
    85115        String s = get(element, attribute);
     
    88118    }
    89119
     120    /**
     121     * Adds a new configuration item.
     122     *
     123     * @param element the name of the element
     124     * @param attribute the name of the attribute
     125     * @param value the value as string
     126     */
    90127    public void put(String element, String attribute, String value) {
    91128        if ("blacklist".equals(element)) {
     
    103140    }
    104141
     142    /**
     143     * Clears the API capabilities.
     144     */
    105145    public final void clear() {
    106         capabilities = new HashMap<>();
    107         imageryBlacklist = new ArrayList<>();
    108     }
    109 
     146        capabilities.clear();
     147        imageryBlacklist.clear();
     148    }
     149
     150    /**
     151     * Determines if a given API version is supported.
     152     * @param version The API version to check
     153     * @return {@code true} is version is between the minimum supported version and the maximum one, {@code false} otherwise
     154     */
    110155    public boolean supportsVersion(String version) {
    111156        return get("version", "minimum").compareTo(version) <= 0
     
    137182
    138183    /**
    139      * checks if the given URL is blacklisted by one of the of the
    140      * regular expressions.
    141      */
    142 
    143     public boolean isOnImageryBlacklist(String url)
    144     {
     184     * Checks if the given URL is blacklisted by one of the of the regular expressions.
     185     * @param url Imagery URL to check
     186     * @return {@code true} if URL is blacklisted, {@code false} otherwise
     187     */
     188    public boolean isOnImageryBlacklist(String url) {
    145189        if (url != null && imageryBlacklist != null) {
    146190            for (String blacklistRegex : imageryBlacklist) {
     
    153197
    154198    /**
    155      * returns the full list of blacklist regular expressions.
    156      */
    157     public List<String> getImageryBlacklist()
    158     {
     199     * Returns the full list of imagery blacklist regular expressions.
     200     * @return full list of imagery blacklist regular expressions
     201     */
     202    public List<String> getImageryBlacklist() {
    159203        return Collections.unmodifiableList(imageryBlacklist);
    160204    }
     205
     206    /**
     207     * A parser for the "capabilities" response XML.
     208     * @since 7473
     209     */
     210    public static final class CapabilitiesParser extends DefaultHandler {
     211
     212        private Capabilities capabilities;
     213
     214        @Override
     215        public void startDocument() throws SAXException {
     216            capabilities = new Capabilities();
     217        }
     218
     219        @Override
     220        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
     221            for (int i=0; i< atts.getLength(); i++) {
     222                capabilities.put(qName, atts.getQName(i), atts.getValue(i));
     223            }
     224        }
     225
     226        /**
     227         * Returns the read capabilities.
     228         * @return the read capabilities
     229         */
     230        public Capabilities getCapabilities() {
     231            return capabilities;
     232        }
     233
     234        /**
     235         * Parses and returns capabilities from the given input source.
     236         *
     237         * @param inputSource The input source to read capabilities from
     238         * @return the capabilities
     239         * @throws SAXException if any SAX errors occur during processing
     240         * @throws IOException if any I/O errors occur
     241         * @throws ParserConfigurationException if a parser cannot be created
     242         */
     243        public static Capabilities parse(InputSource inputSource) throws SAXException, IOException, ParserConfigurationException {
     244            CapabilitiesParser parser = new CapabilitiesParser();
     245            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, parser);
     246            return parser.getCapabilities();
     247        }
     248    }
    161249}
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r7434 r7473  
    2727
    2828import javax.xml.parsers.ParserConfigurationException;
    29 import javax.xml.parsers.SAXParserFactory;
    3029
    3130import org.openstreetmap.josm.Main;
     
    3736import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3837import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     38import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser;
    3939import org.openstreetmap.josm.tools.CheckParameterUtil;
    4040import org.openstreetmap.josm.tools.Utils;
    4141import org.openstreetmap.josm.tools.XmlParsingException;
    42 import org.xml.sax.Attributes;
    4342import org.xml.sax.InputSource;
    4443import org.xml.sax.SAXException;
    4544import org.xml.sax.SAXParseException;
    46 import org.xml.sax.helpers.DefaultHandler;
    4745
    4846/**
     
    111109    }
    112110
    113     /** the server URL */
     111    /** Server URL */
    114112    private String serverUrl;
    115113
    116     /**
    117      * Object describing current changeset
    118      */
     114    /** Object describing current changeset */
    119115    private Changeset changeset;
    120116
    121     /**
    122      * API version used for server communications
    123      */
     117    /** API version used for server communications */
    124118    private String version = null;
    125119
    126     /** the api capabilities */
    127     private Capabilities capabilities = new Capabilities();
    128 
    129     /**
    130      * true if successfully initialized
    131      */
     120    /** API capabilities */
     121    private Capabilities capabilities = null;
     122
     123    /** true if successfully initialized */
    132124    private boolean initialized = false;
    133125
    134126    /**
    135      * A parser for the "capabilities" response XML
    136      */
    137     private class CapabilitiesParser extends DefaultHandler {
    138         @Override
    139         public void startDocument() throws SAXException {
    140             capabilities.clear();
    141         }
    142 
    143         @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    144             for (int i=0; i< atts.getLength(); i++) {
    145                 capabilities.put(qName, atts.getQName(i), atts.getValue(i));
    146             }
    147         }
    148     }
    149 
    150     /**
    151      * creates an OSM api for a specific server URL
     127     * Constructs a new {@code OsmApi} for a specific server URL.
    152128     *
    153129     * @param serverUrl the server URL. Must not be null
     
    293269
    294270    private void initializeCapabilities(String xml) throws SAXException, IOException, ParserConfigurationException {
    295         InputSource inputSource = new InputSource(new StringReader(xml));
    296         SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
     271        capabilities = CapabilitiesParser.parse(new InputSource(new StringReader(xml)));
    297272    }
    298273
     
    752727
    753728    /**
    754      * Replies the API capabilities
     729     * Replies the API capabilities.
    755730     *
    756731     * @return the API capabilities, or null, if the API is not initialized yet
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r7436 r7473  
    653653            throw new IllegalArgumentException("Invalid HTTP url");
    654654        }
     655        if (Main.isDebugEnabled()) {
     656            Main.debug("Opening HTTP connection to "+httpURL.toExternalForm());
     657        }
    655658        HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
    656659        connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
Note: See TracChangeset for help on using the changeset viewer.