Changeset 7473 in josm for trunk/src/org
- Timestamp:
- 2014-08-31T01:30:58+02:00 (10 years ago)
- 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 11 11 12 12 import javax.swing.JOptionPane; 13 import javax.xml.parsers.ParserConfigurationException; 13 14 14 15 import org.openstreetmap.josm.Main; … … 16 17 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 17 18 import org.openstreetmap.josm.gui.help.HelpUtil; 18 import org.openstreetmap.josm.io.IllegalDataException; 19 import org.openstreetmap.josm.io.OsmChangesetParser; 19 import org.openstreetmap.josm.io.Capabilities; 20 20 import org.openstreetmap.josm.io.OsmTransferException; 21 21 import org.openstreetmap.josm.tools.CheckParameterUtil; 22 22 import org.openstreetmap.josm.tools.Utils; 23 import org.xml.sax.InputSource; 23 24 import org.xml.sax.SAXException; 24 25 25 26 /** 26 27 * 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 method28 * It tries to retrieve capabilities from the given URL. If it succeeds, the method 28 29 * {@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 also32 * to "https://x.y.y/api/0/capabilities" or "https://x.y.y/a/capabilities" with valid capabilities. If we get33 * valid capabilities with an URL we therefore can't be sure that the base URL is valid API URL.34 30 * @since 2745 35 31 */ … … 71 67 } 72 68 73 protected void alertInvalidC hangesetUrl(String url) {69 protected void alertInvalidCapabilitiesUrl(String url) { 74 70 HelpAwareOptionPane.showMessageDialogInEDT( 75 71 parent, … … 120 116 } 121 117 122 protected void alertInvalidC hangesetList() {118 protected void alertInvalidCapabilities() { 123 119 HelpAwareOptionPane.showMessageDialogInEDT( 124 120 parent, … … 150 146 151 147 /** 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 '/'. 154 149 * 155 150 * @return the normalized API URL … … 173 168 } 174 169 URL capabilitiesUrl; 175 String getC hangesetsUrl = getNormalizedApiUrl() + "/0.6/changesets";170 String getCapabilitiesUrl = getNormalizedApiUrl() + "/0.6/capabilities"; 176 171 try { 177 capabilitiesUrl = new URL(getC hangesetsUrl);172 capabilitiesUrl = new URL(getCapabilitiesUrl); 178 173 } catch(MalformedURLException e) { 179 alertInvalidC hangesetUrl(getChangesetsUrl);174 alertInvalidCapabilitiesUrl(getCapabilitiesUrl); 180 175 return; 181 176 } … … 195 190 196 191 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; 206 197 } 207 198 success = true; -
trunk/src/org/openstreetmap/josm/io/Capabilities.java
r7012 r7473 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.IOException; 6 7 import java.util.ArrayList; 7 8 import java.util.Collections; … … 10 11 import java.util.Map; 11 12 13 import javax.xml.parsers.ParserConfigurationException; 14 import javax.xml.parsers.SAXParserFactory; 15 12 16 import org.openstreetmap.josm.Main; 17 import org.xml.sax.Attributes; 18 import org.xml.sax.InputSource; 19 import org.xml.sax.SAXException; 20 import org.xml.sax.helpers.DefaultHandler; 13 21 14 22 /** 15 * Represents the server capabilities23 * Represents the OSM API server capabilities. 16 24 * 17 25 * Example capabilites document: … … 43 51 public class Capabilities { 44 52 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; 47 55 48 56 /** … … 50 58 */ 51 59 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 */ 55 71 public boolean isDefined(String element, String attribute) { 56 72 if (! capabilities.containsKey(element)) return false; … … 60 76 } 61 77 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) { 63 86 if (! capabilities.containsKey(element)) return null; 64 87 HashMap<String, String> e = capabilities.get(element); … … 68 91 69 92 /** 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 77 99 */ 78 100 public Double getDouble(String element, String attribute) throws NumberFormatException { … … 82 104 } 83 105 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 */ 84 114 public Long getLong(String element, String attribute) { 85 115 String s = get(element, attribute); … … 88 118 } 89 119 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 */ 90 127 public void put(String element, String attribute, String value) { 91 128 if ("blacklist".equals(element)) { … … 103 140 } 104 141 142 /** 143 * Clears the API capabilities. 144 */ 105 145 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 */ 110 155 public boolean supportsVersion(String version) { 111 156 return get("version", "minimum").compareTo(version) <= 0 … … 137 182 138 183 /** 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) { 145 189 if (url != null && imageryBlacklist != null) { 146 190 for (String blacklistRegex : imageryBlacklist) { … … 153 197 154 198 /** 155 * returns the full list ofblacklist 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() { 159 203 return Collections.unmodifiableList(imageryBlacklist); 160 204 } 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 } 161 249 } -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r7434 r7473 27 27 28 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.SAXParserFactory;30 29 31 30 import org.openstreetmap.josm.Main; … … 37 36 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 38 37 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 38 import org.openstreetmap.josm.io.Capabilities.CapabilitiesParser; 39 39 import org.openstreetmap.josm.tools.CheckParameterUtil; 40 40 import org.openstreetmap.josm.tools.Utils; 41 41 import org.openstreetmap.josm.tools.XmlParsingException; 42 import org.xml.sax.Attributes;43 42 import org.xml.sax.InputSource; 44 43 import org.xml.sax.SAXException; 45 44 import org.xml.sax.SAXParseException; 46 import org.xml.sax.helpers.DefaultHandler;47 45 48 46 /** … … 111 109 } 112 110 113 /** the server URL */111 /** Server URL */ 114 112 private String serverUrl; 115 113 116 /** 117 * Object describing current changeset 118 */ 114 /** Object describing current changeset */ 119 115 private Changeset changeset; 120 116 121 /** 122 * API version used for server communications 123 */ 117 /** API version used for server communications */ 124 118 private String version = null; 125 119 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 */ 132 124 private boolean initialized = false; 133 125 134 126 /** 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. 152 128 * 153 129 * @param serverUrl the server URL. Must not be null … … 293 269 294 270 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))); 297 272 } 298 273 … … 752 727 753 728 /** 754 * Replies the API capabilities 729 * Replies the API capabilities. 755 730 * 756 731 * @return the API capabilities, or null, if the API is not initialized yet -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r7436 r7473 653 653 throw new IllegalArgumentException("Invalid HTTP url"); 654 654 } 655 if (Main.isDebugEnabled()) { 656 Main.debug("Opening HTTP connection to "+httpURL.toExternalForm()); 657 } 655 658 HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection(); 656 659 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
Note:
See TracChangeset
for help on using the changeset viewer.