source: josm/trunk/src/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTask.java@ 12542

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

fix HeadlessException seen in unit tests logs

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GraphicsEnvironment;
8import java.io.IOException;
9import java.util.Collections;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetCache;
18import org.openstreetmap.josm.data.osm.UserInfo;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.JosmUserIdentityManager;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23import org.openstreetmap.josm.io.ChangesetQuery;
24import org.openstreetmap.josm.io.OsmServerChangesetReader;
25import org.openstreetmap.josm.io.OsmServerUserInfoReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.xml.sax.SAXException;
28
29/**
30 * This is a task for downloading the open changesets of the current user
31 * from the OSM server.
32 */
33public class DownloadOpenChangesetsTask extends PleaseWaitRunnable {
34
35 private boolean canceled;
36 private OsmServerChangesetReader reader;
37 private List<Changeset> changesets;
38 private Exception lastException;
39 private final Component parent;
40
41 /**
42 * Constructs the task
43 * @param parent is a component to show error messages
44 */
45 public DownloadOpenChangesetsTask(Component parent) {
46 super(parent, tr("Downloading open changesets ..."), false /* don't ignore exceptions */);
47 this.parent = parent;
48 }
49
50 @Override
51 protected void cancel() {
52 this.canceled = true;
53 synchronized (this) {
54 if (reader != null) {
55 reader.cancel();
56 }
57 }
58 }
59
60 @Override
61 protected void finish() {
62 if (JosmUserIdentityManager.getInstance().isAnonymous()) {
63 String msg = tr("Could not retrieve the list of your open changesets because<br>"
64 + "JOSM does not know your identity.<br>"
65 + "You have either chosen to work anonymously or you are not entitled<br>"
66 + "to know the identity of the user on whose behalf you are working.");
67 Main.warn(msg);
68 if (!GraphicsEnvironment.isHeadless()) {
69 JOptionPane.showMessageDialog(GuiHelper.getFrameForComponent(parent),
70 "<html>" + msg + "</html>", tr("Missing user identity"), JOptionPane.ERROR_MESSAGE);
71 }
72 return;
73 }
74 if (canceled) return;
75 if (lastException != null) {
76 ExceptionDialogUtil.explainException(lastException);
77 return;
78 }
79 if (changesets.isEmpty()) {
80 if (!GraphicsEnvironment.isHeadless()) {
81 JOptionPane.showMessageDialog(
82 Main.parent,
83 tr("There are no open changesets"),
84 tr("No open changesets"),
85 JOptionPane.INFORMATION_MESSAGE
86 );
87 }
88 return;
89 }
90 SwingUtilities.invokeLater(() -> ChangesetCache.getInstance().update(changesets));
91 }
92
93 /**
94 * Refreshes the user info from the server. This is necessary if we don't know the users id yet.
95 */
96 protected void refreshUserIdentity() {
97 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
98 try {
99 OsmServerUserInfoReader infoReader = new OsmServerUserInfoReader();
100 UserInfo info = infoReader.fetchUserInfo(getProgressMonitor().createSubTaskMonitor(1, false));
101 im.setFullyIdentified(info.getDisplayName(), info);
102 } catch (OsmTransferException e) {
103 // retrieving the user info can fail if the current user is not authorised to
104 // retrieve it, i.e. if he is working with an OAuth Access Token which doesn't
105 // have the respective privileges or if he didn't or he can't authenticate with
106 // a username/password-pair.
107 //
108 // Downgrade your knowlege about its identity if we've assumed that he was fully
109 // identified. Otherwise, if he is anonymous or partially identified, keep our level
110 // of knowlege.
111 //
112 if (im.isFullyIdentified()) {
113 im.setPartiallyIdentified(im.getUserName());
114 }
115 Main.warn(e, tr("Failed to retrieve user infos for the current JOSM user. Exception was: {0}", e.toString()));
116 }
117 }
118
119 @Override
120 protected void realRun() throws SAXException, IOException, OsmTransferException {
121 try {
122 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
123 if (im.isAnonymous()) {
124 refreshUserIdentity();
125 } else if (im.isFullyIdentified()) {
126 // do nothing
127 } else if (im.isPartiallyIdentified()) {
128 refreshUserIdentity();
129 }
130 if (canceled) return;
131 synchronized (this) {
132 reader = new OsmServerChangesetReader();
133 }
134 ChangesetQuery query = new ChangesetQuery().beingOpen(true);
135 if (im.isAnonymous())
136 // we still don't know anything about the current user. Can't retrieve
137 // its changesets
138 return;
139 else if (im.isFullyIdentified()) {
140 query = query.forUser(im.getUserId());
141 } else {
142 // we only know the users name, not its id. Nevermind, try to read
143 // its open changesets anyway.
144 //
145 query = query.forUser(im.getUserName());
146 }
147 changesets = reader.queryChangesets(
148 query,
149 getProgressMonitor().createSubTaskMonitor(1, false /* not internal */)
150 );
151 } catch (OsmTransferException e) {
152 if (canceled)
153 return;
154 lastException = e;
155 }
156 }
157
158 /**
159 * Determines if this task has been cancelled.
160 * @return {@code true} if this task has been cancelled
161 */
162 public boolean isCanceled() {
163 return canceled;
164 }
165
166 /**
167 * Returns the changesets.
168 * @return the changesets, or {@code null}
169 * @since 11110
170 */
171 public final List<Changeset> getChangesets() {
172 return changesets != null ? Collections.unmodifiableList(changesets) : null;
173 }
174}
Note: See TracBrowser for help on using the repository browser.