/*
 * Copyright 2011 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.connect.support;

import org.springframework.social.connect.ServiceProviderConnectionMemento;
import org.springframework.social.connect.spi.ServiceApiAdapter;
import org.springframework.social.oauth2.OAuth2ServiceProvider;

public class OAuth2ServiceProviderConnection<S> extends AbstractServiceProviderConnection<S> {

	private String accessToken;
	
	private String refreshToken;
	
	private Long expireTime;
	
	public OAuth2ServiceProviderConnection(String providerId, String providerUserId, OAuth2ServiceProvider<S> serviceProvider,
			String accessToken, String refreshToken, Long expiresTime, ServiceApiAdapter<S> serviceApiAdapter) {
		super(providerId, providerUserId, serviceProvider.getServiceApi(accessToken), serviceApiAdapter);
		init(accessToken, refreshToken, expireTime);
	}

	public OAuth2ServiceProviderConnection(ServiceProviderConnectionMemento memento, OAuth2ServiceProvider<S> serviceProvider, ServiceApiAdapter<S> serviceApiAdapter) {
		super(memento, serviceProvider.getServiceApi(memento.getAccessToken()), serviceApiAdapter);
		init(memento.getAccessToken(), memento.getRefreshToken(), memento.getExpireTime());		
	}
	
	// subclassing hooks
	
	@Override
	public ServiceProviderConnectionMemento createMemento() {
		return new ServiceProviderConnectionMemento(getKey().getProviderId(), getKey().getProviderUserId(),
				getProfileName(), getProfileUrl(), getProfilePictureUrl(), accessToken, null, refreshToken, expireTime);
	}

	// internal helpers
	
	private void init(String accessToken, String refreshToken, Long expireTime) {
		this.accessToken = accessToken;
		this.refreshToken = refreshToken;
		this.expireTime = expireTime;
	}

}

@@ -803,16 +803,70 @@
 dapter;%0A
+import org.springframework.social.oauth2.AccessGrant;%0A
 import o
@@ -1018,16 +1018,69 @@
 n%3CS%3E %7B%0A%0A
+%09private OAuth2ServiceProvider%3CS%3E serviceProvider;%0A%09%0A
 %09private
@@ -1497,16 +1497,33 @@
 %0A%09%09init(
+serviceProvider, 
 accessTo
@@ -1825,16 +1825,33 @@
 %0A%09%09init(
+serviceProvider, 
 memento.
@@ -1949,16 +1949,330 @@
 ng hooks
+%0A%0A%09@Override%0A%09protected S doRefresh() %7B%0A%09%09AccessGrant accessGrant = serviceProvider.getOAuthOperations().refreshAccessToken(refreshToken);%0A%09%09setAccessFields(accessGrant.getAccessToken(), accessGrant.getRefreshToken(), /* accessGrant.getExpireTime() */ null);%0A%09%09return serviceProvider.getServiceApi(accessToken);%0A%09%7D
 %0A%09%0A%09@Ove
@@ -2513,16 +2513,50 @@
 ssToken,
+ /* accessGrant.getExpireTime() */
  null, r
@@ -2606,17 +2606,16 @@
 helpers%0A
-%09
 %0A%09privat
@@ -2626,16 +2626,253 @@
 id init(
+OAuth2ServiceProvider%3CS%3E serviceProvider, String accessToken, String refreshToken, Long expireTime) %7B%0A%09%09this.serviceProvider = serviceProvider;%0A%09%09setAccessFields(accessToken, refreshToken, expireTime);%0A%09%7D%0A%09%0A%09private void setAccessFields(
 String a

refresh impl