Changeset 5422 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2012-08-11T17:37:00+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
r5266 r5422 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.data.oauth; 3 4 import java.net.MalformedURLException; 5 import java.net.URL; 3 6 4 7 import oauth.signpost.OAuthConsumer; … … 8 11 9 12 import org.openstreetmap.josm.data.Preferences; 13 import org.openstreetmap.josm.io.OsmApi; 10 14 import org.openstreetmap.josm.tools.CheckParameterUtil; 11 15 12 16 /** 13 17 * This class manages a set of OAuth parameters. 14 * 18 * @since 2747 15 19 */ 16 20 public class OAuthParameters { 17 21 22 /** 23 * The default JOSM OAuth consumer key. 24 */ 18 25 static public final String DEFAULT_JOSM_CONSUMER_KEY = "AdCRxTpvnbmfV8aPqrTLyA"; 26 /** 27 * The default JOSM OAuth consumer secret. 28 */ 19 29 static public final String DEFAULT_JOSM_CONSUMER_SECRET = "XmYOiGY9hApytcBC3xCec3e28QBqOWz5g6DSb5UpE"; 30 /** 31 * The default OSM OAuth request token URL. 32 */ 20 33 static public final String DEFAULT_REQUEST_TOKEN_URL = "http://www.openstreetmap.org/oauth/request_token"; 34 /** 35 * The default OSM OAuth access token URL. 36 */ 21 37 static public final String DEFAULT_ACCESS_TOKEN_URL = "http://www.openstreetmap.org/oauth/access_token"; 38 /** 39 * The default OSM OAuth authorize URL. 40 */ 22 41 static public final String DEFAULT_AUTHORISE_URL = "http://www.openstreetmap.org/oauth/authorize"; 23 42 … … 25 44 /** 26 45 * Replies a set of default parameters for a consumer accessing the standard OSM server 27 * at http://api.openstreetmap.org/api46 * at {@link OsmApi#DEFAULT_API_URL}. 28 47 * 29 48 * @return a set of default parameters 30 49 */ 31 50 static public OAuthParameters createDefault() { 51 return createDefault(null); 52 } 53 54 /** 55 * Replies a set of default parameters for a consumer accessing an OSM server 56 * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL} 57 * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>. 58 * 59 * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used. 60 * @return a set of default parameters for the given {@code apiUrl} 61 * @since 5422 62 */ 63 static public OAuthParameters createDefault(String apiUrl) { 32 64 OAuthParameters parameters = new OAuthParameters(); 33 65 parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY); 34 66 parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET); 35 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL); 36 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL); 37 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL); 67 if (apiUrl == null || apiUrl.isEmpty() || apiUrl.equals(OsmApi.DEFAULT_API_URL)) { 68 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL); 69 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL); 70 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL); 71 } else { 72 try { 73 String host = new URL(apiUrl).getHost(); 74 if (host.endsWith("dev.openstreetmap.org")) { 75 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host)); 76 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host)); 77 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host)); 78 } 79 } catch (MalformedURLException e) { 80 // Ignored 81 } 82 } 38 83 return parameters; 39 84 } … … 48 93 boolean useDefault = pref.getBoolean("oauth.settings.use-default", true ); 49 94 if (useDefault) 50 return createDefault( );95 return createDefault(pref.get("osm-server.url")); 51 96 OAuthParameters parameters = new OAuthParameters(); 52 97 parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", "")); … … 78 123 private String authoriseUrl; 79 124 125 /** 126 * Constructs a new, unitialized, {@code OAuthParameters}. 127 * 128 * @see #createDefault 129 * @see #createFromPreferences 130 */ 80 131 public OAuthParameters() { 81 132 } … … 96 147 } 97 148 149 /** 150 * Gets the consumer key. 151 * @return The consumer key 152 */ 98 153 public String getConsumerKey() { 99 154 return consumerKey; 100 155 } 156 157 /** 158 * Sets the consumer key. 159 * @param consumerKey The consumer key 160 */ 101 161 public void setConsumerKey(String consumerKey) { 102 162 this.consumerKey = consumerKey; 103 163 } 164 165 /** 166 * Gets the consumer secret. 167 * @return The consumer secret 168 */ 104 169 public String getConsumerSecret() { 105 170 return consumerSecret; 106 171 } 172 173 /** 174 * Sets the consumer secret. 175 * @param consumerSecret The consumer secret 176 */ 107 177 public void setConsumerSecret(String consumerSecret) { 108 178 this.consumerSecret = consumerSecret; 109 179 } 180 181 /** 182 * Gets the request token URL. 183 * @return The request token URL 184 */ 110 185 public String getRequestTokenUrl() { 111 186 return requestTokenUrl; 112 187 } 188 189 /** 190 * Sets the request token URL. 191 * @param requestTokenUrl the request token URL 192 */ 113 193 public void setRequestTokenUrl(String requestTokenUrl) { 114 194 this.requestTokenUrl = requestTokenUrl; 115 195 } 196 197 /** 198 * Gets the access token URL. 199 * @return The access token URL 200 */ 116 201 public String getAccessTokenUrl() { 117 202 return accessTokenUrl; 118 203 } 204 205 /** 206 * Sets the access token URL. 207 * @param accessTokenUrl The access token URL 208 */ 119 209 public void setAccessTokenUrl(String accessTokenUrl) { 120 210 this.accessTokenUrl = accessTokenUrl; 121 211 } 212 213 /** 214 * Gets the authorise URL. 215 * @return The authorise URL 216 */ 122 217 public String getAuthoriseUrl() { 123 218 return authoriseUrl; 124 219 } 220 221 /** 222 * Sets the authorise URL. 223 * @param authoriseUrl The authorise URL 224 */ 125 225 public void setAuthoriseUrl(String authoriseUrl) { 126 226 this.authoriseUrl = authoriseUrl; … … 128 228 129 229 /** 130 * Builds an {@link OAuthConsumer} based on these parameters 230 * Builds an {@link OAuthConsumer} based on these parameters. 131 231 * 132 232 * @return the consumer 133 233 */ 134 234 public OAuthConsumer buildConsumer() { 135 OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret); 136 return consumer; 235 return new DefaultOAuthConsumer(consumerKey, consumerSecret); 137 236 } 138 237 … … 142 241 * @param consumer the consumer. Must not be null. 143 242 * @return the provider 144 * @throws IllegalArgumentException thrownif consumer is null243 * @throws IllegalArgumentException if consumer is null 145 244 */ 146 245 public OAuthProvider buildProvider(OAuthConsumer consumer) throws IllegalArgumentException { … … 153 252 } 154 253 254 /** 255 * Saves these OAuth parameters to the given {@code Preferences}. 256 * @param pref The Preferences into which are saved these OAuth parameters with the prefix "oauth.settings" 257 */ 155 258 public void saveToPreferences(Preferences pref) { 156 if (this.equals(createDefault( ))) {259 if (this.equals(createDefault(pref.get("osm-server.url")))) { 157 260 pref.put("oauth.settings.use-default", true ); 158 261 clearPreferences(pref);
Note:
See TracChangeset
for help on using the changeset viewer.