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

Last change on this file since 10446 was 10212, checked in by Don-vip, 8 years ago

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

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