Changeset 624 in josm for trunk


Ignore:
Timestamp:
2008-05-10T21:35:30+02:00 (16 years ago)
Author:
framm
Message:
  • preliminary support for API 0.6, patches by Martijn van Oosterhout <kleptog@…>. Should not have an effect on 0.5 operations
Location:
trunk/src/org/openstreetmap/josm
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r396 r624  
    5050        @Override public String toString() {
    5151                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()+"}";
    5353        }
    5454
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r597 r624  
    111111        public boolean incomplete = false;
    112112
     113        /**
     114         * Contains the version number as returned by the API. Needed to
     115         * ensure update consistancy
     116         */
     117        public int version = -1;
     118         
    113119        /**
    114120         * Contains a list of "uninteresting" keys that do not make an object
     
    240246                selected = osm.selected;
    241247                timestamp = osm.timestamp;
     248                version = osm.version;
    242249                tagged = osm.tagged;
    243250                incomplete = osm.incomplete;
     
    256263                        deleted == osm.deleted &&
    257264                        (semanticOnly || (timestamp == null ? osm.timestamp==null : timestamp.equals(osm.timestamp))) &&
     265                        (semanticOnly || (version==osm.version)) &&
    258266                        (semanticOnly || (user == null ? osm.user==null : user==osm.user)) &&
    259267                        (semanticOnly || (visible == osm.visible)) &&
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r582 r624  
    5757
    5858    @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())+"}";
    6060    }
    6161
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r597 r624  
    7979
    8080    @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())+"}";
    8282    }
    8383
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r592 r624  
    1616import java.util.Collection;
    1717import java.util.LinkedList;
     18import javax.swing.JOptionPane;
    1819
    1920import org.openstreetmap.josm.Main;
     
    2223import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2324import org.openstreetmap.josm.data.osm.Way;
     25import org.openstreetmap.josm.data.osm.Changeset;
    2426import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
    2527import org.openstreetmap.josm.data.osm.visitor.Visitor;
    2628import org.xml.sax.SAXException;
     29import org.openstreetmap.josm.io.XmlWriter.OsmWriterInterface;
    2730
    2831/**
     
    5356         */
    5457        private boolean cancel = false;
     58       
     59        /**
     60         * Object describing current changeset
     61         */
     62        private Changeset changeset;
    5563
    5664        /**
     
    8593                Main.pleaseWaitDlg.progress.setMaximum(list.size());
    8694                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               
    88113                NameVisitor v = new NameVisitor();
    89114                try {
     
    101126                                Main.pleaseWaitDlg.progress.setValue(progress+1);
    102127                        }
     128                        if( useChangesets ) stopChangeset(10);
    103129                } catch (RuntimeException e) {
     130                        if( useChangesets ) stopChangeset(10);
    104131                        e.printStackTrace();
    105132                        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);
    106298                }
    107299        }
     
    115307                        sendRequest("PUT", "node", n, true);
    116308                } else if (n.deleted) {
    117                         sendRequest("DELETE", "node", n, false);
     309                        sendRequest("DELETE", "node", n, true);
    118310                } else {
    119311                        sendRequest("PUT", "node", n, true);
     
    130322                        sendRequest("PUT", "way", w, true);
    131323                } else if (w.deleted) {
    132                         sendRequest("DELETE", "way", w, false);
     324                        sendRequest("DELETE", "way", w, true);
    133325                } else {
    134326                        sendRequest("PUT", "way", w, true);
     
    145337                        sendRequest("PUT", "relation", e, true);
    146338                } else if (e.deleted) {
    147                         sendRequest("DELETE", "relation", e, false);
     339                        sendRequest("DELETE", "relation", e, true);
    148340                } else {
    149341                        sendRequest("PUT", "relation", e, true);
     
    174366         * @param urlSuffix The suffix to add at the server url.
    175367         * @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
    178369         */
    179370        private void sendRequestRetry(String requestMethod, String urlSuffix,
    180                         OsmPrimitive osm, boolean addBody, int retries) {
     371                        OsmPrimitive osm, OsmWriterInterface body, int retries) {
    181372                try {
    182373                        if (cancel)
     
    184375                        String version = Main.pref.get("osm-server.version", "0.5");
    185376                        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());
    190382                        System.out.print("upload to: "+url+ "..." );
    191383                        activeConnection = (HttpURLConnection)url.openConnection();
    192384                        activeConnection.setConnectTimeout(15000);
    193385                        activeConnection.setRequestMethod(requestMethod);
    194             addAuth(activeConnection);
    195                         if (addBody) {
     386                        addAuth(activeConnection);
     387                        if (body != null) {
    196388                                activeConnection.setDoOutput(true);
    197389                                OutputStream out = activeConnection.getOutputStream();
    198                                 OsmWriter.output(out, new OsmWriter.Single(osm, true));
     390                                OsmWriter.output(out, body);
    199391                                out.close();
    200             }
     392                        }
    201393                        activeConnection.connect();
    202394                        System.out.println("connected");
    203395
    204396                        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                                }
    207407                        System.out.println("got return: "+retCode+" with id "+osm.id);
    208408                        String retMsg = activeConnection.getResponseMessage();
     
    216416                                        Thread.sleep(10000);
    217417                                        System.out.println("retrying ("+retries+" left)");
    218                                         sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);
     418                                        sendRequestRetry(requestMethod, urlSuffix, osm, body, retries);
    219419                                } else {
    220420                                        // Look for a detailed error message from the server
     
    224424                                        // Report our error
    225425                                        ByteArrayOutputStream o = new ByteArrayOutputStream();
    226                                         OsmWriter.output(o, new OsmWriter.Single(osm, true));
     426                                        OsmWriter.output(o, body);
    227427                                        System.out.println(new String(o.toByteArray(), "UTF-8").toString());
    228428                                        throw new RuntimeException(retCode+" "+retMsg);
     
    236436                                return; // assume cancel
    237437                        if (retries-- > 0)
    238                                 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);
     438                                sendRequestRetry(requestMethod, urlSuffix, osm, body, retries);
    239439                        else
    240440                                throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e);
     
    249449        private void sendRequest(String requestMethod, String urlSuffix,
    250450                        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);
    252456        }
    253457}
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r380 r624  
    1313import org.openstreetmap.josm.data.osm.Node;
    1414import org.openstreetmap.josm.data.osm.OsmPrimitive;
     15import org.openstreetmap.josm.data.osm.Changeset;
    1516import org.openstreetmap.josm.data.osm.Way;
    1617import org.openstreetmap.josm.data.osm.visitor.Visitor;
     
    3435
    3536        private final boolean osmConform;
     37        private final Changeset changeset;
    3638
    3739        public abstract static class Osm implements OsmWriterInterface {
     
    7375
    7476                public void write(PrintWriter out) {
    75                         Visitor writer = new OsmWriter(out, osmConform);
     77                        Visitor writer = new OsmWriter(out, osmConform, null);
    7678                        for (Node n : ds.nodes)
    7779                                if (shouldWrite(n))
     
    109111                private final OsmPrimitive osm;
    110112                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) {
    113116                        this.osm = osm;
    114117                        this.osmConform = osmConform;
     118                        this.changeset = changeset;
    115119                }
    116120
    117121                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) {
    123127                super(out);
    124128                this.osmConform = osmConform;
     129                this.changeset = changeset;
    125130        }
    126131
     
    205210                }
    206211                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+"'" );
    208216        }
    209217}
Note: See TracChangeset for help on using the changeset viewer.