source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerChangesetReader.java@ 11856

Last change on this file since 11856 was 11193, checked in by Don-vip, 7 years ago

fix #13872 - OAuth signing of all API requests to support user-based bandwith limit instead of IP-based one (based on patch by wiktorn)

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.io.IOException;
8import java.io.InputStream;
9import java.text.MessageFormat;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.List;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetDataSet;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.tools.CheckParameterUtil;
22import org.openstreetmap.josm.tools.XmlParsingException;
23
24/**
25 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
26 *
27 */
28public class OsmServerChangesetReader extends OsmServerReader {
29
30 /**
31 * don't use - not implemented!
32 */
33 @Override
34 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
35 return null;
36 }
37
38 protected final InputStream getChangesetInputStream(long id, boolean includeDiscussion, ProgressMonitor monitor)
39 throws OsmTransferException {
40 StringBuilder sb = new StringBuilder(48).append("changeset/").append(id);
41 if (includeDiscussion) {
42 sb.append("?include_discussion=true");
43 }
44 return getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
45 }
46
47 /**
48 * Queries a list
49 * @param query the query specification. Must not be null.
50 * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
51 * @return the list of changesets read from the server
52 * @throws IllegalArgumentException if query is null
53 * @throws OsmTransferException if something goes wrong
54 */
55 public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
56 CheckParameterUtil.ensureParameterNotNull(query, "query");
57 List<Changeset> result = null;
58 if (monitor == null) {
59 monitor = NullProgressMonitor.INSTANCE;
60 }
61 try {
62 monitor.beginTask(tr("Reading changesets..."));
63 StringBuilder sb = new StringBuilder();
64 sb.append("changesets?").append(query.getQueryString());
65 try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
66 if (in == null)
67 return Collections.emptyList();
68 monitor.indeterminateSubTask(tr("Downloading changesets ..."));
69 result = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
70 } catch (IOException e) {
71 Main.warn(e);
72 }
73 } catch (OsmTransferException e) {
74 throw e;
75 } catch (IllegalDataException e) {
76 throw new OsmTransferException(e);
77 } finally {
78 monitor.finishTask();
79 }
80 return result;
81 }
82
83 /**
84 * Reads the changeset with id <code>id</code> from the server.
85 *
86 * @param id the changeset id. id &gt; 0 required.
87 * @param includeDiscussion determines if discussion comments must be downloaded or not
88 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
89 * @return the changeset read
90 * @throws OsmTransferException if something goes wrong
91 * @throws IllegalArgumentException if id &lt;= 0
92 * @since 7704
93 */
94 public Changeset readChangeset(long id, boolean includeDiscussion, ProgressMonitor monitor) throws OsmTransferException {
95 if (id <= 0)
96 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
97 if (monitor == null) {
98 monitor = NullProgressMonitor.INSTANCE;
99 }
100 Changeset result = null;
101 try {
102 monitor.beginTask(tr("Reading changeset {0} ...", id));
103 try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
104 if (in == null)
105 return null;
106 monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
107 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
108 if (changesets == null || changesets.isEmpty())
109 return null;
110 result = changesets.get(0);
111 } catch (IOException e) {
112 Main.warn(e);
113 }
114 } catch (OsmTransferException e) {
115 throw e;
116 } catch (IllegalDataException e) {
117 throw new OsmTransferException(e);
118 } finally {
119 monitor.finishTask();
120 }
121 return result;
122 }
123
124 /**
125 * Reads the changesets with id <code>ids</code> from the server.
126 *
127 * @param ids the list of ids. Ignored if null. Only load changesets for ids &gt; 0.
128 * @param includeDiscussion determines if discussion comments must be downloaded or not
129 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
130 * @return the changeset read
131 * @throws OsmTransferException if something goes wrong
132 * @throws IllegalArgumentException if id &lt;= 0
133 * @since 7704
134 */
135 public List<Changeset> readChangesets(Collection<Integer> ids, boolean includeDiscussion, ProgressMonitor monitor)
136 throws OsmTransferException {
137 if (ids == null)
138 return Collections.emptyList();
139 if (monitor == null) {
140 monitor = NullProgressMonitor.INSTANCE;
141 }
142 try {
143 monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...", ids.size(), ids.size()));
144 monitor.setTicksCount(ids.size());
145 List<Changeset> ret = new ArrayList<>();
146 int i = 0;
147 for (int id : ids) {
148 if (id <= 0) {
149 continue;
150 }
151 i++;
152 try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
153 if (in == null)
154 return null;
155 monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
156 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
157 if (changesets == null || changesets.isEmpty()) {
158 continue;
159 }
160 ret.addAll(changesets);
161 } catch (IOException e) {
162 Main.warn(e);
163 }
164 monitor.worked(1);
165 }
166 return ret;
167 } catch (OsmTransferException e) {
168 throw e;
169 } catch (IllegalDataException e) {
170 throw new OsmTransferException(e);
171 } finally {
172 monitor.finishTask();
173 }
174 }
175
176 /**
177 * Downloads the content of a changeset
178 *
179 * @param id the changeset id. &gt; 0 required.
180 * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
181 * @return the changeset content
182 * @throws IllegalArgumentException if id &lt;= 0
183 * @throws OsmTransferException if something went wrong
184 */
185 public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws OsmTransferException {
186 if (id <= 0)
187 throw new IllegalArgumentException(
188 MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
189 if (monitor == null) {
190 monitor = NullProgressMonitor.INSTANCE;
191 }
192 ChangesetDataSet result = null;
193 try {
194 monitor.beginTask(tr("Downloading changeset content"));
195 StringBuilder sb = new StringBuilder(32).append("changeset/").append(id).append("/download");
196 try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
197 if (in == null)
198 return null;
199 monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
200 OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
201 result = parser.parse(monitor.createSubTaskMonitor(1, true));
202 } catch (IOException e) {
203 Main.warn(e);
204 }
205 } catch (XmlParsingException e) {
206 throw new OsmTransferException(e);
207 } finally {
208 monitor.finishTask();
209 }
210 return result;
211 }
212}
Note: See TracBrowser for help on using the repository browser.