Skip to content

Commit

Permalink
Handle signature differences between the 3.1M2 and 3.1RC1 versions of…
Browse files Browse the repository at this point in the history
… RestTemplate.setInterceptors() to work with either. (SOCIAL-247)
  • Loading branch information
habuma committed Sep 7, 2011
1 parent 76bda22 commit 71c0207
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Expand Up @@ -52,9 +52,7 @@ public static RestTemplate create(OAuth1Credentials credentials) {
RestTemplate client = new RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
if (interceptorsSupported) {
// favored
LinkedList<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>();
interceptors.add(new OAuth1RequestInterceptor(credentials));
setInterceptors(client, interceptors);
setInterceptor(client, new OAuth1RequestInterceptor(credentials));
} else {
// 3.0.x compatibility
client.setRequestFactory(new Spring30OAuth1RequestFactory(client.getRequestFactory(), credentials));
Expand Down Expand Up @@ -83,12 +81,16 @@ public static ClientHttpRequestFactory addOAuthSigning(ClientHttpRequestFactory
* Handles the differences between 3.1M2 and 3.1RC1 setInterceptors() method signatures.
* To be removed when Spring 3.1RC1 is released.
*/
private static void setInterceptors(RestTemplate client, LinkedList<ClientHttpRequestInterceptor> interceptors) {
private static void setInterceptor(RestTemplate client, ClientHttpRequestInterceptor interceptor) {
try {
// Would like to call getInterceptors().add(interceptor), but the current Spring snapshot
// doesn't initialize the interceptors list.
Method method = RestTemplate.class.getMethod("setInterceptors", List.class);
List<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>();
interceptors.add(interceptor);
method.invoke(client, interceptors);
} catch (NoSuchMethodException e) {
setInterceptors(client, interceptors.toArray(new ClientHttpRequestInterceptor[0]));
setInterceptors(client, new ClientHttpRequestInterceptor[] { interceptor });
} catch (Exception shouldntHappen) {}
}

Expand Down
Expand Up @@ -37,9 +37,7 @@ public static RestTemplate create(String accessToken, OAuth2Version version) {
RestTemplate client = new RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
if (interceptorsSupported) {
// favored
LinkedList<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>();
interceptors.add(new OAuth2RequestInterceptor(accessToken, version));
setInterceptors(client, interceptors);
setInterceptor(client, new OAuth2RequestInterceptor(accessToken, version));
} else {
// 3.0.x compatibility
client.setRequestFactory(new Spring30OAuth2RequestFactory(client.getRequestFactory(), accessToken, version));
Expand All @@ -59,12 +57,16 @@ public static ClientHttpRequestFactory addOAuthSigning(ClientHttpRequestFactory
* Handles the differences between 3.1M2 and 3.1RC1 setInterceptors() method signatures.
* To be removed when Spring 3.1RC1 is released.
*/
private static void setInterceptors(RestTemplate client, LinkedList<ClientHttpRequestInterceptor> interceptors) {
private static void setInterceptor(RestTemplate client, ClientHttpRequestInterceptor interceptor) {
try {
// Would like to call getInterceptors().add(interceptor), but the current Spring snapshot
// doesn't initialize the interceptors list.
Method method = RestTemplate.class.getMethod("setInterceptors", List.class);
List<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>();
interceptors.add(interceptor);
method.invoke(client, interceptors);
} catch (NoSuchMethodException e) {
setInterceptors(client, interceptors.toArray(new ClientHttpRequestInterceptor[0]));
setInterceptors(client, new ClientHttpRequestInterceptor[] { interceptor });
} catch (Exception shouldntHappen) {}
}

Expand Down

0 comments on commit 71c0207

Please sign in to comment.