source: josm/trunk/src/org/openstreetmap/josm/io/OsmConnection.java@ 1670

Last change on this file since 1670 was 1670, checked in by Gubaer, 15 years ago

fixed: bug in OsmApi.getOsmApi()
cleanup: exception handling in interfacing with OSM API
new: new action for updating individual elements with the their current state on the server (including new menu item in the file menu)
new: improved user feedback in case of conflicts
new: handles 410 Gone conflicts when uploading a changeset
new: undoable command for "purging" a primitive from the current dataset (necessary if the primitive is already deleted on the server and the user wants to remove it from its local dataset)
new: undoable command for "undeleting" an already deleted primitive on the server (kind of "cloning")
new: after a full upload, checks whether there are primitives in the local dataset which might be deleted on the server.
new: data structures for history data
new: history download support in io package

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Font;
7import java.awt.GridBagLayout;
8import java.net.Authenticator;
9import java.net.HttpURLConnection;
10import java.net.PasswordAuthentication;
11import java.nio.ByteBuffer;
12import java.nio.CharBuffer;
13import java.nio.charset.Charset;
14import java.nio.charset.CharsetEncoder;
15import java.nio.charset.CharacterCodingException;
16
17import javax.swing.JCheckBox;
18import javax.swing.JLabel;
19import javax.swing.JPanel;
20import javax.swing.JPasswordField;
21import javax.swing.JTextField;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.gui.ExtendedDialog;
25import org.openstreetmap.josm.tools.Base64;
26import org.openstreetmap.josm.tools.GBC;
27
28/**
29 * Base class that handles common things like authentication for the reader and writer
30 * to the osm server.
31 *
32 * @author imi
33 */
34public class OsmConnection {
35
36 protected boolean cancel = false;
37 protected HttpURLConnection activeConnection;
38
39 private static OsmAuth authentication = new OsmAuth();
40 /**
41 * Initialize the http defaults and the authenticator.
42 */
43 static {
44 // TODO: current authentication handling is sub-optimal in that it seems to use the same authenticator for
45 // any kind of request. HTTP requests executed by plugins, e.g. to password-protected WMS servers,
46 // will use the same username/password which is undesirable.
47 try {
48 HttpURLConnection.setFollowRedirects(true);
49 Authenticator.setDefault(authentication);
50 } catch (SecurityException e) {
51 }
52 }
53
54 /**
55 * The authentication class handling the login requests.
56 */
57 private static class OsmAuth extends Authenticator {
58 /**
59 * Set to true, when the autenticator tried the password once.
60 */
61 boolean passwordtried = false;
62 /**
63 * Whether the user cancelled the password dialog
64 */
65 boolean authCancelled = false;
66
67 @Override protected PasswordAuthentication getPasswordAuthentication() {
68 String username = Main.pref.get("osm-server.username");
69 String password = Main.pref.get("osm-server.password");
70 if (passwordtried || username.equals("") || password.equals("")) {
71 JPanel p = new JPanel(new GridBagLayout());
72 if (!username.equals("") && !password.equals("")) {
73 p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
74 }
75 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
76 JTextField usernameField = new JTextField(username, 20);
77 p.add(usernameField, GBC.eol());
78 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
79 JPasswordField passwordField = new JPasswordField(password, 20);
80 p.add(passwordField, GBC.eol());
81 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
82 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
83 p.add(warning, GBC.eop());
84
85 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
86 p.add(savePassword, GBC.eop());
87
88 int choice = new ExtendedDialog(Main.parent,
89 tr("Enter Password"),
90 p,
91 new String[] {tr("Login"), tr("Cancel")},
92 new String[] {"ok.png", "cancel.png"}).getValue();
93
94 if (choice != 1) {
95 authCancelled = true;
96 return null;
97 }
98 username = usernameField.getText();
99 password = String.valueOf(passwordField.getPassword());
100 if (savePassword.isSelected()) {
101 Main.pref.put("osm-server.username", username);
102 Main.pref.put("osm-server.password", password);
103 }
104 if (username.equals(""))
105 return null;
106 }
107 passwordtried = true;
108 return new PasswordAuthentication(username, password.toCharArray());
109 }
110 }
111
112 /**
113 * Must be called before each connection attemp to initialize the authentication.
114 */
115 protected final void initAuthentication() {
116 authentication.authCancelled = false;
117 authentication.passwordtried = false;
118 }
119
120 /**
121 * @return Whether the connection was cancelled.
122 */
123 protected final boolean isAuthCancelled() {
124 return authentication.authCancelled;
125 }
126
127 public void cancel() {
128 Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
129 cancel = true;
130 if (activeConnection != null) {
131 activeConnection.setConnectTimeout(100);
132 activeConnection.setReadTimeout(100);
133 try {
134 Thread.sleep(100);
135 } catch (InterruptedException ex) {}
136 activeConnection.disconnect();
137 }
138 }
139
140 protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
141 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
142 String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
143 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
144 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
145 }
146}
Note: See TracBrowser for help on using the repository browser.