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

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

update unitils-core to 3.4.2, increase unit test coverage with EqualsVerifier, make OAuthParameters immutable

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