source: josm/trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java@ 6243

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

FindBugs - fix potential NPE

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.oauth;
3
4import java.net.MalformedURLException;
5import java.net.URL;
6
7import oauth.signpost.OAuthConsumer;
8import oauth.signpost.OAuthProvider;
9import oauth.signpost.basic.DefaultOAuthConsumer;
10import oauth.signpost.basic.DefaultOAuthProvider;
11
12import org.openstreetmap.josm.data.Preferences;
13import org.openstreetmap.josm.io.OsmApi;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16/**
17 * This class manages a set of OAuth parameters.
18 * @since 2747
19 */
20public class OAuthParameters {
21
22 /**
23 * The default JOSM OAuth consumer key (created by user josmeditor).
24 */
25 static public final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
26 /**
27 * The default JOSM OAuth consumer secret (created by user josmeditor).
28 */
29 static public final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
30 /**
31 * The default OSM OAuth request token URL.
32 */
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 */
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 */
41 static public final String DEFAULT_AUTHORISE_URL = "http://www.openstreetmap.org/oauth/authorize";
42
43
44 /**
45 * Replies a set of default parameters for a consumer accessing the standard OSM server
46 * at {@link OsmApi#DEFAULT_API_URL}.
47 *
48 * @return a set of default parameters
49 */
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) {
64 OAuthParameters parameters = new OAuthParameters();
65 parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
66 parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
67 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
68 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
69 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
70 if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
71 try {
72 String host = new URL(apiUrl).getHost();
73 if (host.endsWith("dev.openstreetmap.org")) {
74 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
75 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
76 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
77 }
78 } catch (MalformedURLException e) {
79 // Ignored
80 }
81 }
82 return parameters;
83 }
84
85 /**
86 * Replies a set of parameters as defined in the preferences.
87 *
88 * @param pref the preferences
89 * @return the parameters
90 */
91 static public OAuthParameters createFromPreferences(Preferences pref) {
92 OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
93 parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
94 parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
95 parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
96 parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
97 parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
98 return parameters;
99 }
100
101 private String consumerKey;
102 private String consumerSecret;
103 private String requestTokenUrl;
104 private String accessTokenUrl;
105 private String authoriseUrl;
106
107 /**
108 * Constructs a new, unitialized, {@code OAuthParameters}.
109 *
110 * @see #createDefault
111 * @see #createFromPreferences
112 */
113 public OAuthParameters() {
114 }
115
116 /**
117 * Creates a clone of the parameters in <code>other</code>.
118 *
119 * @param other the other parameters. Must not be null.
120 * @throws IllegalArgumentException thrown if other is null
121 */
122 public OAuthParameters(OAuthParameters other) throws IllegalArgumentException{
123 CheckParameterUtil.ensureParameterNotNull(other, "other");
124 this.consumerKey = other.consumerKey;
125 this.consumerSecret = other.consumerSecret;
126 this.accessTokenUrl = other.accessTokenUrl;
127 this.requestTokenUrl = other.requestTokenUrl;
128 this.authoriseUrl = other.authoriseUrl;
129 }
130
131 /**
132 * Gets the consumer key.
133 * @return The consumer key
134 */
135 public String getConsumerKey() {
136 return consumerKey;
137 }
138
139 /**
140 * Sets the consumer key.
141 * @param consumerKey The consumer key
142 */
143 public void setConsumerKey(String consumerKey) {
144 this.consumerKey = consumerKey;
145 }
146
147 /**
148 * Gets the consumer secret.
149 * @return The consumer secret
150 */
151 public String getConsumerSecret() {
152 return consumerSecret;
153 }
154
155 /**
156 * Sets the consumer secret.
157 * @param consumerSecret The consumer secret
158 */
159 public void setConsumerSecret(String consumerSecret) {
160 this.consumerSecret = consumerSecret;
161 }
162
163 /**
164 * Gets the request token URL.
165 * @return The request token URL
166 */
167 public String getRequestTokenUrl() {
168 return requestTokenUrl;
169 }
170
171 /**
172 * Sets the request token URL.
173 * @param requestTokenUrl the request token URL
174 */
175 public void setRequestTokenUrl(String requestTokenUrl) {
176 this.requestTokenUrl = requestTokenUrl;
177 }
178
179 /**
180 * Gets the access token URL.
181 * @return The access token URL
182 */
183 public String getAccessTokenUrl() {
184 return accessTokenUrl;
185 }
186
187 /**
188 * Sets the access token URL.
189 * @param accessTokenUrl The access token URL
190 */
191 public void setAccessTokenUrl(String accessTokenUrl) {
192 this.accessTokenUrl = accessTokenUrl;
193 }
194
195 /**
196 * Gets the authorise URL.
197 * @return The authorise URL
198 */
199 public String getAuthoriseUrl() {
200 return authoriseUrl;
201 }
202
203 /**
204 * Sets the authorise URL.
205 * @param authoriseUrl The authorise URL
206 */
207 public void setAuthoriseUrl(String authoriseUrl) {
208 this.authoriseUrl = authoriseUrl;
209 }
210
211 /**
212 * Builds an {@link OAuthConsumer} based on these parameters.
213 *
214 * @return the consumer
215 */
216 public OAuthConsumer buildConsumer() {
217 return new DefaultOAuthConsumer(consumerKey, consumerSecret);
218 }
219
220 /**
221 * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
222 *
223 * @param consumer the consumer. Must not be null.
224 * @return the provider
225 * @throws IllegalArgumentException if consumer is null
226 */
227 public OAuthProvider buildProvider(OAuthConsumer consumer) throws IllegalArgumentException {
228 CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
229 return new DefaultOAuthProvider(
230 requestTokenUrl,
231 accessTokenUrl,
232 authoriseUrl
233 );
234 }
235
236 /**
237 * Saves these OAuth parameters to the given {@code Preferences}.
238 * @param pref The Preferences into which are saved these OAuth parameters with the prefix "oauth.settings"
239 */
240 public void saveToPreferences(Preferences pref) {
241 pref.put("oauth.settings.consumer-key", consumerKey);
242 pref.put("oauth.settings.consumer-secret", consumerSecret);
243 pref.put("oauth.settings.request-token-url", requestTokenUrl);
244 pref.put("oauth.settings.access-token-url", accessTokenUrl);
245 pref.put("oauth.settings.authorise-url", authoriseUrl);
246 }
247
248 @Override
249 public int hashCode() {
250 final int prime = 31;
251 int result = 1;
252 result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
253 result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
254 result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
255 result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
256 result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
257 return result;
258 }
259
260 @Override
261 public boolean equals(Object obj) {
262 if (this == obj)
263 return true;
264 if (obj == null)
265 return false;
266 if (getClass() != obj.getClass())
267 return false;
268 OAuthParameters other = (OAuthParameters) obj;
269 if (accessTokenUrl == null) {
270 if (other.accessTokenUrl != null)
271 return false;
272 } else if (!accessTokenUrl.equals(other.accessTokenUrl))
273 return false;
274 if (authoriseUrl == null) {
275 if (other.authoriseUrl != null)
276 return false;
277 } else if (!authoriseUrl.equals(other.authoriseUrl))
278 return false;
279 if (consumerKey == null) {
280 if (other.consumerKey != null)
281 return false;
282 } else if (!consumerKey.equals(other.consumerKey))
283 return false;
284 if (consumerSecret == null) {
285 if (other.consumerSecret != null)
286 return false;
287 } else if (!consumerSecret.equals(other.consumerSecret))
288 return false;
289 if (requestTokenUrl == null) {
290 if (other.requestTokenUrl != null)
291 return false;
292 } else if (!requestTokenUrl.equals(other.requestTokenUrl))
293 return false;
294 return true;
295 }
296}
Note: See TracBrowser for help on using the repository browser.