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

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

code style/cleanup - Uncommented Empty Constructor

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