This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial commit
- Loading branch information
0 parents
commit b190311
Showing
14 changed files
with
945 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-social</artifactId> | ||
<name>Spring Social</name> | ||
<packaging>jar</packaging> | ||
<version>1.0.0-BUILD-SNAPSHOT</version> | ||
<properties> | ||
<java-version>1.6</java-version> | ||
<org.springframework-version>3.0.4.BUILD-SNAPSHOT</org.springframework-version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>${org.springframework-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security.oauth</groupId> | ||
<artifactId>spring-security-oauth</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>commons-httpclient</groupId> | ||
<artifactId>commons-httpclient</artifactId> | ||
<version>3.1</version> | ||
</dependency> | ||
|
||
<!-- Servlet --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Jackson JSON Mapper --> | ||
<dependency> | ||
<groupId>org.codehaus.jackson</groupId> | ||
<artifactId>jackson-mapper-asl</artifactId> | ||
<version>1.5.3</version> | ||
</dependency> | ||
|
||
<!-- Test --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.7</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${org.springframework-version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.8.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<repositories> | ||
<!-- For testing against latest Spring snapshots --> | ||
<repository> | ||
<id>org.springframework.maven.snapshot</id> | ||
<name>Spring Maven Snapshot Repository</name> | ||
<url>http://maven.springframework.org/snapshot</url> | ||
<releases><enabled>false</enabled></releases> | ||
<snapshots><enabled>true</enabled></snapshots> | ||
</repository> | ||
<!-- For Spring releases --> | ||
<repository> | ||
<id>org.springframework.maven.release</id> | ||
<name>Spring Maven Release Repository</name> | ||
<url>http://maven.springframework.org/release</url> | ||
<snapshots><enabled>false</enabled></snapshots> | ||
</repository> | ||
<!-- For developing against latest Spring milestones --> | ||
<repository> | ||
<id>org.springframework.maven.milestone</id> | ||
<name>Spring Maven Milestone Repository</name> | ||
<url>http://maven.springframework.org/milestone</url> | ||
<snapshots><enabled>false</enabled></snapshots> | ||
</repository> | ||
</repositories> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${java-version}</source> | ||
<target>${java-version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>install</id> | ||
<phase>install</phase> | ||
<goals><goal>sources</goal></goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/springframework/social/facebook/FacebookAccessToken.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.facebook; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/* | ||
* This isn't particularly portable or necessarily in line with what we may do with SS-OAuth and OAuth 2. But, | ||
* it's a start and it helps me get the FB access token. | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface FacebookAccessToken { | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/springframework/social/facebook/FacebookLink.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,31 @@ | ||
package org.springframework.social.facebook; | ||
|
||
public class FacebookLink { | ||
private final String link; | ||
private final String name; | ||
private final String caption; | ||
private final String description; | ||
|
||
public FacebookLink(String link, String name, String caption, String description) { | ||
this.link = link; | ||
this.name = name; | ||
this.caption = caption; | ||
this.description = description; | ||
} | ||
|
||
public String getLink() { | ||
return link; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getCaption() { | ||
return caption; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/springframework/social/facebook/FacebookOperations.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,15 @@ | ||
package org.springframework.social.facebook; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.util.MultiValueMap; | ||
|
||
public interface FacebookOperations { | ||
FacebookUserInfo getUserInfo(String accessToken); | ||
List<String> getFriendIds(String accessToken); | ||
void postToWall(String accessToken, String message); | ||
void postToWall(String accessToken, String message, FacebookLink link); | ||
void publish(String accessToken, String object, String connection, MultiValueMap<String, String> data); | ||
byte[] getProfilePicture(String accessToken); | ||
byte[] getProfilePicture(String accessToken, String profileId); | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/springframework/social/facebook/FacebookService.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,90 @@ | ||
package org.springframework.social.facebook; | ||
|
||
import static java.util.Arrays.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.client.CommonsClientHttpRequestFactory; | ||
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class FacebookService implements FacebookOperations { | ||
private RestTemplate restTemplate; | ||
|
||
public FacebookService() { | ||
MappingJacksonHttpMessageConverter jsonMessageConverter = new MappingJacksonHttpMessageConverter(); | ||
jsonMessageConverter.setSupportedMediaTypes(asList(new MediaType("text", "javascript")) ); | ||
this.restTemplate = new RestTemplate(); | ||
restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory()); | ||
this.restTemplate.getMessageConverters().add(jsonMessageConverter); | ||
} | ||
|
||
public FacebookUserInfo getUserInfo(String accessToken) { | ||
return restTemplate.getForObject(OBJECT_URL + ACCESS_TOKEN_PARAM, FacebookUserInfo.class, "me", accessToken); | ||
} | ||
|
||
public List<String> getFriendIds(String accessToken) { | ||
Map<String, List<Map<String, String>>> results = | ||
restTemplate.getForObject(CONNECTION_URL + ACCESS_TOKEN_PARAM, Map.class, | ||
CURRENT_USER, FRIENDS, accessToken); | ||
List<Map<String, String>> friends = results.get("data"); | ||
|
||
List<String> friendIds = new ArrayList<String>(); | ||
for (Map<String, String> friendData : friends) { | ||
friendIds.add(friendData.get("id")); | ||
} | ||
return friendIds; | ||
} | ||
|
||
public void postToWall(String accessToken, String message) { | ||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | ||
map.set("message", message); | ||
publish(accessToken, CURRENT_USER, FEED, map); | ||
} | ||
|
||
public void postToWall(String accessToken, String message, FacebookLink link) { | ||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | ||
map.set("link", link.getLink()); | ||
map.set("name", link.getName()); | ||
map.set("caption", link.getCaption()); | ||
map.set("description", link.getDescription()); | ||
map.set("message", message); | ||
publish(accessToken, CURRENT_USER, FEED, map); | ||
} | ||
|
||
public void publish(String accessToken, String object, String connection, MultiValueMap<String, String> data) { | ||
MultiValueMap<String, String> requestData = new LinkedMultiValueMap<String, String>(data); | ||
requestData.add("access_token", accessToken); | ||
restTemplate.postForLocation(CONNECTION_URL, requestData, object, connection); | ||
} | ||
|
||
public byte[] getProfilePicture(String accessToken) { | ||
return getProfilePicture(accessToken, "me"); | ||
} | ||
|
||
public byte[] getProfilePicture(String accessToken, String profileId) { | ||
ResponseEntity<byte[]> imageBytes = restTemplate.getForEntity(PROFILE_LARGE_PICTURE_URL, | ||
byte[].class, profileId, accessToken); | ||
return imageBytes.getBody(); | ||
} | ||
|
||
// to support unit testing | ||
void setRestTemplate(RestTemplate restTemplate) { | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
static final String PROFILE_LARGE_PICTURE_URL = "https://graph.facebook.com/{profile}/picture?type=large&access_token={token}"; | ||
static final String OBJECT_URL = "https://graph.facebook.com/{objectId}"; | ||
static final String CONNECTION_URL = OBJECT_URL + "/{connection}"; | ||
|
||
static final String ACCESS_TOKEN_PARAM = "?access_token={token}"; | ||
static final String FRIENDS = "friends"; | ||
static final String FEED = "feed"; | ||
static final String CURRENT_USER = "me"; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/springframework/social/facebook/FacebookUserId.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,14 @@ | ||
package org.springframework.social.facebook; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface FacebookUserId { | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/springframework/social/facebook/FacebookUserInfo.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.facebook; | ||
|
||
import org.codehaus.jackson.annotate.JsonIgnoreProperties; | ||
import org.codehaus.jackson.annotate.JsonProperty; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown=true) | ||
public class FacebookUserInfo { | ||
private long id; | ||
private String name; | ||
private String firstName; | ||
private String lastName; | ||
private String email; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@JsonProperty("first_name") | ||
public void setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
@JsonProperty("last_name") | ||
public void setLastName(String lastName) { | ||
this.lastName = lastName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
} |
Oops, something went wrong.