source: josm/trunk/src/org/openstreetmap/josm/io/DiffResultProcessor.java@ 8340

Last change on this file since 8340 was 8287, checked in by Don-vip, 9 years ago

fix findsecbugs:XXE_SAXPARSER - "Security - XML Parsing Vulnerable to XXE (SAXParser)"

  • Property svn:eol-style set to native
File size: 7.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.io.IOException;
7import java.io.StringReader;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.HashSet;
12import java.util.Map;
13import java.util.Set;
14
15import javax.xml.parsers.ParserConfigurationException;
16
17import org.openstreetmap.josm.data.osm.Changeset;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
21import org.openstreetmap.josm.data.osm.PrimitiveId;
22import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26import org.openstreetmap.josm.tools.Utils;
27import org.openstreetmap.josm.tools.XmlParsingException;
28import org.xml.sax.Attributes;
29import org.xml.sax.InputSource;
30import org.xml.sax.Locator;
31import org.xml.sax.SAXException;
32import org.xml.sax.helpers.DefaultHandler;
33
34public class DiffResultProcessor {
35
36 private static class DiffResultEntry {
37 public long new_id;
38 public int new_version;
39 }
40
41 /**
42 * mapping from old id to new id and version, the result of parsing the diff result
43 * replied by the server
44 */
45 private Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<>();
46 /**
47 * the set of processed primitives *after* the new id, the new version and the new changeset id is set
48 */
49 private Set<OsmPrimitive> processed;
50 /**
51 * the collection of primitives being uploaded
52 */
53 private Collection<? extends OsmPrimitive> primitives;
54
55 /**
56 * Creates a diff result reader
57 *
58 * @param primitives the collection of primitives which have been uploaded. If null,
59 * assumes an empty collection.
60 */
61 public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
62 if (primitives == null) {
63 primitives = Collections.emptyList();
64 }
65 this.primitives = primitives;
66 this.processed = new HashSet<>();
67 }
68
69 /**
70 * Parse the response from a diff upload to the OSM API.
71 *
72 * @param diffUploadResponse the response. Must not be null.
73 * @param progressMonitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
74 * @throws IllegalArgumentException if diffUploadRequest is null
75 * @throws XmlParsingException if the diffUploadRequest can't be parsed successfully
76 *
77 */
78 public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws XmlParsingException {
79 if (progressMonitor == null) {
80 progressMonitor = NullProgressMonitor.INSTANCE;
81 }
82 CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
83 try {
84 progressMonitor.beginTask(tr("Parsing response from server..."));
85 InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
86 Utils.newSafeSAXParser().parse(inputSource, new Parser());
87 } catch(XmlParsingException e) {
88 throw e;
89 } catch(IOException | ParserConfigurationException | SAXException e) {
90 throw new XmlParsingException(e);
91 } finally {
92 progressMonitor.finishTask();
93 }
94 }
95
96 /**
97 * Postprocesses the diff result read and parsed from the server.
98 *
99 * Uploaded objects are assigned their new id (if they got assigned a new
100 * id by the server), their new version (if the version was incremented),
101 * and the id of the changeset to which they were uploaded.
102 *
103 * @param cs the current changeset. Ignored if null.
104 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
105 * @return the collection of processed primitives
106 */
107 protected Set<OsmPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
108 if (monitor == null) {
109 monitor = NullProgressMonitor.INSTANCE;
110 }
111 DataSet ds = null;
112 if (!primitives.isEmpty()) {
113 ds = primitives.iterator().next().getDataSet();
114 }
115 if (ds != null) {
116 ds.beginUpdate();
117 }
118 try {
119 monitor.beginTask("Postprocessing uploaded data ...");
120 monitor.setTicksCount(primitives.size());
121 monitor.setTicks(0);
122 for (OsmPrimitive p : primitives) {
123 monitor.worked(1);
124 DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
125 if (entry == null) {
126 continue;
127 }
128 processed.add(p);
129 if (!p.isDeleted()) {
130 p.setOsmId(entry.new_id, entry.new_version);
131 p.setVisible(true);
132 } else {
133 p.setVisible(false);
134 }
135 if (cs != null && !cs.isNew()) {
136 p.setChangesetId(cs.getId());
137 }
138 }
139 return processed;
140 } finally {
141 if (ds != null) {
142 ds.endUpdate();
143 }
144 monitor.finishTask();
145 }
146 }
147
148 private class Parser extends DefaultHandler {
149 private Locator locator;
150
151 @Override
152 public void setDocumentLocator(Locator locator) {
153 this.locator = locator;
154 }
155
156 protected void throwException(String msg) throws XmlParsingException {
157 throw new XmlParsingException(msg).rememberLocation(locator);
158 }
159
160 @Override
161 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
162 try {
163 switch (qName) {
164 case "diffResult":
165 // the root element, ignore
166 break;
167 case "node":
168 case "way":
169 case "relation":
170 PrimitiveId id = new SimplePrimitiveId(
171 Long.parseLong(atts.getValue("old_id")),
172 OsmPrimitiveType.fromApiTypeName(qName)
173 );
174 DiffResultEntry entry = new DiffResultEntry();
175 if (atts.getValue("new_id") != null) {
176 entry.new_id = Long.parseLong(atts.getValue("new_id"));
177 }
178 if (atts.getValue("new_version") != null) {
179 entry.new_version = Integer.parseInt(atts.getValue("new_version"));
180 }
181 diffResults.put(id, entry);
182 break;
183 default:
184 throwException(tr("Unexpected XML element with name ''{0}''", qName));
185 }
186 } catch (NumberFormatException e) {
187 throw new XmlParsingException(e).rememberLocation(locator);
188 }
189 }
190 }
191}
Note: See TracBrowser for help on using the repository browser.