package org.springframework.social.oauth1;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
import org.springframework.http.HttpMethod;
import org.springframework.social.oauth.OAuthClientRequestSigner;

/**
 * Implementation of an {@link OAuthClientRequestSigner} that uses Scribe to
 * calculate the Authorization header.
 * 
 * @author Craig Walls
 * 
 */
public class ScribeOAuth1RequestSigner extends OAuth1ClientRequestSigner {
	private final OAuthService service;
	private final String accessToken;
	private final String accessTokenSecret;

	/**
	 * Create a new instance of ScribeOAuth1RequestSigner.
	 * 
	 * @param accessToken
	 *            the access token value
	 * @param accessTokenSecret
	 *            the access token secret
	 * @param apiKey
	 *            the API key assigned by the provider
	 * @param apiSecret
	 *            the API secret assigned by the provider
	 */
	public ScribeOAuth1RequestSigner(String apiKey, String apiSecret, String accessToken, String accessTokenSecret) {
		this.accessToken = accessToken;
		this.accessTokenSecret = accessTokenSecret;
		this.service = new ServiceBuilder().provider(PreAuthorizedOAuthApi.class).apiKey(apiKey).apiSecret(apiSecret)
				.callback("http://greenhouse.springsource.org").build();
	}

	protected String buildAuthorizationHeader(HttpMethod method, URL url, Map<String, String> parameters) {
		try {
			// Need to decode the URL given because Scribe is assuming that it
			// isn't UTF-8 encoded yet and will re-encode it.
			String decodedUrl = URLDecoder.decode(url.toString(), "UTF-8");
			OAuthRequest request = new OAuthRequest(Verb.valueOf(method.name()), decodedUrl);
			Token token = new Token(accessToken, accessTokenSecret);
			service.signRequest(token, request);
			return request.getHeaders().get("Authorization");
		} catch (UnsupportedEncodingException shouldntHappen) {
			return null;
		}
	}
}

@@ -1617,177 +1617,38 @@
 %7B%0A%09%09
+S
 tr
-y %7B%0A%09%09%09// Need to decode the URL given because Scribe is assuming that it%0A%09%09%09// isn't UTF-8 encoded yet and will re-encode it.%0A%09%09%09String decodedUrl = URLDecoder.decode
+ing adjustedUrl = adjustUrl
 (url
@@ -1662,21 +1662,11 @@
 ng()
-, %22UTF-8%22
 );%0A
-%09
 %09%09OA
@@ -1736,21 +1736,22 @@
 )), 
-decod
+adjust
 edUrl);%0A
 %09%09%09T
@@ -1746,17 +1746,16 @@
 edUrl);%0A
-%09
 %09%09Token 
@@ -1807,17 +1807,16 @@
 ret);%0A%09%09
-%09
 service.
@@ -1844,17 +1844,16 @@
 quest);%0A
-%09
 %09%09return
@@ -1896,16 +1896,493 @@
 tion%22);%0A
+%09%7D%0A%0A%09private String adjustUrl(String url) %7B%0A%09%09try %7B%0A%09%09%09// Scribe assumes that the URL is not yet encoded, but the request%0A%09%09%09// factory gives us a pre-encoded URL. So, decode it before giving%0A%09%09%09// it to Scribe.%0A%09%09%09String decodedUrl = URLDecoder.decode(url.toString(), %22UTF-8%22);%0A%0A%09%09%09// The hash sign (#), however, causes trouble in a URL because it%0A%09%09%09// looks like a URL fragment marker. Leave it encoded.%0A%09%09%09decodedUrl = decodedUrl.replace(%22#%22, %22%2523%22);%0A%09%09%09return decodedUrl;%0A
 %09%09%7D catc

Fix for broken Twitter search when authenticated.