Navigation Menu

Skip to content

Commit

Permalink
Adding user agent to request as per #1, some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellavelle committed Oct 16, 2011
1 parent c485e4f commit 01d5c7b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 28 deletions.
@@ -1,5 +1,12 @@
package org.springframework.social.lastfm.api.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;

/**
Expand All @@ -10,4 +17,8 @@
*/
class LastFmErrorHandler extends DefaultResponseErrorHandler {





}
Expand Up @@ -49,7 +49,8 @@ private void initSubApis(String token, String sessionKey, String apiKey,
* constructor will support those operations. Those operations requiring
* authentication will throw {@link NotAuthorizedException}.
*/
public LastFmTemplate() {
public LastFmTemplate(String userAgent) {
super(userAgent);
initialize(null, null, null, null);
}

Expand All @@ -61,9 +62,9 @@ public LastFmTemplate() {
* An access token given by LastFm after a successful
* authentication
*/
public LastFmTemplate(String token, String sessionKey, String apiKey,
public LastFmTemplate(String userAgent,String token, String sessionKey, String apiKey,
String secret) {
super(token, sessionKey);
super(userAgent,token, sessionKey);
initialize(token, sessionKey, apiKey, secret);

}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
Expand All @@ -13,6 +14,7 @@
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.social.ApiBinding;
import org.springframework.social.lastfm.connect.RestTemplateWithHeaders;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.web.client.RestTemplate;

Expand All @@ -24,6 +26,7 @@ public abstract class AbstractLastFmAuthApiBinding implements ApiBinding {

private final String token;
private final String sessionKey;
private final String userAgent;

private final RestTemplate restTemplate;

Expand All @@ -33,11 +36,15 @@ public abstract class AbstractLastFmAuthApiBinding implements ApiBinding {
* for accessing operations on a provider's API that do not require user
* authorization.
*/
protected AbstractLastFmAuthApiBinding() {
protected AbstractLastFmAuthApiBinding(String userAgent) {
token = null;
sessionKey = null;
restTemplate = new RestTemplate(
ClientHttpRequestFactorySelector.getRequestFactory());
this.userAgent = userAgent;
HttpHeaders headers = new HttpHeaders();
headers.add("User-Agent", userAgent);

restTemplate = new RestTemplateWithHeaders(
ClientHttpRequestFactorySelector.getRequestFactory(),headers);
restTemplate.setMessageConverters(getMessageConverters(true));


Expand All @@ -51,11 +58,15 @@ protected AbstractLastFmAuthApiBinding() {
* @param accessToken
* the access token
*/
protected AbstractLastFmAuthApiBinding(String token, String sessionKey) {
protected AbstractLastFmAuthApiBinding(String userAgent,String token, String sessionKey) {
this.token = token;
this.userAgent = userAgent;
this.sessionKey = sessionKey;
restTemplate = new RestTemplate(
ClientHttpRequestFactorySelector.getRequestFactory());
HttpHeaders headers = new HttpHeaders();
headers.add("User-Agent", userAgent);

restTemplate = new RestTemplateWithHeaders(
ClientHttpRequestFactorySelector.getRequestFactory(),headers);
restTemplate.setMessageConverters(getMessageConverters(true));
configureRestTemplate(restTemplate);

Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.http.HttpHeaders;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
Expand Down Expand Up @@ -40,28 +41,31 @@ public class LastFmAuthTemplate implements LastFmAuthOperations {
private final String authorizeUrl;

private final RestTemplate restTemplate;
protected final RestTemplate formRestTemplate;

private final String userAgent;


public LastFmAuthTemplate(String clientId, String clientSecret) {
public LastFmAuthTemplate(String clientId, String clientSecret,String userAgent) {
this(clientId, clientSecret, "http://www.last.fm/api/auth/",
"http://ws.audioscrobbler.com/2.0/");
"http://ws.audioscrobbler.com/2.0/",userAgent);
}

public LastFmAuthTemplate(String clientId, String clientSecret,
String authorizeUrl, String accessTokenUrl) {
String authorizeUrl, String accessTokenUrl,String userAgent) {
Assert.notNull(clientId, "The clientId property cannot be null");
Assert.notNull(clientSecret, "The clientSecret property cannot be null");
Assert.notNull(authorizeUrl, "The authorizeUrl property cannot be null");
Assert.notNull(userAgent, "The userAgent property cannot be null");

Assert.notNull(accessTokenUrl,
"The accessTokenUrl property cannot be null");
this.clientId = clientId;
this.clientSecret = clientSecret;
String clientInfo = "?api_key=" + formEncode(clientId);
this.authorizeUrl = authorizeUrl + clientInfo;
this.accessTokenUrl = accessTokenUrl;
this.userAgent = userAgent;
this.restTemplate = createRestTemplate(true);
this.formRestTemplate = createRestTemplate(false);

}

Expand Down Expand Up @@ -104,8 +108,10 @@ public AccessGrant exchangeForAccess(String token,
* override to register an appropriate message converter.
*/
protected RestTemplate createRestTemplate(boolean json) {
RestTemplate restTemplate = new RestTemplate(
ClientHttpRequestFactorySelector.getRequestFactory());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("User-Agent", userAgent);
RestTemplate restTemplate = new RestTemplateWithHeaders(
ClientHttpRequestFactorySelector.getRequestFactory(),httpHeaders);
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
2);
converters.add(new FormHttpMessageConverter());
Expand Down
Expand Up @@ -24,8 +24,8 @@ public class LastFmConnectionFactory extends ConnectionFactory<LastFm> {
* the LastFm APi clientId
* @param clientSecret the LastFm Api clientSecret
*/
public LastFmConnectionFactory(String clientId, String clientSecret) {
super("lastfm", new LastFmServiceProvider(clientId, clientSecret),
public LastFmConnectionFactory(String clientId, String clientSecret,String userAgent) {
super("lastfm", new LastFmServiceProvider(clientId, clientSecret,userAgent),
new LastFmAdapter());
}

Expand Down
Expand Up @@ -13,6 +13,7 @@ public class LastFmServiceProvider implements LastFmAuthServiceProvider {

private String clientId;
private String secret;
private String userAgent;

private final LastFmAuthOperations lastFmAuthOperations;

Expand All @@ -27,10 +28,11 @@ public LastFmServiceProvider(LastFmAuthOperations lastFmAuthOperations) {
this.lastFmAuthOperations = lastFmAuthOperations;
}

public LastFmServiceProvider(String clientId, String clientSecret) {
lastFmAuthOperations = new LastFmAuthTemplate(clientId, clientSecret);
public LastFmServiceProvider(String clientId, String clientSecret,String userAgent) {
lastFmAuthOperations = new LastFmAuthTemplate(clientId, clientSecret,userAgent);
this.clientId = clientId;
this.secret = clientSecret;
this.userAgent = userAgent;
}

// implementing OAuth2ServiceProvider
Expand All @@ -40,7 +42,7 @@ public final LastFmAuthOperations getLastFmAuthOperations() {
}

public LastFm getApi(String token, String sessionKey) {
return new LastFmTemplate(token, sessionKey, clientId, secret);
return new LastFmTemplate(userAgent,token, sessionKey, clientId, secret);

}

Expand Down
Expand Up @@ -81,12 +81,6 @@ public String toString() {
}
}
s = s + secret;


System.out.println("Method sig:" + s);

//String s = "api_key" + apiKey + "method" + method + "token" + token
// + secret;

byte[] defaultBytes = s.getBytes();
try {
Expand All @@ -101,7 +95,6 @@ public String toString() {
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
System.out.println("Hashtext:" + hashtext);

return hashtext;

Expand Down
@@ -0,0 +1,66 @@
package org.springframework.social.lastfm.connect;

import java.io.IOException;
import java.net.URI;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

public class RestTemplateWithHeaders extends RestTemplate {

private HttpHeaders headers = new HttpHeaders();

public HttpHeaders getRequestHttpHeaders() {
return headers;
}


public RestTemplateWithHeaders(ClientHttpRequestFactory requestFactory,HttpHeaders headers) {
super(requestFactory);
}


@Override
protected <T> T doExecute(URI url, HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {


RequestCallbackDecorator requestCallbackDecorator =
new RequestCallbackDecorator(requestCallback);

return super.doExecute(url, method,
requestCallbackDecorator, responseExtractor);

}



private class RequestCallbackDecorator implements RequestCallback
{

public RequestCallbackDecorator(
RequestCallback targetRequestCallback) {
this.targetRequestCallback = targetRequestCallback;
}

private RequestCallback targetRequestCallback;

@Override
public void doWithRequest(ClientHttpRequest request)
throws IOException {

request.getHeaders().putAll(headers);

if (null != targetRequestCallback) {
targetRequestCallback.doWithRequest(request);
}
}
}
}

0 comments on commit 01d5c7b

Please sign in to comment.