Changeset 624 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2008-05-10T21:35:30+02:00 (17 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/Node.java
r396 r624 50 50 @Override public String toString() { 51 51 if (coor == null) return "{Node id="+id+"}"; 52 return "{Node id="+id+", lat="+coor.lat()+",lon="+coor.lon()+"}";52 return "{Node id="+id+",version="+version+",lat="+coor.lat()+",lon="+coor.lon()+"}"; 53 53 } 54 54 -
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r597 r624 111 111 public boolean incomplete = false; 112 112 113 /** 114 * Contains the version number as returned by the API. Needed to 115 * ensure update consistancy 116 */ 117 public int version = -1; 118 113 119 /** 114 120 * Contains a list of "uninteresting" keys that do not make an object … … 240 246 selected = osm.selected; 241 247 timestamp = osm.timestamp; 248 version = osm.version; 242 249 tagged = osm.tagged; 243 250 incomplete = osm.incomplete; … … 256 263 deleted == osm.deleted && 257 264 (semanticOnly || (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp))) && 265 (semanticOnly || (version==osm.version)) && 258 266 (semanticOnly || (user == null ? osm.user==null : user==osm.user)) && 259 267 (semanticOnly || (visible == osm.visible)) && -
trunk/src/org/openstreetmap/josm/data/osm/Relation.java
r582 r624 57 57 58 58 @Override public String toString() { 59 return "{Relation id="+id+" members="+Arrays.toString(members.toArray())+"}";59 return "{Relation id="+id+" version="+version+" members="+Arrays.toString(members.toArray())+"}"; 60 60 } 61 61 -
trunk/src/org/openstreetmap/josm/data/osm/Way.java
r597 r624 79 79 80 80 @Override public String toString() { 81 return "{Way id="+id+" nodes="+Arrays.toString(nodes.toArray())+"}";81 return "{Way id="+id+" version="+version+" nodes="+Arrays.toString(nodes.toArray())+"}"; 82 82 } 83 83 -
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r592 r624 16 16 import java.util.Collection; 17 17 import java.util.LinkedList; 18 import javax.swing.JOptionPane; 18 19 19 20 import org.openstreetmap.josm.Main; … … 22 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 24 import org.openstreetmap.josm.data.osm.Way; 25 import org.openstreetmap.josm.data.osm.Changeset; 24 26 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 25 27 import org.openstreetmap.josm.data.osm.visitor.Visitor; 26 28 import org.xml.sax.SAXException; 29 import org.openstreetmap.josm.io.XmlWriter.OsmWriterInterface; 27 30 28 31 /** … … 53 56 */ 54 57 private boolean cancel = false; 58 59 /** 60 * Object describing current changeset 61 */ 62 private Changeset changeset; 55 63 56 64 /** … … 85 93 Main.pleaseWaitDlg.progress.setMaximum(list.size()); 86 94 Main.pleaseWaitDlg.progress.setValue(0); 87 95 96 boolean useChangesets = Main.pref.get("osm-server.version", "0.5").equals("0.6"); 97 98 String comment = null; 99 while( useChangesets && comment == null) 100 { 101 comment = JOptionPane.showInputDialog(Main.parent, tr("Provide a brief comment as to the changes to you are uploading:"), 102 tr("Commit comment"), JOptionPane.QUESTION_MESSAGE); 103 if( comment == null ) 104 return; 105 /* Don't let people just hit enter */ 106 if( comment.trim().length() >= 3 ) 107 break; 108 comment = null; 109 } 110 if( useChangesets && !startChangeset(10, comment) ) 111 return; 112 88 113 NameVisitor v = new NameVisitor(); 89 114 try { … … 101 126 Main.pleaseWaitDlg.progress.setValue(progress+1); 102 127 } 128 if( useChangesets ) stopChangeset(10); 103 129 } catch (RuntimeException e) { 130 if( useChangesets ) stopChangeset(10); 104 131 e.printStackTrace(); 105 132 throw new SAXException("An error occoured: "+e.getMessage()); 133 } 134 } 135 136 /* FIXME: This code is terrible, please fix it!!!! */ 137 138 /* Ok, this needs some explanation: The problem is that the code for 139 * retrying requests is intertwined with the code that generates the 140 * actual request. This means that for the retry code for the 141 * changeset stuff, it's basically a copy/cut/change slightly 142 * process. What actually needs to happen is that the retrying needs 143 * to be split from the creation of the requests and the retry loop 144 * handled in one place (preferably without recursion). While at you 145 * can fix the issue where hitting cancel doesn't do anything while 146 * retrying. - Mv0 Apr 2008 147 */ 148 private boolean startChangeset(int retries, String comment) { 149 Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset...")); 150 changeset = new Changeset(); 151 changeset.put( "created_by", "josm" ); 152 changeset.put( "comment", comment ); 153 try { 154 if (cancel) 155 return false; // assume cancel 156 String version = Main.pref.get("osm-server.version", "0.6"); 157 URL url = new URL( 158 Main.pref.get("osm-server.url") + 159 "/" + version + 160 "/" + "changeset" + 161 "/" + "create"); 162 System.out.print("upload to: "+url+ "..." ); 163 activeConnection = (HttpURLConnection)url.openConnection(); 164 activeConnection.setConnectTimeout(15000); 165 activeConnection.setRequestMethod("PUT"); 166 addAuth(activeConnection); 167 168 activeConnection.setDoOutput(true); 169 OutputStream out = activeConnection.getOutputStream(); 170 OsmWriter.output(out, changeset); 171 out.close(); 172 173 activeConnection.connect(); 174 System.out.println("connected"); 175 176 int retCode = activeConnection.getResponseCode(); 177 if (retCode == 200) 178 changeset.id = readId(activeConnection.getInputStream()); 179 System.out.println("got return: "+retCode+" with id "+changeset.id); 180 String retMsg = activeConnection.getResponseMessage(); 181 activeConnection.disconnect(); 182 if (retCode == 404) 183 { 184 System.out.println("Server does not support changesets, continuing"); 185 return true; 186 } 187 if (retCode != 200 && retCode != 412) { 188 if (retries >= 0) { 189 retries--; 190 System.out.print("backing off for 10 seconds..."); 191 Thread.sleep(10000); 192 System.out.println("retrying ("+retries+" left)"); 193 return startChangeset(retries, comment); 194 } else { 195 // Look for a detailed error message from the server 196 if (activeConnection.getHeaderField("Error") != null) 197 retMsg += "\n" + activeConnection.getHeaderField("Error"); 198 199 // Report our error 200 ByteArrayOutputStream o = new ByteArrayOutputStream(); 201 OsmWriter.output(o, changeset); 202 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 203 throw new RuntimeException(retCode+" "+retMsg); 204 } 205 } 206 } catch (UnknownHostException e) { 207 throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 208 } catch(SocketTimeoutException e) { 209 System.out.println(" timed out, retries left: " + retries); 210 if (cancel) 211 return false; // assume cancel 212 if (retries-- > 0) 213 startChangeset(retries, comment); 214 else 215 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 216 } catch (Exception e) { 217 if (cancel) 218 return false; // assume cancel 219 if (e instanceof RuntimeException) 220 throw (RuntimeException)e; 221 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 222 } 223 return true; 224 } 225 226 private void stopChangeset(int retries) { 227 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 228 try { 229 if (cancel) 230 return; // assume cancel 231 String version = Main.pref.get("osm-server.version", "0.6"); 232 URL url = new URL( 233 Main.pref.get("osm-server.url") + 234 "/" + version + 235 "/" + "changeset" + 236 "/" + changeset.id + 237 "/close" ); 238 System.out.print("upload to: "+url+ "..." ); 239 activeConnection = (HttpURLConnection)url.openConnection(); 240 activeConnection.setConnectTimeout(15000); 241 activeConnection.setRequestMethod("PUT"); 242 addAuth(activeConnection); 243 244 activeConnection.setDoOutput(true); 245 OutputStream out = activeConnection.getOutputStream(); 246 OsmWriter.output(out, changeset); 247 out.close(); 248 249 activeConnection.connect(); 250 System.out.println("connected"); 251 252 int retCode = activeConnection.getResponseCode(); 253 if (retCode == 200) 254 changeset.id = readId(activeConnection.getInputStream()); 255 System.out.println("got return: "+retCode+" with id "+changeset.id); 256 String retMsg = activeConnection.getResponseMessage(); 257 activeConnection.disconnect(); 258 if (retCode == 404) 259 { 260 System.out.println("Server does not support changesets, continuing"); 261 return; 262 } 263 if (retCode != 200 && retCode != 412) { 264 if (retries >= 0) { 265 retries--; 266 System.out.print("backing off for 10 seconds..."); 267 Thread.sleep(10000); 268 System.out.println("retrying ("+retries+" left)"); 269 stopChangeset(retries); 270 } else { 271 // Look for a detailed error message from the server 272 if (activeConnection.getHeaderField("Error") != null) 273 retMsg += "\n" + activeConnection.getHeaderField("Error"); 274 275 // Report our error 276 ByteArrayOutputStream o = new ByteArrayOutputStream(); 277 OsmWriter.output(o, changeset); 278 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 279 throw new RuntimeException(retCode+" "+retMsg); 280 } 281 } 282 } catch (UnknownHostException e) { 283 throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 284 } catch(SocketTimeoutException e) { 285 System.out.println(" timed out, retries left: " + retries); 286 if (cancel) 287 return; // assume cancel 288 if (retries-- > 0) 289 stopChangeset(retries); 290 else 291 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 292 } catch (Exception e) { 293 if (cancel) 294 return; // assume cancel 295 if (e instanceof RuntimeException) 296 throw (RuntimeException)e; 297 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 106 298 } 107 299 } … … 115 307 sendRequest("PUT", "node", n, true); 116 308 } else if (n.deleted) { 117 sendRequest("DELETE", "node", n, false);309 sendRequest("DELETE", "node", n, true); 118 310 } else { 119 311 sendRequest("PUT", "node", n, true); … … 130 322 sendRequest("PUT", "way", w, true); 131 323 } else if (w.deleted) { 132 sendRequest("DELETE", "way", w, false);324 sendRequest("DELETE", "way", w, true); 133 325 } else { 134 326 sendRequest("PUT", "way", w, true); … … 145 337 sendRequest("PUT", "relation", e, true); 146 338 } else if (e.deleted) { 147 sendRequest("DELETE", "relation", e, false);339 sendRequest("DELETE", "relation", e, true); 148 340 } else { 149 341 sendRequest("PUT", "relation", e, true); … … 174 366 * @param urlSuffix The suffix to add at the server url. 175 367 * @param osm The primitive to encode to the server. 176 * @param addBody <code>true</code>, if the whole primitive body should be added. 177 * <code>false</code>, if only the id is encoded. 368 * @param body the body to be sent 178 369 */ 179 370 private void sendRequestRetry(String requestMethod, String urlSuffix, 180 OsmPrimitive osm, boolean addBody, int retries) {371 OsmPrimitive osm, OsmWriterInterface body, int retries) { 181 372 try { 182 373 if (cancel) … … 184 375 String version = Main.pref.get("osm-server.version", "0.5"); 185 376 URL url = new URL( 186 Main.pref.get("osm-server.url") + 187 "/" + version + 188 "/" + urlSuffix + 189 "/" + (osm.id==0 ? "create" : osm.id)); 377 new URL(Main.pref.get("osm-server.url") + 378 "/" + version + "/"), 379 urlSuffix + 380 "/" + (osm.id==0 ? "create" : osm.id), 381 (java.net.URLStreamHandler)new MyHttpHandler()); 190 382 System.out.print("upload to: "+url+ "..." ); 191 383 activeConnection = (HttpURLConnection)url.openConnection(); 192 384 activeConnection.setConnectTimeout(15000); 193 385 activeConnection.setRequestMethod(requestMethod); 194 195 if ( addBody) {386 addAuth(activeConnection); 387 if (body != null) { 196 388 activeConnection.setDoOutput(true); 197 389 OutputStream out = activeConnection.getOutputStream(); 198 OsmWriter.output(out, new OsmWriter.Single(osm, true));390 OsmWriter.output(out, body); 199 391 out.close(); 200 392 } 201 393 activeConnection.connect(); 202 394 System.out.println("connected"); 203 395 204 396 int retCode = activeConnection.getResponseCode(); 205 if (retCode == 200 && osm.id == 0) 206 osm.id = readId(activeConnection.getInputStream()); 397 /* When creating new, the returned value is the new id, otherwise it is the new version */ 398 if (retCode == 200) 399 if(osm.id == 0) 400 osm.id = readId(activeConnection.getInputStream()); 401 else 402 { 403 int read_version = (int)readId(activeConnection.getInputStream()); 404 if( read_version > 0 ) 405 osm.version = read_version; 406 } 207 407 System.out.println("got return: "+retCode+" with id "+osm.id); 208 408 String retMsg = activeConnection.getResponseMessage(); … … 216 416 Thread.sleep(10000); 217 417 System.out.println("retrying ("+retries+" left)"); 218 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);418 sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 219 419 } else { 220 420 // Look for a detailed error message from the server … … 224 424 // Report our error 225 425 ByteArrayOutputStream o = new ByteArrayOutputStream(); 226 OsmWriter.output(o, new OsmWriter.Single(osm, true));426 OsmWriter.output(o, body); 227 427 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 228 428 throw new RuntimeException(retCode+" "+retMsg); … … 236 436 return; // assume cancel 237 437 if (retries-- > 0) 238 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);438 sendRequestRetry(requestMethod, urlSuffix, osm, body, retries); 239 439 else 240 440 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); … … 249 449 private void sendRequest(String requestMethod, String urlSuffix, 250 450 OsmPrimitive osm, boolean addBody) { 251 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, 10); 451 XmlWriter.OsmWriterInterface body = null; 452 if (addBody) { 453 body = new OsmWriter.Single(osm, true, changeset); 454 } 455 sendRequestRetry(requestMethod, urlSuffix, osm, body, 10); 252 456 } 253 457 } -
trunk/src/org/openstreetmap/josm/io/OsmWriter.java
r380 r624 13 13 import org.openstreetmap.josm.data.osm.Node; 14 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 import org.openstreetmap.josm.data.osm.Changeset; 15 16 import org.openstreetmap.josm.data.osm.Way; 16 17 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 34 35 35 36 private final boolean osmConform; 37 private final Changeset changeset; 36 38 37 39 public abstract static class Osm implements OsmWriterInterface { … … 73 75 74 76 public void write(PrintWriter out) { 75 Visitor writer = new OsmWriter(out, osmConform );77 Visitor writer = new OsmWriter(out, osmConform, null); 76 78 for (Node n : ds.nodes) 77 79 if (shouldWrite(n)) … … 109 111 private final OsmPrimitive osm; 110 112 private final boolean osmConform; 111 112 public Single(OsmPrimitive osm, boolean osmConform) { 113 private final Changeset changeset; 114 115 public Single(OsmPrimitive osm, boolean osmConform, Changeset changeset) { 113 116 this.osm = osm; 114 117 this.osmConform = osmConform; 118 this.changeset = changeset; 115 119 } 116 120 117 121 public void write(PrintWriter out) { 118 osm.visit(new OsmWriter(out, osmConform ));119 } 120 } 121 122 private OsmWriter(PrintWriter out, boolean osmConform ) {122 osm.visit(new OsmWriter(out, osmConform, changeset)); 123 } 124 } 125 126 private OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) { 123 127 super(out); 124 128 this.osmConform = osmConform; 129 this.changeset = changeset; 125 130 } 126 131 … … 205 210 } 206 211 out.print(" visible='"+osm.visible+"'"); 207 212 if( osm.version != -1 ) 213 out.print( " old_version='"+osm.version+"'"); 214 if( this.changeset != null && this.changeset.id != 0) 215 out.print( " changeset='"+this.changeset.id+"'" ); 208 216 } 209 217 }
Note:
See TracChangeset
for help on using the changeset viewer.