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

Last change on this file since 9197 was 9172, checked in by simon04, 8 years ago

see #12231 - Use HttpClient for OSM API calls

This requires adaptors to the OAuth library: SignpostAdapters

  • Property svn:eol-style set to native
File size: 4.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;
5
6import java.net.Authenticator.RequestorType;
7import java.nio.ByteBuffer;
8import java.nio.CharBuffer;
9import java.nio.charset.CharacterCodingException;
10import java.nio.charset.CharsetEncoder;
11import java.nio.charset.StandardCharsets;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.oauth.OAuthParameters;
15import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
16import org.openstreetmap.josm.io.auth.CredentialsAgentException;
17import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
18import org.openstreetmap.josm.io.auth.CredentialsManager;
19import org.openstreetmap.josm.tools.Base64;
20import org.openstreetmap.josm.tools.HttpClient;
21
22import oauth.signpost.OAuthConsumer;
23import oauth.signpost.exception.OAuthException;
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;
33 protected HttpClient.Response activeConnection;
34 protected OAuthParameters oauthParameters;
35
36 /**
37 * Cancels the connection.
38 */
39 public void cancel() {
40 cancel = true;
41 synchronized (this) {
42 if (activeConnection != null) {
43 activeConnection.disconnect();
44 }
45 }
46 }
47
48 /**
49 * Adds an authentication header for basic authentication
50 *
51 * @param con the connection
52 * @throws OsmTransferException if something went wrong. Check for nested exceptions
53 */
54 protected void addBasicAuthorizationHeader(HttpClient con) throws OsmTransferException {
55 CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
56 CredentialsAgentResponse response;
57 String token;
58 try {
59 synchronized (CredentialsManager.getInstance()) {
60 response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER,
61 con.getURL().getHost(), false /* don't know yet whether the credentials will succeed */);
62 }
63 } catch (CredentialsAgentException e) {
64 throw new OsmTransferException(e);
65 }
66 if (response == null) {
67 token = ":";
68 } else if (response.isCanceled()) {
69 cancel = true;
70 return;
71 } else {
72 String username = response.getUsername() == null ? "" : response.getUsername();
73 String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword());
74 token = username + ':' + password;
75 try {
76 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token));
77 con.setHeader("Authorization", "Basic "+Base64.encode(bytes));
78 } catch (CharacterCodingException e) {
79 throw new OsmTransferException(e);
80 }
81 }
82 }
83
84 /**
85 * Signs the connection with an OAuth authentication header
86 *
87 * @param connection the connection
88 *
89 * @throws OsmTransferException if there is currently no OAuth Access Token configured
90 * @throws OsmTransferException if signing fails
91 */
92 protected void addOAuthAuthorizationHeader(HttpClient connection) throws OsmTransferException {
93 if (oauthParameters == null) {
94 oauthParameters = OAuthParameters.createFromPreferences(Main.pref);
95 }
96 OAuthConsumer consumer = oauthParameters.buildConsumer();
97 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
98 if (!holder.containsAccessToken())
99 throw new MissingOAuthAccessTokenException();
100 consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());
101 try {
102 consumer.sign(connection);
103 } catch (OAuthException e) {
104 throw new OsmTransferException(tr("Failed to sign a HTTP connection with an OAuth Authentication header"), e);
105 }
106 }
107
108 protected void addAuth(HttpClient connection) throws OsmTransferException {
109 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
110 if ("basic".equals(authMethod)) {
111 addBasicAuthorizationHeader(connection);
112 } else if ("oauth".equals(authMethod)) {
113 addOAuthAuthorizationHeader(connection);
114 } else {
115 String msg = tr("Unexpected value for preference ''{0}''. Got ''{1}''.", "osm-server.auth-method", authMethod);
116 Main.warn(msg);
117 throw new OsmTransferException(msg);
118 }
119 }
120
121 /**
122 * Replies true if this connection is canceled
123 *
124 * @return true if this connection is canceled
125 */
126 public boolean isCanceled() {
127 return cancel;
128 }
129}
Note: See TracBrowser for help on using the repository browser.