source: josm/trunk/src/org/openstreetmap/josm/io/Capabilities.java@ 3965

Last change on this file since 3965 was 3934, checked in by framm, 13 years ago

Changed the way in which JOSM handles imagery layer blacklisting. Instead
of a hard-coded list of Google URLs, we now parse the server's
/api/capabilities response which is expected to look like this:

<osm version="0.6" generator="OpenStreetMap server">

<api>

<version minimum="0.6" maximum="0.6"/>
<area maximum="0.25"/>
<tracepoints per_page="5000"/>
<waynodes maximum="2000"/>
<changesets maximum_elements="50000"/>
<timeout seconds="300"/>

</api>
<policy>

<imagery>

<blacklist regex=".*\.google\.com/.*"/>
<blacklist regex=".*209\.85\.2\d\d.*"/>
<blacklist regex=".*209\.85\.1[3-9]\d.*"/>
<blacklist regex=".*209\.85\.12[89].*"/>

</imagery>

</policy>

</osm>

JOSM will now try to establish an API connection when started, so that
it knows about blacklisted layers. It will also re-read the list when
the URL is changed in the preferences, and if any prohibited layers are
active they will be removed.

For an interim period, JOSM still uses the four regular expressions
listed above whenever the server API URL is *.openstreetmap.org and
the API does not send any blacklist entries. It is expected that the
API will soon return proper blacklist entries.

Things that could be improved:

  1. Establish a general listener system where components can register

their interest in a change of the configured OSM server. Currently
we have to plug through to the gui layer (Main.main.mapView) from
the base comms layer (OsmApi) which is ugly.

  1. Establish a new class of blacklist which works by IP number and not

just regular expression, so that you can say "any host name that resolves
to this IP is blacklisted".

  1. Track all layers that were used in editing a data set, and refuse

uploading if the upload URL bans any of these layers.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.List;
10
11/**
12 * Represents the server capabilities
13 *
14 * Example capabilites document:
15 *
16 * <osm version="0.6" generator="OpenStreetMap server">
17 * <api>
18 * <version minimum="0.6" maximum="0.6"/>
19 * <area maximum="0.25"/>
20 * <tracepoints per_page="5000"/>
21 * <waynodes maximum="2000"/>
22 * <changesets maximum_elements="50000"/>
23 * <timeout seconds="300"/>
24 * </api>
25 * <policy>
26 * <imagery>
27 * <blacklist regex=".*\.google\.com/.*"/>
28 * <blacklist regex=".*209\.85\.2\d\d.*"/>
29 * <blacklist regex=".*209\.85\.1[3-9]\d.*"/>
30 * <blacklist regex=".*209\.85\.12[89].*"/>
31 * </imagery>
32 * </policy>
33 * </osm>
34 *
35 * This class is used in conjunction with a very primitive parser
36 * and simply stuffs the each tag and its attributes into a hash
37 * of hashes, with the exception of the "blacklist" tag which gets
38 * a list of its own. The DOM hierarchy is disregarded.
39 */
40public class Capabilities {
41
42 private HashMap<String, HashMap<String,String>> capabilities;
43 private ArrayList<String> imageryBlacklist;
44
45 public Capabilities() {
46 clear();
47 }
48
49 public boolean isDefined(String element, String attribute) {
50 if (! capabilities.containsKey(element)) return false;
51 HashMap<String, String> e = capabilities.get(element);
52 if (e == null) return false;
53 return (e.get(attribute) != null);
54 }
55
56 public String get(String element, String attribute ) {
57 if (! capabilities.containsKey(element)) return null;
58 HashMap<String, String> e = capabilities.get(element);
59 if (e == null) return null;
60 return e.get(attribute);
61 }
62
63 /**
64 * returns the value of configuration item in the capabilities as
65 * double value
66 *
67 * @param element the name of the element
68 * @param attribute the name of the attribute
69 * @return the value; null, if the respective configuration item doesn't exist
70 * @throws NumberFormatException if the value is not a valid double
71 */
72 public Double getDouble(String element, String attribute) throws NumberFormatException {
73 String s = get(element, attribute);
74 if (s == null) return null;
75 return Double.parseDouble(s);
76 }
77
78 public Long getLong(String element, String attribute) {
79 String s = get(element, attribute);
80 if (s == null) return null;
81 return Long.parseLong(s);
82 }
83
84 public void put(String element, String attribute, String value) {
85 if (element.equals("blacklist")) {
86 if (attribute.equals("regex")) {
87 imageryBlacklist.add(value);
88 }
89 } else {
90 if (! capabilities.containsKey(element)) {
91 HashMap<String,String> h = new HashMap<String, String>();
92 capabilities.put(element, h);
93 }
94 HashMap<String, String> e = capabilities.get(element);
95 e.put(attribute, value);
96 }
97 }
98
99 public void clear() {
100 capabilities = new HashMap<String, HashMap<String,String>>();
101 imageryBlacklist = new ArrayList<String>();
102 }
103
104 public boolean supportsVersion(String version) {
105 return get("version", "minimum").compareTo(version) <= 0
106 && get("version", "maximum").compareTo(version) >= 0;
107 }
108
109 /**
110 * Returns the max number of objects in a changeset. -1 if either the capabilities
111 * don't include this parameter or if the parameter value is illegal (not a number,
112 * a negative number)
113 *
114 * @return the max number of objects in a changeset
115 */
116 public int getMaxChangesetSize() {
117 String v = get("changesets", "maximum_elements");
118 if (v == null) return -1;
119 try {
120 int n = Integer.parseInt(v);
121 if (n <= 0) {
122 System.err.println(tr("Warning: illegal value of attribute ''{0}'' of element ''{1}'' in server capabilities. Got ''{2}''", "changesets", "maximum_elements", n ));
123 return -1;
124 }
125 return n;
126 } catch(NumberFormatException e) {
127 System.err.println(tr("Warning: illegal value of attribute ''{0}'' of element ''{1}'' in server capabilities. Got ''{2}''", "changesets", "maximum_elements", v ));
128 return -1;
129 }
130 }
131
132 /**
133 * checks if the given URL is blacklisted by one of the of the
134 * regular expressions.
135 */
136
137 public boolean isOnImageryBlacklist(String url)
138 {
139 for (String blacklistRegex : imageryBlacklist) {
140 if (url.matches(blacklistRegex)) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147 /**
148 * returns the full list of blacklist regular expressions.
149 */
150 public List<String> getImageryBlacklist()
151 {
152 return Collections.unmodifiableList(imageryBlacklist);
153 }
154}
Note: See TracBrowser for help on using the repository browser.