/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.tripit.api.impl;

import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.social.oauth1.ProtectedResourceClientFactory;
import org.springframework.social.tripit.api.Trip;
import org.springframework.social.tripit.api.TripItApi;
import org.springframework.social.tripit.api.TripItProfile;
import org.springframework.web.client.RestTemplate;

/**
 * The central class for interacting with TripIt.
 * <p>
 * TripIt operations require OAuth 1 authentication. Therefore TripIt template
 * must be given the minimal amount of information required to sign requests to
 * the TripIt API with an OAuth <code>Authorization</code> header.
 * </p>
 * @author Craig Walls
 */
public class TripItTemplate implements TripItApi {
	
	private final RestTemplate restTemplate;

	/**
	 * Constructs a TripItTemplate with the minimal amount of information required to sign requests with an OAuth <code>Authorization</code> header.
	 * @param apiKey the application's API key
	 * @param apiSecret the application's API secret
	 * @param accessToken an access token acquired through OAuth authentication with LinkedIn
	 * @param accessTokenSecret an access token secret acquired through OAuth authentication with LinkedIn
	 */
	public TripItTemplate(String apiKey, String apiSecret, String accessToken, String accessTokenSecret) {
		this.restTemplate = ProtectedResourceClientFactory.create(apiKey, apiSecret, accessToken, accessTokenSecret);
		registerTwitterModule(restTemplate);
	}

	public String getProfileId() {
		return getUserProfile().getId();
	}

	public String getProfileUrl() {
		return getUserProfile().getProfileUrl();
	}

	public TripItProfile getUserProfile() {
		return restTemplate.getForObject("https://api.tripit.com/v1/get/profile?format=json", TripItProfile.class);
	}

	public List<Trip> getUpcomingTrips() {
		return restTemplate.getForObject("https://api.tripit.com/v1/list/trip/traveler/true/past/false?format=json", TripList.class).getList();
	}

	// subclassing hooks
	
	protected RestTemplate getRestTemplate() {
		return restTemplate;
	}

	// private helper
	
	private void registerTwitterModule(RestTemplate restTemplate) {
		List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
		for (HttpMessageConverter<?> converter : converters) {
			if(converter instanceof MappingJacksonHttpMessageConverter) {
				MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) converter;
				ObjectMapper objectMapper = new ObjectMapper();				
				objectMapper.registerModule(new TripItModule());
				jsonConverter.setObjectMapper(objectMapper);
			}
		}
	}

}

@@ -924,38 +924,19 @@
 th1.
-ProtectedResourceClientFactory
+ApiTemplate
 ;%0Aim
@@ -1503,16 +1503,36 @@
 emplate 
+extends ApiTemplate 
 implemen
@@ -1549,52 +1549,8 @@
 pi %7B
-%0A%09%0A%09private final RestTemplate restTemplate;
 %0A%0A%09/
@@ -1709,19 +1709,24 @@
  @param 
-api
+consumer
 Key the 
@@ -1758,19 +1758,24 @@
  @param 
-api
+consumer
 Secret t
@@ -2032,19 +2032,24 @@
 (String 
-api
+consumer
 Key, Str
@@ -2052,19 +2052,24 @@
  String 
-api
+consumer
 Secret, 
@@ -2122,77 +2122,35 @@
 %7B%0A%09%09
-this.restTemplate = ProtectedResourceClientFactory.create(apiKey, api
+super(consumerKey, consumer
 Secr
@@ -2201,22 +2201,28 @@
 terT
-witter
+ripItJson
 Module(
-r
+getR
 estT
@@ -2228,16 +2228,18 @@
 Template
+()
 );%0A%09%7D%0A%0A%09
@@ -2430,33 +2430,36 @@
 le() %7B%0A%09%09return 
-r
+getR
 estTemplate.getF
@@ -2445,32 +2445,34 @@
  getRestTemplate
+()
 .getForObject(%22h
@@ -2589,33 +2589,36 @@
 ps() %7B%0A%09%09return 
-r
+getR
 estTemplate.getF
@@ -2608,24 +2608,26 @@
 RestTemplate
+()
 .getForObjec
@@ -2739,103 +2739,8 @@
 %09%7D%0A%0A
-%09// subclassing hooks%0A%09%0A%09protected RestTemplate getRestTemplate() %7B%0A%09%09return restTemplate;%0A%09%7D%0A%0A
 %09// 
@@ -2783,14 +2783,17 @@
 terT
-witter
+ripItJson
 Modu

Add setRequestFactory() on templates to enable custom settings such as proxy configuration.