/** * Constructs the API template without user authorization. This is useful for accessing operations on a provider's API that do not require user authorization. */ protected AbstractOAuth1ApiBinding() { credentials = null; restTemplate = createRestTemplateWithCulledMessageConverters(); configureRestTemplate(restTemplate); }
@Override public RestTemplate getRestTemplate() { if (clientRestTemplate != null) { return clientRestTemplate; } return super.getRestTemplate(); }
/** * After construction, include option to decorate the {@link RestTemplate} followed by an optional * configuration step. Many providers initialize sub-APIs, and this provides a convenient hook. * @throws Exception if any error occurs decorating the RestTemplate */ @Override public void afterPropertiesSet() throws Exception { this.restTemplate = postProcess(this.restTemplate); postConstructionConfiguration(); }
/** * Returns a list of {@link HttpMessageConverter}s to be used by the internal {@link RestTemplate}. * By default, this includes a {@link StringHttpMessageConverter}, a {@link MappingJackson2HttpMessageConverter}, a {@link ByteArrayHttpMessageConverter}, and a {@link FormHttpMessageConverter}. * The {@link FormHttpMessageConverter} is set to use "UTF-8" character encoding. * Override this method to add additional message converters or to replace the default list of message converters. * @return a list of {@link HttpMessageConverter}s to be used by the internal {@link RestTemplate}. */ protected List<HttpMessageConverter<?>> getMessageConverters() { List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(getFormMessageConverter()); messageConverters.add(getJsonMessageConverter()); messageConverters.add(getByteArrayMessageConverter()); return messageConverters; }
@Override protected MappingJackson2HttpMessageConverter getJsonMessageConverter() { MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter(); converter.setObjectMapper(new ObjectMapper().registerModule(new TwitterModule())); return converter; }
/** * Constructs the API template with OAuth credentials necessary to perform operations on behalf of a user. * @param consumerKey the application's consumer key * @param consumerSecret the application's consumer secret * @param accessToken the access token * @param accessTokenSecret the access token secret */ protected AbstractOAuth1ApiBinding(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) { Assert.notNull(consumerKey, "Constructor argument 'consumerKey' cannot be null."); Assert.notNull(consumerSecret, "Constructor argument 'consumerSecret' cannot be null."); Assert.notNull(accessToken, "Constructor argument 'accessToken' cannot be null."); Assert.notNull(accessTokenSecret, "Constructor argument 'accessTokenSecret' cannot be null."); credentials = new OAuth1Credentials(consumerKey, consumerSecret, accessToken, accessTokenSecret); restTemplate = createRestTemplate(credentials); configureRestTemplate(restTemplate); }
private RestTemplate createRestTemplate(OAuth1Credentials credentials) { RestTemplate client = createRestTemplateWithCulledMessageConverters(); OAuth1RequestInterceptor interceptor = new OAuth1RequestInterceptor(credentials); List<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>(); interceptors.add(interceptor); client.setInterceptors(interceptors); return client; }
private RestTemplate createRestTemplateWithCulledMessageConverters() { RestTemplate client; List<HttpMessageConverter<?>> messageConverters = getMessageConverters(); try { client = new RestTemplate(messageConverters); } catch (NoSuchMethodError e) { client = new RestTemplate(); client.setMessageConverters(messageConverters); } client.setRequestFactory(ClientHttpRequestFactorySelector.getRequestFactory()); return client; } }
/** * Returns a list of {@link HttpMessageConverter}s to be used by the internal {@link RestTemplate}. * By default, this includes a {@link StringHttpMessageConverter}, a {@link MappingJackson2HttpMessageConverter}, a {@link ByteArrayHttpMessageConverter}, and a {@link FormHttpMessageConverter}. * The {@link FormHttpMessageConverter} is set to use "UTF-8" character encoding. * Override this method to add additional message converters or to replace the default list of message converters. * @return a list of {@link HttpMessageConverter}s to be used by the internal {@link RestTemplate}. */ protected List<HttpMessageConverter<?>> getMessageConverters() { List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(getFormMessageConverter()); messageConverters.add(getJsonMessageConverter()); messageConverters.add(getByteArrayMessageConverter()); return messageConverters; }
@Override protected MappingJackson2HttpMessageConverter getJsonMessageConverter() { MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter(); converter.setObjectMapper(new ObjectMapper().registerModule(new TwitterModule())); return converter; }
/** * Constructs the API template with OAuth credentials necessary to perform operations on behalf of a user. * @param consumerKey the application's consumer key * @param consumerSecret the application's consumer secret * @param accessToken the access token * @param accessTokenSecret the access token secret */ protected AbstractOAuth1ApiBinding(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) { Assert.notNull(consumerKey, "Constructor argument 'consumerKey' cannot be null."); Assert.notNull(consumerSecret, "Constructor argument 'consumerSecret' cannot be null."); Assert.notNull(accessToken, "Constructor argument 'accessToken' cannot be null."); Assert.notNull(accessTokenSecret, "Constructor argument 'accessTokenSecret' cannot be null."); credentials = new OAuth1Credentials(consumerKey, consumerSecret, accessToken, accessTokenSecret); restTemplate = createRestTemplate(credentials); configureRestTemplate(restTemplate); }
private RestTemplate createRestTemplate(OAuth1Credentials credentials) { RestTemplate client = createRestTemplateWithCulledMessageConverters(); OAuth1RequestInterceptor interceptor = new OAuth1RequestInterceptor(credentials); List<ClientHttpRequestInterceptor> interceptors = new LinkedList<ClientHttpRequestInterceptor>(); interceptors.add(interceptor); client.setInterceptors(interceptors); return client; }
private RestTemplate createRestTemplateWithCulledMessageConverters() { RestTemplate client; List<HttpMessageConverter<?>> messageConverters = getMessageConverters(); try { client = new RestTemplate(messageConverters); } catch (NoSuchMethodError e) { client = new RestTemplate(); client.setMessageConverters(messageConverters); } client.setRequestFactory(ClientHttpRequestFactorySelector.getRequestFactory()); return client; } }
/** * After construction, include option to decorate the {@link RestTemplate} followed by an optional * configuration step. Many providers initialize sub-APIs, and this provides a convenient hook. * @throws Exception if any error occurs decorating the RestTemplate */ @Override public void afterPropertiesSet() throws Exception { this.restTemplate = postProcess(this.restTemplate); postConstructionConfiguration(); }
/** * Constructs the API template without user authorization. This is useful for accessing operations on a provider's API that do not require user authorization. */ protected AbstractOAuth1ApiBinding() { credentials = null; restTemplate = createRestTemplateWithCulledMessageConverters(); configureRestTemplate(restTemplate); }
@Override public RestTemplate getRestTemplate() { if (clientRestTemplate != null) { return clientRestTemplate; } return super.getRestTemplate(); }