Changeset 1071 in josm


Ignore:
Timestamp:
Nov 9, 2008 8:30:39 PM (5 years ago)
Author:
framm
Message:

Support for changeset uploads in 0.6. Error handling is not fully done but basics are working. Full changeset upload is now the default if version is 0.6; set osm-server.atomic-upload=false in your configuration file to go back to individual object upload.

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r769 r1071  
    8787               osm.version = version; 
    8888               osm.checkTagged(); 
    89                         osm.checkDirectionTagged(); 
     89               osm.checkDirectionTagged(); 
    9090          } 
    9191     } 
  • trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java

    r655 r1071  
    1 // License: GPL. For details, see LICENSE file. 
     1//License: GPL. For details, see LICENSE file. 
    22package org.openstreetmap.josm.io; 
    33 
     
    1212 
    1313public class OsmServerObjectReader extends OsmServerReader { 
    14      
    15      public final static  String TYPE_WAY = "way"; 
    16      public final static  String TYPE_REL = "relation"; 
    17      public final static  String TYPE_NODE = "node"; 
    18       
    19      /** 
    20       * Method to download single objects from OSM server. ways, relations, nodes 
    21       * @param id Object ID 
    22       * @param type way node relation 
    23       * @param full download with or without child objects 
    24       * @return the data requested 
    25       * @throws SAXException 
    26       * @throws IOException 
    27       */ 
    28      public DataSet parseOsm(long id, String type, boolean full) throws SAXException, IOException { 
    29             try { 
    30                  
    31                 Main.pleaseWaitDlg.progress.setValue(0); 
    32                 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server...")); 
    33                 StringBuffer sb = new StringBuffer(); 
    34                 sb.append(type); 
    35                 sb.append("/"); 
    36                 sb.append(id); 
    37                 if (full) 
    38                 { 
    39                     sb.append("/full"); 
    40                 } 
    41                  
    42                 final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg); 
    43                 if (in == null) 
    44                     return null; 
    45                 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 
    46                 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 
    47 //                String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5"); 
    48 //                Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 
    49 //                DataSource src = new DataSource(bounds, origin); 
    50 //                data.dataSources.add(src); 
    51                 in.close(); 
    52                 activeConnection = null; 
    53                 return data; 
    54             } catch (IOException e) { 
    55                 if (cancel) 
    56                     return null; 
    57                 throw e; 
    58             } catch (SAXException e) { 
    59                 throw e; 
    60             } catch (Exception e) { 
    61                 if (cancel) 
    62                     return null; 
    63                 if (e instanceof RuntimeException) 
    64                     throw (RuntimeException)e; 
    65                 throw new RuntimeException(e); 
    66             } 
     14 
     15    public final static  String TYPE_WAY = "way"; 
     16    public final static  String TYPE_REL = "relation"; 
     17    public final static  String TYPE_NODE = "node"; 
     18 
     19    /** 
     20     * Method to download single objects from OSM server. ways, relations, nodes 
     21     * @param id Object ID 
     22     * @param type way node relation 
     23     * @param full download with or without child objects 
     24     * @return the data requested 
     25     * @throws SAXException 
     26     * @throws IOException 
     27     */ 
     28    public DataSet parseOsm(long id, String type, boolean full) throws SAXException, IOException { 
     29        try { 
     30 
     31            Main.pleaseWaitDlg.progress.setValue(0); 
     32            Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server...")); 
     33            StringBuffer sb = new StringBuffer(); 
     34            sb.append(type); 
     35            sb.append("/"); 
     36            sb.append(id); 
     37            if (full) 
     38                sb.append("/full"); 
     39 
     40            final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg); 
     41            if (in == null) 
     42                return null; 
     43            Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 
     44            final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 
     45//          String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5"); 
     46//          Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 
     47//          DataSource src = new DataSource(bounds, origin); 
     48//          data.dataSources.add(src); 
     49            in.close(); 
     50            activeConnection = null; 
     51            return data; 
     52        } catch (IOException e) { 
     53            if (cancel) 
     54                return null; 
     55            throw e; 
     56        } catch (SAXException e) { 
     57            throw e; 
     58        } catch (Exception e) { 
     59            if (cancel) 
     60                return null; 
     61            if (e instanceof RuntimeException) 
     62                throw (RuntimeException)e; 
     63            throw new RuntimeException(e); 
    6764        } 
     65    } 
    6866 
    6967} 
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r916 r1071  
    3535                String version = Main.pref.get("osm-server.version", "0.5"); 
    3636                urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr; 
    37 //              System.out.println("download: "+urlStr); 
     37                System.out.println("download: "+urlStr); 
    3838                initAuthentication(); 
    3939                URL url = new URL(urlStr); 
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r977 r1071  
    44import static org.openstreetmap.josm.tools.I18n.tr; 
    55 
     6import java.io.BufferedOutputStream; 
    67import java.io.BufferedReader; 
    78import java.io.ByteArrayOutputStream; 
     
    1011import java.io.InputStreamReader; 
    1112import java.io.OutputStream; 
     13import java.io.OutputStreamWriter; 
     14import java.io.PrintWriter; 
     15import java.io.StringWriter; 
     16import java.io.UnsupportedEncodingException; 
    1217import java.net.ConnectException; 
    1318import java.net.HttpURLConnection; 
     
    2530import org.openstreetmap.josm.data.osm.Way; 
    2631import org.openstreetmap.josm.data.osm.Changeset; 
     32import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor; 
    2733import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 
    2834import org.openstreetmap.josm.data.osm.visitor.Visitor; 
     
    7379 
    7480        long uploadStartTime; 
     81     
    7582        public String timeLeft(int progress, int list_size) { 
    7683                long now = System.currentTimeMillis(); 
     
    8895                time_left_str += Integer.toString(seconds_left); 
    8996                return time_left_str; 
    90         }        
     97        } 
     98     
    9199        public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException { 
    92100                processed = new LinkedList<OsmPrimitive>(); 
     
    96104                Main.pleaseWaitDlg.progress.setValue(0); 
    97105                 
     106        // controls whether or not we open and close a changeset. API 0.6 requires changesets. 
    98107                boolean useChangesets = Main.pref.get("osm-server.version", "0.5").equals("0.6"); 
    99  
     108         
     109        // controls whether or not we try and uplaod the whole bunch in one go 
     110        boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload",  
     111            Main.pref.get("osm-server.version", "0.5").equals("0.6")); 
     112         
    100113                String comment = null; 
    101                 while( useChangesets && comment == null) 
    102                 { 
     114                while (useChangesets && comment == null) { 
    103115                        comment = JOptionPane.showInputDialog(Main.parent, tr("Provide a brief comment as to the changes to you are uploading:"), 
    104                                                              tr("Commit comment"), JOptionPane.QUESTION_MESSAGE); 
     116                         tr("Commit comment"), JOptionPane.QUESTION_MESSAGE); 
    105117                        if( comment == null ) 
    106118                                return; 
     
    111123                } 
    112124                try { 
    113                         if( useChangesets && !startChangeset(10, comment) ) 
     125                        if (useChangesets && !startChangeset(10, comment)) 
    114126                                return; 
    115127                } 
    116128                catch (OsmTransferException ex) { 
    117                         dealWithTransferException (ex); 
     129                        dealWithTransferException(ex); 
    118130                        return; 
    119131                } 
    120                  
    121                 NameVisitor v = new NameVisitor(); 
    122                 try { 
    123                         uploadStartTime = System.currentTimeMillis(); 
    124                         for (OsmPrimitive osm : list) { 
    125                                 if (cancel) 
    126                                         return; 
    127                                 osm.visit(v); 
    128                                 int progress = Main.pleaseWaitDlg.progress.getValue(); 
    129                                 String time_left_str = timeLeft(progress, list.size()); 
    130                                 Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...", 
    131                                         tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str)); 
    132                                 osm.visit(this); 
    133                                 Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1); 
    134                                 Main.pleaseWaitDlg.progress.setValue(progress+1); 
    135                         } 
    136                                 if( useChangesets )  
    137                                         stopChangeset(10); 
     132         
     133                try { 
     134            if (useDiffUploads) { 
     135                uploadDiff(10, list); 
     136            } else { 
     137                NameVisitor v = new NameVisitor(); 
     138                uploadStartTime = System.currentTimeMillis(); 
     139                for (OsmPrimitive osm : list) { 
     140                    if (cancel) 
     141                        return; 
     142                    osm.visit(v); 
     143                    int progress = Main.pleaseWaitDlg.progress.getValue(); 
     144                    String time_left_str = timeLeft(progress, list.size()); 
     145                    Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...", 
     146                            tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str)); 
     147                    osm.visit(this); 
     148                    Main.pleaseWaitDlg.progress.setValue(progress+1); 
     149                } 
     150            } 
     151                        if (useChangesets)  
     152                            stopChangeset(10); 
    138153                } catch (RuntimeException e) { 
    139154                        try { 
    140                                 if( useChangesets ) stopChangeset(10); 
     155                                if (useChangesets)  
     156                    stopChangeset(10); 
    141157                        } 
    142158                        catch (OsmTransferException ex) { 
     
    211227                        } 
    212228                        if (retCode != 200 && retCode != 412) { 
     229                 
    213230                                if (retries >= 0) { 
    214231                                        retries--; 
     
    217234                                        System.out.println("retrying ("+retries+" left)"); 
    218235                                        return startChangeset(retries, comment); 
    219                                 } else {  
    220                                         // Look for a detailed error message from the server 
    221                                         if (activeConnection.getHeaderField("Error") != null) 
    222                                                 retMsg += "\n" + activeConnection.getHeaderField("Error"); 
    223  
    224                                         // Report our error 
    225                                         ByteArrayOutputStream o = new ByteArrayOutputStream(); 
    226                                         OsmWriter.output(o, changeset); 
    227                                         System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
    228                                         //throw new RuntimeException(retCode+" "+retMsg); 
    229                                         throw new OsmTransferException (retCode + " " + retMsg); 
    230236                                } 
     237                 
     238                                // Look for a detailed error message from the server 
     239                                if (activeConnection.getHeaderField("Error") != null) 
     240                                    retMsg += "\n" + activeConnection.getHeaderField("Error"); 
     241 
     242                                // Report our error 
     243                                ByteArrayOutputStream o = new ByteArrayOutputStream(); 
     244                                OsmWriter.output(o, changeset); 
     245                                System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
     246                                //throw new RuntimeException(retCode+" "+retMsg); 
     247                                throw new OsmTransferException (retCode + " " + retMsg); 
     248                         
    231249                        } 
    232250                } catch (UnknownHostException e) { 
     
    265283                return true; 
    266284        } 
    267  
     285     
     286    private void uploadDiff(int retries, Collection<OsmPrimitive> list) throws OsmTransferException { 
     287         
     288        CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset); 
     289 
     290        for (OsmPrimitive osm : list) { 
     291            int progress = Main.pleaseWaitDlg.progress.getValue(); 
     292            Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); 
     293            if (cancel) 
     294                return; 
     295            osm.visit(duv); 
     296            Main.pleaseWaitDlg.progress.setValue(progress+1); 
     297        } 
     298        System.out.println("the document:\n"); 
     299        String diff = duv.getDocument(); 
     300        System.out.println(diff); 
     301         
     302        Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); 
     303        try { 
     304            if (cancel) 
     305                return; // assume cancel 
     306            String version = Main.pref.get("osm-server.version", "0.6"); 
     307            URL url = new URL( 
     308                    Main.pref.get("osm-server.url") + 
     309                    "/" + version + 
     310                    "/" + "changeset" +  
     311                    "/" + changeset.id + 
     312                    "/upload" ); 
     313            System.out.print("upload to: "+url+ "..." ); 
     314            activeConnection = (HttpURLConnection)url.openConnection(); 
     315            activeConnection.setConnectTimeout(15000); 
     316            activeConnection.setRequestMethod("POST"); 
     317            addAuth(activeConnection); 
     318             
     319            activeConnection.setDoOutput(true); 
     320            PrintWriter out; 
     321            try { 
     322                out = new PrintWriter(new OutputStreamWriter(activeConnection.getOutputStream(), "UTF-8")); 
     323            } catch (UnsupportedEncodingException e) { 
     324                throw new RuntimeException(e); 
     325            } 
     326            out.print(diff); 
     327            out.close(); 
     328             
     329            activeConnection.connect(); 
     330            System.out.println("connected"); 
     331 
     332            int retCode = activeConnection.getResponseCode(); 
     333            String retMsg = ""; 
     334             
     335            if (retCode == 200) { 
     336                DiffResultReader.parseDiffResult(activeConnection.getInputStream(), list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg); 
     337            } else if (retCode != 200 && retCode != 412) { 
     338                if (retries >= 0) { 
     339                    retries--; 
     340                    System.out.print("backing off for 10 seconds..."); 
     341                    Thread.sleep(10000); 
     342                    System.out.println("retrying ("+retries+" left)"); 
     343                    stopChangeset(retries); 
     344                } else {  
     345                    // Look for a detailed error message from the server 
     346                    if (activeConnection.getHeaderField("Error") != null) 
     347                        retMsg += "\n" + activeConnection.getHeaderField("Error"); 
     348 
     349                    // Report our error 
     350                    ByteArrayOutputStream o = new ByteArrayOutputStream(); 
     351                    OsmWriter.output(o, changeset); 
     352                    System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 
     353                    //throw new RuntimeException(retCode+" "+retMsg); 
     354                    throw new OsmTransferException(retCode+" "+retMsg); 
     355                } 
     356            } 
     357        } catch (UnknownHostException e) { 
     358            //throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 
     359            throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 
     360        } catch(SocketTimeoutException e) { 
     361            System.out.println(" timed out, retries left: " + retries); 
     362            if (cancel) 
     363                return; // assume cancel 
     364            if (retries-- > 0) 
     365                stopChangeset(retries); 
     366            else 
     367                //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     368                throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     369        } catch(ConnectException e) { 
     370            System.out.println(" timed out, retries left: " + retries); 
     371            if (cancel) 
     372                return; // assume cancel 
     373            if (retries-- > 0) 
     374                stopChangeset(retries); 
     375            else 
     376                //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     377                throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     378        } catch (Exception e) { 
     379            if (cancel) 
     380                return; // assume cancel 
     381            if (e instanceof OsmTransferException) 
     382                throw (OsmTransferException)e; 
     383            if (e instanceof RuntimeException) 
     384                throw (RuntimeException)e; 
     385            throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 
     386        } 
     387    } 
     388     
     389     
    268390        private void stopChangeset(int retries) throws OsmTransferException { 
    269391                Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 
     
    443565                        int retCode = activeConnection.getResponseCode(); 
    444566                        /* When creating new, the returned value is the new id, otherwise it is the new version */ 
    445                         if (retCode == 200) 
    446                         { 
    447                                 if(osm.id == 0) 
    448                                 { 
     567                        if (retCode == 200)     { 
     568                                if (osm.id == 0) { 
    449569                                        osm.id = readId(activeConnection.getInputStream()); 
    450570                                        osm.version = 1; 
    451                                 } 
    452                                 else 
    453                                 { 
     571                                } else { 
    454572                                        int read_version = (int)readId(activeConnection.getInputStream()); 
    455                                         if( read_version > 0 ) 
     573                                        if (read_version > 0) 
    456574                                                osm.version = read_version; 
    457575                                } 
    458                         } 
    459                         else 
    460                         { 
     576                        } else { 
    461577                                System.out.println("got return: "+retCode+" with id "+osm.id); 
    462578                        } 
     
    464580                        if (retCode == 410 && requestMethod.equals("DELETE")) 
    465581                                return; // everything fine.. was already deleted. 
    466                         else if (retCode != 200) 
    467                         { 
    468                                 if (retries >= 0 && retCode != 412) 
    469                                 { 
     582                        else if (retCode != 200) { 
     583                                if (retries >= 0 && retCode != 412)     { 
    470584                                        retries--; 
    471585                                        System.out.print("backing off for 10 seconds..."); 
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r1037 r1071  
    3232         * map to allow references to use the correnct primitives. 
    3333         */ 
    34         private HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>(); 
     34        public HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>(); 
    3535 
    3636        private final boolean osmConform; 
     
    131131        } 
    132132 
    133         private OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) { 
     133        public OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) { 
    134134                super(out); 
    135135                this.osmConform = osmConform; 
     
    178178                return osmConform ? 0 : newIdCounter--; 
    179179        } 
    180  
    181180 
    182181        private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) { 
Note: See TracChangeset for help on using the changeset viewer.