Changeset 1664 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-06-11T23:04:51+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r1663 r1664 21 21 import java.util.ArrayList; 22 22 import java.util.Collection; 23 import java.util.HashMap; 23 24 import java.util.Properties; 24 25 import java.util.StringTokenizer; … … 51 52 static public final int DEFAULT_MAX_NUM_RETRIES = 5; 52 53 54 /** the collection of instantiated OSM APIs */ 55 private static HashMap<String, OsmApi> instances = new HashMap<String, OsmApi>(); 56 57 /** 58 * replies the {@see OsmApi} for a given server URL 59 * 60 * @param serverUrl the server URL 61 * @return the OsmApi 62 * @throws IllegalArgumentException thrown, if serverUrl is null 63 * 64 */ 65 static public OsmApi getOsmApi(String serverUrl) { 66 OsmApi api = instances.get(serverUrl); 67 if (api == null) { 68 api = new OsmApi(serverUrl); 69 } 70 return api; 71 } 72 /** 73 * replies the {@see OsmApi} for the URL given by the preference <code>osm-server.url</code> 74 * 75 * @return the OsmApi 76 * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set 77 * 78 */ 79 static public OsmApi getOsmApi() { 80 String serverUrl = Main.pref.get("osm-server.url"); 81 if (serverUrl == null) 82 throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url")); 83 return getOsmApi(serverUrl); 84 } 85 86 /** the server URL */ 87 private String serverUrl; 88 53 89 /** 54 90 * Object describing current changeset … … 64 100 * Minimum API version accepted by server, from capabilities response 65 101 */ 66 private String minVersion = null;102 //private String minVersion = null; 67 103 68 104 /** 69 105 * Maximum API version accepted by server, from capabilities response 70 106 */ 71 private String maxVersion = null;107 //private String maxVersion = null; 72 108 73 109 /** … … 77 113 private String maxArea = null; 78 114 115 /** the api capabilities */ 116 private Capabilities capabilities = new Capabilities(); 117 79 118 /** 80 119 * true if successfully initialized … … 89 128 */ 90 129 private class CapabilitiesParser extends DefaultHandler { 130 @Override 131 public void startDocument() throws SAXException { 132 capabilities.clear(); 133 } 134 91 135 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 92 if (qName.equals("version")) { 93 minVersion = atts.getValue("minimum"); 94 maxVersion = atts.getValue("maximum"); 95 } else if (qName.equals("area")) { 96 maxArea = atts.getValue("maximum"); 136 for (int i=0; i< qName.length(); i++) { 137 capabilities.put(qName, atts.getQName(i), atts.getValue(i)); 97 138 } 98 139 } 140 } 141 142 /** 143 * creates an OSM api for a specific server URL 144 * 145 * @param serverUrl the server URL. Must not be null 146 * @exception IllegalArgumentException thrown, if serverUrl is null 147 */ 148 protected OsmApi(String serverUrl) { 149 if (serverUrl == null) 150 throw new IllegalArgumentException(tr("parameter '{0} must not be null", "serverUrl")); 151 this.serverUrl = serverUrl; 152 } 153 154 /** 155 * creates an instance of the OSM API. Initializes the server URL with the 156 * value of the preference <code>osm-server.url</code> 157 * 158 * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set 159 */ 160 protected OsmApi() { 161 this.serverUrl = Main.pref.get("osm-server.url"); 162 if (serverUrl == null) 163 throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url")); 99 164 } 100 165 … … 136 201 * @exception Exception any other exception 137 202 */ 138 public void initialize() throws UnknownHostException,SocketTimeoutException, ConnectException,Exception { 203 public void initialize() throws OsmApiInitializationException { 204 if (initialized) 205 return; 139 206 initAuthentication(); 140 207 try { 141 initialized = true; // note: has to be before the sendRequest or that will throw! 142 String capabilities = sendRequest("GET", "capabilities", null); 143 InputSource inputSource = new InputSource(new StringReader(capabilities)); 208 String s = sendRequest("GET", "capabilities", null); 209 InputSource inputSource = new InputSource(new StringReader(s)); 144 210 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser()); 145 if ( maxVersion.compareTo("0.6") >= 0) {211 if (capabilities.supportsVersion("0.6")) { 146 212 version = "0.6"; 147 } else if ( minVersion.compareTo("0.5") <= 0) {213 } else if (capabilities.supportsVersion("0.5")) { 148 214 version = "0.5"; 149 215 } else { 150 216 System.err.println(tr("This version of JOSM is incompatible with the configured server.")); 151 217 System.err.println(tr("It supports protocol versions 0.5 and 0.6, while the server says it supports {0} to {1}.", 152 minVersion, maxVersion));218 capabilities.get("version", "minimum"), capabilities.get("version", "maximum"))); 153 219 initialized = false; 154 220 } 155 221 System.out.println(tr("Communications with {0} established using protocol version {1}", 156 Main.pref.get("osm-server.url"),222 serverUrl, 157 223 version)); 158 224 osmWriter.setVersion(version); 225 initialized = true; 159 226 } catch (Exception ex) { 160 227 initialized = false; 161 throw ex;228 throw new OsmApiInitializationException(ex); 162 229 } 163 230 } … … 215 282 */ 216 283 public String getBaseUrl() { 217 StringBuffer rv = new StringBuffer( Main.pref.get("osm-server.url"));284 StringBuffer rv = new StringBuffer(serverUrl); 218 285 if (version != null) { 219 286 rv.append("/"); … … 235 302 */ 236 303 public void createPrimitive(OsmPrimitive osm) throws OsmTransferException { 304 initialize(); 237 305 osm.id = parseLong(sendRequest("PUT", which(osm)+"/create", toXml(osm, true))); 238 306 osm.version = 1; … … 248 316 */ 249 317 public void modifyPrimitive(OsmPrimitive osm) throws OsmTransferException { 318 initialize(); 250 319 if (version.equals("0.5")) { 251 320 // legacy mode does not return the new object version. … … 263 332 */ 264 333 public void deletePrimitive(OsmPrimitive osm) throws OsmTransferException { 334 initialize(); 265 335 // legacy mode does not require payload. normal mode (0.6 and up) requires payload for version matching. 266 336 sendRequest("DELETE", which(osm)+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false)); … … 288 358 */ 289 359 public void stopChangeset() throws OsmTransferException { 360 initialize(); 290 361 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 291 362 sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null); … … 306 377 throw new OsmTransferException(tr("No changeset present for diff upload")); 307 378 379 initialize(); 308 380 final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>(); 309 310 381 311 382 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this); … … 362 433 String requestBody) throws OsmTransferException { 363 434 364 if (!initialized) throw new OsmTransferException(tr("Not initialized"));365 366 435 StringBuffer responseBody = new StringBuffer(); 367 436 368 437 int retries = Main.pref.getInteger("osm-server.max-num-retries", DEFAULT_MAX_NUM_RETRIES); 369 438 retries = Math.max(0,retries); 370 371 439 372 440 while(true) { // the retry loop -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r1608 r1664 29 29 */ 30 30 public abstract class OsmServerReader extends OsmConnection { 31 32 private OsmApi api = newOsmApi();33 31 32 private OsmApi api = OsmApi.getOsmApi(); 33 34 34 /** 35 35 * Open a connection to the given url and return a reader on the input stream … … 40 40 */ 41 41 protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 42 42 43 43 // initialize API. Abort download in case of configuration or network 44 44 // errors … … 48 48 } catch(Exception e) { 49 49 JOptionPane.showMessageDialog( 50 null,51 tr( "Failed to initialize communication with the OSM server {0}.\n"52 + "Check the server URL in your preferences and your internet connection.",53 Main.pref.get("osm-server.url")54 ),55 tr("Error"),56 JOptionPane.ERROR_MESSAGE50 null, 51 tr( "Failed to initialize communication with the OSM server {0}.\n" 52 + "Check the server URL in your preferences and your internet connection.", 53 Main.pref.get("osm-server.url") 54 ), 55 tr("Error"), 56 JOptionPane.ERROR_MESSAGE 57 57 ); 58 58 e.printStackTrace(); … … 66 66 protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException { 67 67 68 // System.out.println("download: "+urlStr);68 // System.out.println("download: "+urlStr); 69 69 URL url = new URL(urlStr); 70 70 activeConnection = (HttpURLConnection)url.openConnection(); … … 74 74 } 75 75 76 if (Main.pref.getBoolean("osm-server.use-compression", true)) 76 if (Main.pref.getBoolean("osm-server.use-compression", true)) { 77 77 activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); 78 } 78 79 79 80 activeConnection.setConnectTimeout(15000); … … 89 90 return null; 90 91 if (activeConnection.getResponseCode() == 500) 91 {92 92 throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time.")); 93 }94 // System.out.println("got return: "+activeConnection.getResponseCode());95 93 96 94 String encoding = activeConnection.getContentEncoding(); -
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r1663 r1664 33 33 public Collection<OsmPrimitive> processed; 34 34 35 private OsmApi api = newOsmApi();35 private OsmApi api = OsmApi.getOsmApi(); 36 36 37 37 private static final int MSECS_PER_SECOND = 1000;
Note:
See TracChangeset
for help on using the changeset viewer.