Skip to content

Commit

Permalink
trying to fix the Connect stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
robhinds committed Mar 22, 2012
1 parent 6968648 commit bf4788b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>

</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected void requireAuthorization() {
}

protected String buildUri(String path) {
// return
// "https://api.stackexchange.com/2.0/users/258813?site=stackoverflow";
return API_URL_BASE + path + API_SO_FILTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.stackoverflow.api.StackOverflow;
import org.springframework.social.stackoverflow.api.UserOperations;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.web.client.RestOperations;

public class StackOverflowTemplate extends AbstractOAuth2ApiBinding implements StackOverflow {
Expand All @@ -17,6 +18,7 @@ public class StackOverflowTemplate extends AbstractOAuth2ApiBinding implements S
*/
public StackOverflowTemplate(String accessToken) {
super(accessToken);
super.setRequestFactory(ClientHttpRequestFactorySelector.bufferRequests(getRestTemplate().getRequestFactory()));
this.userOperations = new UserTemplate(getRestTemplate(), isAuthorized());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.springframework.social.stackoverflow.connect;

import org.springframework.social.ApiException;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.UserProfileBuilder;
import org.springframework.social.stackoverflow.api.StackOverflow;
import org.springframework.social.stackoverflow.api.StackOverflowUser;

/**
* StackOverflow API Adapter implementation
*
* @author robert.hinds
*
*/
public class StackOverflowAdapter implements ApiAdapter<StackOverflow> {

public boolean test(StackOverflow stackoverflow) {
try {
stackoverflow.userOperations().getUser();
return true;
} catch (ApiException e) {
return false;
}
}

public void setConnectionValues(StackOverflow stackoverflow, ConnectionValues values) {
// StackOverflowUser user = stackoverflow.userOperations().getUser();
// values.setProviderUserId(Long.toString(user.getUserId()));
// values.setDisplayName(user.getDisplayName());
// values.setProfileUrl(user.getProfileUrl());
// values.setImageUrl(user.getProfileImageUrl());
}

public UserProfile fetchUserProfile(StackOverflow stackoverflow) {
StackOverflowUser user = stackoverflow.userOperations().getUser();
return new UserProfileBuilder().setName(user.getDisplayName()).setUsername(user.getDisplayName()).build();
}

public void updateStatus(StackOverflow api, String message) {
// not supported
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.springframework.social.stackoverflow.connect;

import org.springframework.social.connect.support.OAuth2ConnectionFactory;
import org.springframework.social.stackoverflow.api.StackOverflow;

/**
* StackOverflow ConnectionFactory implementation
*
* @author robert.hinds
*
*/
public class StackOverflowConnectionFactory extends OAuth2ConnectionFactory<StackOverflow> {

public StackOverflowConnectionFactory(String clientId, String clientSecret) {
super("stackoverflow", new StackOverflowServiceProvider(clientId, clientSecret), new StackOverflowAdapter());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
*
*/
package org.springframework.social.stackoverflow.connect;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.OAuth2Template;
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
* Implementation of OAuth2Template to override the createRestTemplate method so
* we can handle the data returned from StackOverflow. Based on teh Facebook
* implementation as SO implemented their OAuth2 based on Facebook's
*
* @author robert.hinds
*
*/
public class StackOverflowOAuth2Template extends OAuth2Template {

public StackOverflowOAuth2Template(String clientId, String clientSecret, String authorizeUrl, String accessTokenUrl) {
super(clientId, clientSecret, authorizeUrl, accessTokenUrl);
}

@Override
protected RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(2);
FormHttpMessageConverter messageConverter = new FormHttpMessageConverter() {
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return true;
}
};
converters.add(messageConverter);
converters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(converters);
return restTemplate;
}

@Override
@SuppressWarnings("unchecked")
protected AccessGrant postForAccessGrant(String accessTokenUrl, MultiValueMap<String, String> parameters) {
MultiValueMap<String, String> response = getRestTemplate().postForObject(accessTokenUrl, parameters, MultiValueMap.class);
String expires = response.getFirst("expires");
return new AccessGrant(response.getFirst("access_token"), null, null, expires != null ? Integer.valueOf(expires) : null);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.springframework.social.stackoverflow.connect;

import org.springframework.social.oauth2.AbstractOAuth2ServiceProvider;
import org.springframework.social.oauth2.OAuth2Template;
import org.springframework.social.stackoverflow.api.StackOverflow;
import org.springframework.social.stackoverflow.api.impl.StackOverflowTemplate;

Expand All @@ -14,7 +13,7 @@
public class StackOverflowServiceProvider extends AbstractOAuth2ServiceProvider<StackOverflow> {

public StackOverflowServiceProvider(String clientId, String clientSecret) {
super(new OAuth2Template(clientId, clientSecret, "https://github.com/login/oauth/authorize", "https://github.com/login/oauth/access_token"));
super(new StackOverflowOAuth2Template(clientId, clientSecret, "https://stackexchange.com/oauth", "https://stackexchange.com/oauth/access_token"));
}

@Override
Expand Down

0 comments on commit bf4788b

Please sign in to comment.