Ignore:
Timestamp:
2012-08-11T17:37:00+02:00 (12 years ago)
Author:
Don-vip
Message:

see #7943 - Introduce OsmApi.DEFAULT_API_URL, better handling of change of API URL in OAuth management, javadoc improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java

    r5266 r5422  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.oauth;
     3
     4import java.net.MalformedURLException;
     5import java.net.URL;
    36
    47import oauth.signpost.OAuthConsumer;
     
    811
    912import org.openstreetmap.josm.data.Preferences;
     13import org.openstreetmap.josm.io.OsmApi;
    1014import org.openstreetmap.josm.tools.CheckParameterUtil;
    1115
    1216/**
    1317 * This class manages a set of OAuth parameters.
    14  *
     18 * @since 2747
    1519 */
    1620public class OAuthParameters {
    1721
     22    /**
     23     * The default JOSM OAuth consumer key.
     24     */
    1825    static public final String DEFAULT_JOSM_CONSUMER_KEY = "AdCRxTpvnbmfV8aPqrTLyA";
     26    /**
     27     * The default JOSM OAuth consumer secret.
     28     */
    1929    static public final String DEFAULT_JOSM_CONSUMER_SECRET = "XmYOiGY9hApytcBC3xCec3e28QBqOWz5g6DSb5UpE";
     30    /**
     31     * The default OSM OAuth request token URL.
     32     */
    2033    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     */
    2137    static public final String DEFAULT_ACCESS_TOKEN_URL = "http://www.openstreetmap.org/oauth/access_token";
     38    /**
     39     * The default OSM OAuth authorize URL.
     40     */
    2241    static public final String DEFAULT_AUTHORISE_URL = "http://www.openstreetmap.org/oauth/authorize";
    2342
     
    2544    /**
    2645     * Replies a set of default parameters for a consumer accessing the standard OSM server
    27      * at http://api.openstreetmap.org/api
     46     * at {@link OsmApi#DEFAULT_API_URL}.
    2847     *
    2948     * @return a set of default parameters
    3049     */
    3150    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) {
    3264        OAuthParameters parameters = new OAuthParameters();
    3365        parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
    3466        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        }
    3883        return parameters;
    3984    }
     
    4893        boolean useDefault = pref.getBoolean("oauth.settings.use-default", true );
    4994        if (useDefault)
    50             return createDefault();
     95            return createDefault(pref.get("osm-server.url"));
    5196        OAuthParameters parameters = new OAuthParameters();
    5297        parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", ""));
     
    78123    private String authoriseUrl;
    79124
     125    /**
     126     * Constructs a new, unitialized, {@code OAuthParameters}.
     127     *
     128     * @see #createDefault
     129     * @see #createFromPreferences
     130     */
    80131    public OAuthParameters() {
    81132    }
     
    96147    }
    97148
     149    /**
     150     * Gets the consumer key.
     151     * @return The consumer key
     152     */
    98153    public String getConsumerKey() {
    99154        return consumerKey;
    100155    }
     156   
     157    /**
     158     * Sets the consumer key.
     159     * @param consumerKey The consumer key
     160     */
    101161    public void setConsumerKey(String consumerKey) {
    102162        this.consumerKey = consumerKey;
    103163    }
     164   
     165    /**
     166     * Gets the consumer secret.
     167     * @return The consumer secret
     168     */
    104169    public String getConsumerSecret() {
    105170        return consumerSecret;
    106171    }
     172   
     173    /**
     174     * Sets the consumer secret.
     175     * @param consumerSecret The consumer secret
     176     */
    107177    public void setConsumerSecret(String consumerSecret) {
    108178        this.consumerSecret = consumerSecret;
    109179    }
     180   
     181    /**
     182     * Gets the request token URL.
     183     * @return The request token URL
     184     */
    110185    public String getRequestTokenUrl() {
    111186        return requestTokenUrl;
    112187    }
     188   
     189    /**
     190     * Sets the request token URL.
     191     * @param requestTokenUrl the request token URL
     192     */
    113193    public void setRequestTokenUrl(String requestTokenUrl) {
    114194        this.requestTokenUrl = requestTokenUrl;
    115195    }
     196   
     197    /**
     198     * Gets the access token URL.
     199     * @return The access token URL
     200     */
    116201    public String getAccessTokenUrl() {
    117202        return accessTokenUrl;
    118203    }
     204   
     205    /**
     206     * Sets the access token URL.
     207     * @param accessTokenUrl The access token URL
     208     */
    119209    public void setAccessTokenUrl(String accessTokenUrl) {
    120210        this.accessTokenUrl = accessTokenUrl;
    121211    }
     212   
     213    /**
     214     * Gets the authorise URL.
     215     * @return The authorise URL
     216     */
    122217    public String getAuthoriseUrl() {
    123218        return authoriseUrl;
    124219    }
     220   
     221    /**
     222     * Sets the authorise URL.
     223     * @param authoriseUrl The authorise URL
     224     */
    125225    public void setAuthoriseUrl(String authoriseUrl) {
    126226        this.authoriseUrl = authoriseUrl;
     
    128228
    129229    /**
    130      * Builds an {@link OAuthConsumer} based on these parameters
     230     * Builds an {@link OAuthConsumer} based on these parameters.
    131231     *
    132232     * @return the consumer
    133233     */
    134234    public OAuthConsumer buildConsumer() {
    135         OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
    136         return consumer;
     235        return new DefaultOAuthConsumer(consumerKey, consumerSecret);
    137236    }
    138237
     
    142241     * @param consumer the consumer. Must not be null.
    143242     * @return the provider
    144      * @throws IllegalArgumentException thrown if consumer is null
     243     * @throws IllegalArgumentException if consumer is null
    145244     */
    146245    public OAuthProvider buildProvider(OAuthConsumer consumer) throws IllegalArgumentException {
     
    153252    }
    154253
     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     */
    155258    public void saveToPreferences(Preferences pref) {
    156         if (this.equals(createDefault())) {
     259        if (this.equals(createDefault(pref.get("osm-server.url")))) {
    157260            pref.put("oauth.settings.use-default", true );
    158261            clearPreferences(pref);
Note: See TracChangeset for help on using the changeset viewer.