/*
 * 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.oauth2;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriTemplate;

/**
 * OAuth2Operations implementation that uses REST-template to make the OAuth calls.
 * @author Keith Donald
 */
public class OAuth2Template implements OAuth2Operations {

	private final String clientId;
	
	private final String clientSecret;

	private final String accessTokenUrl;

	private final UriTemplate authorizeUrlTemplate;
	
	private final RestTemplate restTemplate = new RestTemplate();
	
	public OAuth2Template(String clientId, String clientSecret, String authorizeUrl, String accessTokenUrl) {
		this.clientId = clientId;
		this.clientSecret = clientSecret;
		this.authorizeUrlTemplate = new UriTemplate(authorizeUrl);
		this.accessTokenUrl = accessTokenUrl;
	}

	public String buildAuthorizeUrl(String redirectUri, String scope) {
		Map<String, String> urlVariables = new HashMap<String, String>();
		urlVariables.put("client_id", clientId);
		urlVariables.put("redirect_uri", redirectUri);
		urlVariables.put("scope", scope);
		return authorizeUrlTemplate.expand(urlVariables).toString();
	}

	public AccessGrant exchangeForAccess(String authorizationCode, String redirectUri) {
		MultiValueMap<String, String> requestParameters = new LinkedMultiValueMap<String, String>();
		requestParameters.set("client_id", clientId);
		requestParameters.set("client_secret", clientSecret);
		requestParameters.set("code", authorizationCode);
		requestParameters.set("redirect_uri", redirectUri);
		requestParameters.set("grant_type", "authorization_code");
		@SuppressWarnings("unchecked")
		Map<String, String> result = getRestOperations().postForObject(accessTokenUrl, requestParameters, Map.class);
		return new AccessGrant(result.get("access_token"), result.get("refresh_token"));
	}

	// subclassing hooks
	
	protected RestOperations getRestOperations() {
		return restTemplate;
	}
}

@@ -696,21 +696,155 @@
 va.util.
-Map;%0A
+List;%0Aimport java.util.Map;%0A%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.converter.FormHttpMessageConverter;
 %0Aimport 
@@ -1765,16 +1765,50 @@
 kenUrl;%0A
+%09%09addTextToMapMessageConverter();%0A
 %09%7D%0A%0A%09pub
@@ -2630,30 +2630,25 @@
 Map%3CString, 
-String
+?
 %3E result = g
@@ -2752,16 +2752,24 @@
 ssGrant(
+valueOf(
 result.g
@@ -2786,18 +2786,27 @@
 _token%22)
+)
 , 
+valueOf(
 result.g
@@ -2825,16 +2825,17 @@
 token%22))
+)
 ;%0A%09%7D%0A%0A%09/
@@ -2930,10 +2930,1205 @@
 ate;%0A%09%7D%0A
+%09%0A%09// private helpers%0A%09// TODO : Can probably tweak RestTemplate's message converters to deal with this better.%0A%09private String valueOf(Object object) %7B%0A%09%09if (object == null) %7B%0A%09%09%09return null;%0A%09%09%7D else if (object instanceof List) %7B%0A%09%09%09List list = (List) object;%0A%09%09%09if (list.size() %3E 0) %7B%0A%09%09%09%09return String.valueOf(list.get(0));%0A%09%09%09%7D%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return String.valueOf(object);%0A%09%7D%0A%0A%09/*%0A%09 * Facebook returns form-encoded results with a content type of %22text/plain%22. The %22text/plain%22 content type prevents%0A%09 * any of the default encoders from being able to parse the results, even though FormHttpMessageConverter is%0A%09 * perfectly capable of doing so. This method adds another FormHttpMessageConverter that can read %22text/plain%22 into%0A%09 * a Map so that this works for Facebook.%0A%09 */%0A%09private void addTextToMapMessageConverter() %7B%0A%09%09FormHttpMessageConverter messageConverter = new FormHttpMessageConverter() %7B%0A%09%09%09public boolean canRead(Class%3C?%3E clazz, MediaType mediaType) %7B%0A%09%09%09%09return clazz.equals(Map.class) && mediaType != null && mediaType.getType().equals(%22text%22)%0A%09%09%09%09%09%09&& mediaType.getSubtype().equals(%22plain%22);%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09restTemplate.getMessageConverters().add(messageConverter);%0A%09%7D%0A
 %7D%0A

Fix for handling either JSON or form-encoded access token results from providers.