<commit_before>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 <NAME>
 * 
 */
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;
		}
	}
}<commit_msg>Fix for broken Twitter search when authenticated.<commit_after>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 <NAME>
 * 
 */
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) {
		String adjustedUrl = adjustUrl(url.toString());
		OAuthRequest request = new OAuthRequest(Verb.valueOf(method.name()), adjustedUrl);
		Token token = new Token(accessToken, accessTokenSecret);
		service.signRequest(token, request);
		return request.getHeaders().get("Authorization");
	}

	private String adjustUrl(String url) {
		try {
			// Scribe assumes that the URL is not yet encoded, but the request
			// factory gives us a pre-encoded URL. So, decode it before giving
			// it to Scribe.
			String decodedUrl = URLDecoder.decode(url.toString(), "UTF-8");

			// The hash sign (#), however, causes trouble in a URL because it
			// looks like a URL fragment marker. Leave it encoded.
			decodedUrl = decodedUrl.replace("#", "%23");
			return decodedUrl;
		} catch (UnsupportedEncodingException shouldntHappen) {
			return null;
		}
	}
}
{'fraction_non_alphanumeric': 0.09256092347156905, 'fraction_numerical': 0.0032064985036340315, 'mean_word_length': 4.3967704728950405, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7394728', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1287, 'n_words': 368}