-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/springframework/social/stackoverflow/connect/StackOverflowAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...java/org/springframework/social/stackoverflow/connect/StackOverflowConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...in/java/org/springframework/social/stackoverflow/connect/StackOverflowOAuth2Template.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters