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

Last change on this file since 6313 was 6313, checked in by Don-vip, 11 years ago

cosmetics in error reporting

  • Property svn:eol-style set to native
File size: 5.4 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.net.Authenticator.RequestorType;
7import java.net.HttpURLConnection;
8import java.nio.ByteBuffer;
9import java.nio.CharBuffer;
10import java.nio.charset.CharacterCodingException;
11import java.nio.charset.Charset;
12import java.nio.charset.CharsetEncoder;
13
14import oauth.signpost.OAuthConsumer;
15import oauth.signpost.exception.OAuthException;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.oauth.OAuthParameters;
19import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
20import org.openstreetmap.josm.io.auth.CredentialsAgentException;
21import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
22import org.openstreetmap.josm.io.auth.CredentialsManager;
23import org.openstreetmap.josm.tools.Base64;
24
25/**
26 * Base class that handles common things like authentication for the reader and writer
27 * to the osm server.
28 *
29 * @author imi
30 */
31public class OsmConnection {
32 protected boolean cancel = false;
33 protected HttpURLConnection activeConnection;
34 protected OAuthParameters oauthParameters;
35
36 /**
37 * Initialize the http defaults and the authenticator.
38 */
39 static {
40 try {
41 HttpURLConnection.setFollowRedirects(true);
42 } catch (SecurityException e) {
43 e.printStackTrace();
44 }
45 }
46
47 public void cancel() {
48 cancel = true;
49 synchronized (this) {
50 if (activeConnection != null) {
51 activeConnection.setConnectTimeout(100);
52 activeConnection.setReadTimeout(100);
53 }
54 }
55 try {
56 Thread.sleep(100);
57 } catch (InterruptedException ex) {
58 Main.warn("InterruptedException in "+getClass().getSimpleName()+" during cancel");
59 }
60
61 synchronized (this) {
62 if (activeConnection != null) {
63 activeConnection.disconnect();
64 }
65 }
66 }
67
68 /**
69 * Adds an authentication header for basic authentication
70 *
71 * @param con the connection
72 * @throws OsmTransferException thrown if something went wrong. Check for nested exceptions
73 */
74 protected void addBasicAuthorizationHeader(HttpURLConnection con) throws OsmTransferException {
75 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
76 CredentialsAgentResponse response;
77 String token;
78 try {
79 synchronized (CredentialsManager.getInstance()) {
80 response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER,
81 con.getURL().getHost(), false /* don't know yet whether the credentials will succeed */);
82 }
83 } catch (CredentialsAgentException e) {
84 throw new OsmTransferException(e);
85 }
86 if (response == null) {
87 token = ":";
88 } else if (response.isCanceled()) {
89 cancel = true;
90 return;
91 } else {
92 String username= response.getUsername() == null ? "" : response.getUsername();
93 String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword());
94 token = username + ":" + password;
95 try {
96 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token));
97 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
98 } catch(CharacterCodingException e) {
99 throw new OsmTransferException(e);
100 }
101 }
102 }
103
104 /**
105 * Signs the connection with an OAuth authentication header
106 *
107 * @param connection the connection
108 *
109 * @throws OsmTransferException thrown if there is currently no OAuth Access Token configured
110 * @throws OsmTransferException thrown if signing fails
111 */
112 protected void addOAuthAuthorizationHeader(HttpURLConnection connection) throws OsmTransferException {
113 if (oauthParameters == null) {
114 oauthParameters = OAuthParameters.createFromPreferences(Main.pref);
115 }
116 OAuthConsumer consumer = oauthParameters.buildConsumer();
117 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
118 if (! holder.containsAccessToken())
119 throw new MissingOAuthAccessTokenException();
120 consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());
121 try {
122 consumer.sign(connection);
123 } catch(OAuthException e) {
124 throw new OsmTransferException(tr("Failed to sign a HTTP connection with an OAuth Authentication header"), e);
125 }
126 }
127
128 protected void addAuth(HttpURLConnection connection) throws OsmTransferException {
129 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
130 if (authMethod.equals("basic")) {
131 addBasicAuthorizationHeader(connection);
132 } else if (authMethod.equals("oauth")) {
133 addOAuthAuthorizationHeader(connection);
134 } else {
135 String msg = tr("Unexpected value for preference ''{0}''. Got ''{1}''.", "osm-server.auth-method", authMethod);
136 Main.warn(msg);
137 throw new OsmTransferException(msg);
138 }
139 }
140
141 /**
142 * Replies true if this connection is canceled
143 *
144 * @return true if this connection is canceled
145 */
146 public boolean isCanceled() {
147 return cancel;
148 }
149}
Note: See TracBrowser for help on using the repository browser.