Ignore:
Timestamp:
2017-10-06T15:17:51+02:00 (2 years ago)
Author:
bastiK
Message:

see #15229 - do not copy the entire preferences list, just to set a custom server API in OAuth wizard

Location:
trunk/src/org/openstreetmap/josm/data/oauth
Files:
2 edited

Legend:

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

    r12841 r12928  
    77import org.openstreetmap.josm.io.auth.CredentialsAgent;
    88import org.openstreetmap.josm.io.auth.CredentialsAgentException;
     9import org.openstreetmap.josm.spi.preferences.Config;
    910import org.openstreetmap.josm.tools.CheckParameterUtil;
    1011import org.openstreetmap.josm.tools.Logging;
     
    143144     * credential manager.
    144145     *
     146     * @param cm the credential manager. Must not be null.
     147     * @throws IllegalArgumentException if cm is null
     148     */
     149    public void init(CredentialsAgent cm) {
     150        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
     151        OAuthToken token = null;
     152        try {
     153            token = cm.lookupOAuthAccessToken();
     154        } catch (CredentialsAgentException e) {
     155            Logging.error(e);
     156            Logging.warn(tr("Failed to retrieve OAuth Access Token from credential manager"));
     157            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
     158        }
     159        saveToPreferences = Config.getPref().getBoolean("oauth.access-token.save-to-preferences", true);
     160        if (token != null) {
     161            accessTokenKey = token.getKey();
     162            accessTokenSecret = token.getSecret();
     163        }
     164    }
     165
     166    /**
     167     * Initializes the content of this holder from the Access Token managed by the
     168     * credential manager.
     169     *
    145170     * @param pref the preferences. Must not be null.
    146171     * @param cm the credential manager. Must not be null.
    147172     * @throws IllegalArgumentException if cm is null
    148      */
     173     * @deprecated (since 12928) replaced by {@link #init(org.openstreetmap.josm.io.auth.CredentialsAgent)}
     174     */
     175    @Deprecated
    149176    public void init(Preferences pref, CredentialsAgent cm) {
    150177        CheckParameterUtil.ensureParameterNotNull(pref, "pref");
     
    169196     * by a credential manager.
    170197     *
    171      * @param preferences the preferences. Must not be null.
    172198     * @param cm the credentials manager. Must not be null.
    173      * @throws IllegalArgumentException if preferences is null
    174      * @throws IllegalArgumentException if cm is null
    175      */
    176     public void save(Preferences preferences, CredentialsAgent cm) {
    177         CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
    178         CheckParameterUtil.ensureParameterNotNull(cm, "cm");
    179         preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
     199     * @throws IllegalArgumentException if cm is null
     200     */
     201    public void save(CredentialsAgent cm) {
     202        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
     203        Config.getPref().putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
    180204        try {
    181205            if (!saveToPreferences) {
     
    192216
    193217    /**
     218     * Saves the content of this holder to the preferences and a credential store managed
     219     * by a credential manager.
     220     *
     221     * @param preferences the preferences. Must not be null.
     222     * @param cm the credentials manager. Must not be null.
     223     * @throws IllegalArgumentException if preferences is null
     224     * @throws IllegalArgumentException if cm is null
     225     * @deprecated (since 12928) replaced by {@link #save(org.openstreetmap.josm.io.auth.CredentialsAgent)}
     226     */
     227    @Deprecated
     228    public void save(Preferences preferences, CredentialsAgent cm) {
     229        CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
     230        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
     231        preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
     232        try {
     233            if (!saveToPreferences) {
     234                cm.storeOAuthAccessToken(null);
     235            } else {
     236                cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
     237            }
     238        } catch (CredentialsAgentException e) {
     239            Logging.error(e);
     240            Logging.warn(tr("Failed to store OAuth Access Token to credentials manager"));
     241            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
     242        }
     243    }
     244
     245    /**
    194246     * Clears the content of this holder
    195247     */
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java

    r10294 r12928  
    77import org.openstreetmap.josm.data.Preferences;
    88import org.openstreetmap.josm.io.OsmApi;
     9import org.openstreetmap.josm.spi.preferences.Config;
    910import org.openstreetmap.josm.tools.CheckParameterUtil;
    1011import org.openstreetmap.josm.tools.Utils;
     
    7980     * Replies a set of parameters as defined in the preferences.
    8081     *
     82     * @param apiUrl the API URL. Must not be null.
     83     * @return the parameters
     84     */
     85    public static OAuthParameters createFromApiUrl(String apiUrl) {
     86        OAuthParameters parameters = createDefault(apiUrl);
     87        return new OAuthParameters(
     88                Config.getPref().get("oauth.settings.consumer-key", parameters.getConsumerKey()),
     89                Config.getPref().get("oauth.settings.consumer-secret", parameters.getConsumerSecret()),
     90                Config.getPref().get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
     91                Config.getPref().get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
     92                Config.getPref().get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()),
     93                Config.getPref().get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()),
     94                Config.getPref().get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl()));
     95    }
     96
     97    /**
     98     * Replies a set of parameters as defined in the preferences.
     99     *
    81100     * @param pref the preferences
    82101     * @return the parameters
    83      */
     102     * @deprecated (since 12928) replaced by {@link #createFromApiUrl(java.lang.String)}
     103     */
     104    @Deprecated
    84105    public static OAuthParameters createFromPreferences(Preferences pref) {
    85106        OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
     
    95116
    96117    /**
     118     * Remembers the current values in the preferences.
     119     */
     120    public void rememberPreferences() {
     121        Config.getPref().put("oauth.settings.consumer-key", getConsumerKey());
     122        Config.getPref().put("oauth.settings.consumer-secret", getConsumerSecret());
     123        Config.getPref().put("oauth.settings.request-token-url", getRequestTokenUrl());
     124        Config.getPref().put("oauth.settings.access-token-url", getAccessTokenUrl());
     125        Config.getPref().put("oauth.settings.authorise-url", getAuthoriseUrl());
     126        Config.getPref().put("oauth.settings.osm-login-url", getOsmLoginUrl());
     127        Config.getPref().put("oauth.settings.osm-logout-url", getOsmLogoutUrl());
     128    }
     129
     130    /**
    97131     * Remembers the current values in the preferences <code>pref</code>.
    98132     *
    99133     * @param pref the preferences. Must not be null.
    100134     * @throws IllegalArgumentException if pref is null.
    101      */
     135     * @deprecated (since 12928) replaced by {@link #rememberPreferences()}
     136     */
     137    @Deprecated
    102138    public void rememberPreferences(Preferences pref) {
    103139        CheckParameterUtil.ensureParameterNotNull(pref, "pref");
     
    129165     * @param osmLogoutUrl the OSM logout URL (for automatic mode)
    130166     * @see #createDefault
    131      * @see #createFromPreferences
     167     * @see #createFromApiUrl
    132168     * @since 9220
    133169     */
Note: See TracChangeset for help on using the changeset viewer.