Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding the first face4j code. May God bless the open source world!

To quote myself: I'm just a bad guy with good intentions.
  • Loading branch information...
commit 1ebb408a7ddac5b3442d618d14246278691c6590 0 parents
@nischal nischal authored
Showing with 1,364 additions and 0 deletions.
  1. +57 −0 build.xml
  2. BIN  dist/dependencies/commons-codec-1.3.jar
  3. BIN  dist/dependencies/commons-httpclient-3.1.jar
  4. BIN  dist/dependencies/commons-logging-1.1.1.jar
  5. BIN  dist/dependencies/gson-1.3.jar
  6. BIN  dist/face4j-0.1.jar
  7. BIN  lib/commons-codec-1.3.jar
  8. BIN  lib/commons-httpclient-3.1.jar
  9. BIN  lib/commons-logging-1.1.1.jar
  10. BIN  lib/gson-1.3.jar
  11. +1 −0  readme.txt
  12. +27 −0 src/com/face4j/facebook/Client.java
  13. +88 −0 src/com/face4j/facebook/Facebook.java
  14. +36 −0 src/com/face4j/facebook/OAuthAccessToken.java
  15. +29 −0 src/com/face4j/facebook/entity/Concentration.java
  16. +29 −0 src/com/face4j/facebook/entity/Degree.java
  17. +49 −0 src/com/face4j/facebook/entity/Education.java
  18. +29 −0 src/com/face4j/facebook/entity/Employer.java
  19. +29 −0 src/com/face4j/facebook/entity/Hometown.java
  20. +29 −0 src/com/face4j/facebook/entity/Location.java
  21. +29 −0 src/com/face4j/facebook/entity/Position.java
  22. +29 −0 src/com/face4j/facebook/entity/School.java
  23. +183 −0 src/com/face4j/facebook/entity/User.java
  24. +81 −0 src/com/face4j/facebook/entity/Work.java
  25. +29 −0 src/com/face4j/facebook/entity/Year.java
  26. +25 −0 src/com/face4j/facebook/enums/Display.java
  27. +163 −0 src/com/face4j/facebook/enums/Permission.java
  28. +32 −0 src/com/face4j/facebook/enums/PictureType.java
  29. +37 −0 src/com/face4j/facebook/exception/FacebookException.java
  30. +129 −0 src/com/face4j/facebook/factory/FacebookFactory.java
  31. +5 −0 src/com/face4j/facebook/factory/OAuthFactory.java
  32. +89 −0 src/com/face4j/facebook/http/APICaller.java
  33. +48 −0 src/com/face4j/facebook/util/Constants.java
  34. +27 −0 src/com/face4j/facebook/util/JSONToObjectTransformer.java
  35. +55 −0 src/com/face4j/facebook/util/URLMaker.java
57 build.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+
+<project name="face4j" basedir="." default="help">
+ <property name="java-src.dir" value="src" />
+ <property name="lib.dir" value="lib" />
+ <property name="build.dir" value="build" />
+ <property name="project.name" value="face4j" />
+ <property name="project.version" value="0.1" />
+ <property name="dist.dir" value="dist" />
+ <property name="archive.name-face4j" value="face4j-${project.version}" />
+ <property name="exclude-classes" value="com/face4j/dummy/**,com/face4j/facebook/test/**"></property>
+
+ <path id="classpath">
+ <fileset dir="${lib.dir}"/>
+ <pathelement path="${build.dir}" />
+ </path>
+
+ <target name="help">
+ <echo message="" />
+ <echo message="${project.name} build file" />
+ <echo message="-----------------------------------" />
+ <echo message="" />
+ <echo message="Available targets are:" />
+ <echo message="" />
+ <echo message="clean --> Deletes compiled classes and JAR" />
+ <echo message="" />
+ <echo message="compile --> Compile all Java files" />
+ <echo message="jar --> Package as JAR file" />
+ </target>
+
+ <target name="compile" description="Compile main source java files">
+ <mkdir dir="${build.dir}/classes" />
+ <javac destdir="${build.dir}/classes" debug="true" optimize="false" encoding="iso-8859-1" deprecation="false" failonerror="true" excludes="${exclude-classes}">
+ <src path="${java-src.dir}" />
+
+ <classpath refid="classpath" />
+ </javac>
+ </target>
+
+ <target name="jar" depends="compile" description="packages up the face4j class files into jar files">
+ <jar destfile="${lib.dir}/${archive.name-face4j}.jar" basedir="${build.dir}/classes" />
+ <copy todir="${dist.dir}/dependencies" >
+ <fileset dir="${lib.dir}" includes="commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar,gson-1.3.jar" />
+ </copy>
+ <copy todir="${dist.dir}" >
+ <fileset dir="${lib.dir}" includes="${archive.name-face4j}.jar" />
+ </copy>
+ <delete file="${lib.dir}/${archive.name-face4j}.jar"/>
+ </target>
+
+ <target name="clean" description="Clean output directories">
+ <property name="archive.name" value="${project.name}-${project.version}" />
+ <delete dir="build" />
+ <delete file="${archive.name}.jar" />
+ </target>
+
+</project>
BIN  dist/dependencies/commons-codec-1.3.jar
Binary file not shown
BIN  dist/dependencies/commons-httpclient-3.1.jar
Binary file not shown
BIN  dist/dependencies/commons-logging-1.1.1.jar
Binary file not shown
BIN  dist/dependencies/gson-1.3.jar
Binary file not shown
BIN  dist/face4j-0.1.jar
Binary file not shown
BIN  lib/commons-codec-1.3.jar
Binary file not shown
BIN  lib/commons-httpclient-3.1.jar
Binary file not shown
BIN  lib/commons-logging-1.1.1.jar
Binary file not shown
BIN  lib/gson-1.3.jar
Binary file not shown
1  readme.txt
@@ -0,0 +1 @@
+The face4j.jar and all the other dependencies are needed.
27 src/com/face4j/facebook/Client.java
@@ -0,0 +1,27 @@
+package com.face4j.facebook;
+
+public class Client {
+
+ private String clientId;
+ private String clientSecret;
+
+ /**
+ * @param clientId The client ID of your application as provided by the provider (ex. Facebook, twitter). You would need to register your
+ * application with the provider to obtain a client id.
+ * @param clientSecret The client secret of your application
+ */
+ public Client(String clientId, String clientSecret){
+ this.clientId = clientId;
+ this.clientSecret = clientSecret;
+ }
+
+ public String getClientId() {
+ return clientId;
+ }
+
+ public String getClientSecret() {
+ return clientSecret;
+ }
+
+
+}
88 src/com/face4j/facebook/Facebook.java
@@ -0,0 +1,88 @@
+package com.face4j.facebook;
+
+import java.io.Serializable;
+import java.util.logging.Logger;
+
+import org.apache.commons.httpclient.NameValuePair;
+
+import com.face4j.facebook.entity.User;
+import com.face4j.facebook.exception.FacebookException;
+import com.face4j.facebook.http.APICaller;
+import com.face4j.facebook.util.Constants;
+import com.face4j.facebook.util.JSONToObjectTransformer;
+
+/**
+ * This is the main facebook class that will have methods which return facebook data as well as publish data to facebook
+ * This is a work in progress.
+ * @author Nischal Shetty - nischalshetty85 at gmail
+ *
+ */
+public class Facebook implements Serializable {
+
+ private static final long serialVersionUID = 350726728289608542L;
+
+ Logger logger = Logger.getLogger(Facebook.class.getName());
+
+ //private Client client;
+ private OAuthAccessToken authAccessToken;
+
+
+ /**
+ * @param authAccessToken
+ */
+ public Facebook(OAuthAccessToken authAccessToken){
+ this.authAccessToken = authAccessToken;
+ }
+
+
+ /**
+ * Returns the current user (for whom the client has been set).
+ * @return
+ * @throws Exception
+ */
+ public User getCurrentUser() throws FacebookException{
+ return getUser(Constants.SELF_PROFILE_INFO);
+ }
+
+
+ /**
+ * Returns a facebook users available info.
+ * @param username
+ * @return
+ * @throws FacebookException
+ */
+ public User getUser(String username) throws FacebookException{
+
+ //APICaller would retrieve the json string object from facebook by making a https call
+ APICaller caller = APICaller.getInstance();
+ String userJson = null;
+
+ NameValuePair[] nameValuePairs = {new NameValuePair(Constants.PARAM_ACCESS_TOKEN,this.authAccessToken.getAccessToken())};
+
+ //userJson = caller.getData(Constants.FACEBOOK_GRAPH_URL+"/"+username+"?"+Constants.PARAM_ACCESS_TOKEN+"="+URLEncoder.encode(this.authAccessToken.getAccessToken(),"UTF-8"));
+ userJson = caller.getData(Constants.FACEBOOK_GRAPH_URL+"/"+username,nameValuePairs);
+
+ //Once the json string object is obtaind, it is passed to obj transformer and the right object is retrieved
+ User user = JSONToObjectTransformer.getUser(userJson);
+
+ return user;
+ }
+
+ /**
+ * Returns a facebook users available info.
+ * @param username
+ * @return
+ * @throws FacebookException
+ */
+ public User getUser(long id) throws FacebookException{
+ return getUser(""+id);
+ }
+
+
+
+ public OAuthAccessToken getAuthAccessToken() {
+ return authAccessToken;
+ }
+
+
+}
36 src/com/face4j/facebook/OAuthAccessToken.java
@@ -0,0 +1,36 @@
+package com.face4j.facebook;
+
+public class OAuthAccessToken {
+
+ private String accessToken;
+ private Long expires;
+
+ public OAuthAccessToken(String accessToken){
+ this.accessToken = accessToken;
+ }
+
+ /**
+ * The access token as obtained from facebook.
+ * @return
+ */
+ public String getAccessToken() {
+ return accessToken;
+ }
+
+ /**
+ * Returns the expires time. null value represents the token doesn't expire meaning its long-lived
+ * @return
+ */
+ public Long getExpires() {
+ return expires;
+ }
+
+ /**
+ * Set the expires time. In case the token is long-lived set null
+ * @param expires
+ */
+ public void setExpires(Long expires) {
+ this.expires = expires;
+ }
+
+}
29 src/com/face4j/facebook/entity/Concentration.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Concentration implements Serializable{
+
+ private static final long serialVersionUID = -8571562108296652573L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
29 src/com/face4j/facebook/entity/Degree.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Degree implements Serializable{
+
+ private static final long serialVersionUID = -4401265936474298087L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
49 src/com/face4j/facebook/entity/Education.java
@@ -0,0 +1,49 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Education implements Serializable{
+
+ private static final long serialVersionUID = -3907283366668849023L;
+
+ private School school;
+
+ private Degree degree;
+
+ private Year year;
+
+ private Concentration[] concentrations;
+
+ public School getSchool() {
+ return school;
+ }
+
+ public void setSchool(School school) {
+ this.school = school;
+ }
+
+ public Degree getDegree() {
+ return degree;
+ }
+
+ public void setDegree(Degree degree) {
+ this.degree = degree;
+ }
+
+ public Year getYear() {
+ return year;
+ }
+
+ public void setYear(Year year) {
+ this.year = year;
+ }
+
+ public Concentration[] getConcentrations() {
+ return concentrations;
+ }
+
+ public void setConcentrations(Concentration[] concentrations) {
+ this.concentrations = concentrations;
+ }
+
+}
29 src/com/face4j/facebook/entity/Employer.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Employer implements Serializable {
+
+ private static final long serialVersionUID = 7073751319710108889L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
29 src/com/face4j/facebook/entity/Hometown.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Hometown implements Serializable {
+
+ private static final long serialVersionUID = 140766500508933899L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
29 src/com/face4j/facebook/entity/Location.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Location implements Serializable {
+
+ private static final long serialVersionUID = 172082218551598833L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
29 src/com/face4j/facebook/entity/Position.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Position implements Serializable {
+
+ private static final long serialVersionUID = 4665423707810638566L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
29 src/com/face4j/facebook/entity/School.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class School implements Serializable{
+
+ private static final long serialVersionUID = 1652570725506319844L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
183 src/com/face4j/facebook/entity/User.java
@@ -0,0 +1,183 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+import com.face4j.facebook.enums.PictureType;
+import com.face4j.facebook.util.URLMaker;
+
+public class User implements Serializable{
+
+ private static final long serialVersionUID = -7351804744720611823L;
+
+ private long id;
+
+ private String firstName;
+
+ private String lastName;
+
+ private String name;
+
+ private String link;
+
+ private String about;
+
+ private String birthday;
+
+ private Work[] work;
+
+ private Education[] education;
+
+ private String email;
+
+ private String website;
+
+ private Hometown hometown;
+
+ /**
+ * The user's ID
+ */
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ /**
+ * The user's first name
+ */
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ /**
+ * The user's last name
+ */
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ /**
+ * The user's full name
+ */
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * A link to the user's profile
+ */
+ public String getLink() {
+ return link;
+ }
+
+ public void setLink(String link) {
+ this.link = link;
+ }
+
+ public String getAbout() {
+ return about;
+ }
+
+ public void setAbout(String about) {
+ this.about = about;
+ }
+
+ /**
+ * The user's birthday. Format is MM/dd/yyyy . Can also be MM/dd (facebook let's users decide whether they want to display the date or no).
+ * Users can also opt to keep their birthday private. In this case, even if you have access to a users birthday, you will not be able to access it.
+ */
+ public String getBirthday() {
+ return birthday;
+ }
+
+ public void setBirthday(String birthday) {
+ this.birthday = birthday;
+ }
+
+
+ /**
+ * The proxied or contact email address granted by the user
+ */
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ /**
+ * A link to the user's personal website
+ */
+ public String getWebsite() {
+ return website;
+ }
+
+ public void setWebsite(String website) {
+ this.website = website;
+ }
+
+ public Hometown getHometown() {
+ return hometown;
+ }
+
+ public void setHometown(Hometown hometown) {
+ this.hometown = hometown;
+ }
+
+
+ /**
+ * A list of the work history from the user's profile
+ */
+ public Work[] getWork() {
+ return work;
+ }
+
+ public void setWork(Work[] work) {
+ this.work = work;
+ }
+
+ /**
+ * A list of the education history from the user's profile
+ */
+ public Education[] getEducation() {
+ return education;
+ }
+
+ public void setEducation(Education[] education) {
+ this.education = education;
+ }
+
+ /**
+ * Returns the default image profile pic url of a user
+ * @return
+ */
+ public String getPictureURL() {
+ return URLMaker.getImageURL(this.id);
+ }
+
+ /**
+ * Returns the url along with a param that indicates the size of the pic
+ * @param pictureType
+ * @return
+ */
+ public String getPictureURL(PictureType pictureType) {
+ return URLMaker.getImageURL(this.id, pictureType);
+ }
+
+
+}
81 src/com/face4j/facebook/entity/Work.java
@@ -0,0 +1,81 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class Work implements Serializable{
+
+ private static final long serialVersionUID = -2524534086097442175L;
+
+ private Employer employer;
+
+ private Location location;
+
+ private Position position;
+
+ private String startDate; //date format returned by facebook is yyyy-MM
+
+ private String endDate; //date format returned by facebook is yyyy-MM
+
+ public Employer getEmployer() {
+ return employer;
+ }
+
+ public void setEmployer(Employer employer) {
+ this.employer = employer;
+ }
+
+ public Date getStartDate() {
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
+ Date date = null;
+ if(this.startDate!=null){
+ try {
+ date = dateFormat.parse(this.startDate);
+ } catch (ParseException e) {
+ }
+ }
+ return date;
+ }
+
+ public void setStartDate(String startDate) {
+ this.startDate = startDate;
+ }
+
+ public Date getEndDate() {
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
+ Date date = null;
+ if(this.endDate!=null){
+ try {
+ date = dateFormat.parse(this.endDate);
+ } catch (ParseException e) {
+ }
+ }
+ return date;
+ }
+
+ public void setEndDate(String endDate) {
+ this.endDate = endDate;
+ }
+
+ public Location getLocation() {
+ return location;
+ }
+
+ public void setLocation(Location location) {
+ this.location = location;
+ }
+
+ public Position getPosition() {
+ return position;
+ }
+
+ public void setPosition(Position position) {
+ this.position = position;
+ }
+
+
+
+}
29 src/com/face4j/facebook/entity/Year.java
@@ -0,0 +1,29 @@
+package com.face4j.facebook.entity;
+
+import java.io.Serializable;
+
+public class Year implements Serializable{
+
+ private static final long serialVersionUID = -1878949062349966955L;
+
+ private long id;
+
+ private String name;
+
+ 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;
+ }
+
+}
25 src/com/face4j/facebook/enums/Display.java
@@ -0,0 +1,25 @@
+package com.face4j.facebook.enums;
+
+public enum Display {
+
+ /**
+ * Display a full-page authorization screen (the default)
+ */
+ PAGE,
+
+ /**
+ * Display a compact dialog optimized for web popup windows
+ */
+ POPUP,
+
+ /**
+ * Display a WAP / mobile-optimized version of the dialog
+ */
+ WAP,
+
+ /**
+ * Display an iPhone/Android/smartphone-optimized version of the dialog
+ */
+ TOUCH
+
+}
163 src/com/face4j/facebook/enums/Permission.java
@@ -0,0 +1,163 @@
+package com.face4j.facebook.enums;
+
+public enum Permission {
+
+ /**
+ * Enables your application to post content, comments, and likes to a user's stream and to the streams of the user's friends,
+ * without prompting the user each time.
+ */
+ PUBLISH_STREAM,
+
+ /**
+ * Enables your application to create and modify events on the user's behalf
+ */
+ CREATE_EVENT ,
+
+ /**
+ * Enables your application to RSVP to events on the user's behalf
+ */
+ RSVP_EVENT,
+
+ /**
+ * Enables your application to send messages to the user and respond to messages from the user via text message
+ */
+ SMS,
+
+ /**
+ * Enables your application to perform authorized requests on behalf of the user at any time.
+ * By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user
+ * when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.
+ */
+ OFFLINE_ACCESS,
+
+ /**
+ * Provides access to the user's primary email address in the email property. Do not spam users.
+ * Your use of email must comply both with Facebook policies and with the CAN-SPAM Act.
+ */
+ EMAIL,
+
+ /**
+ * Provides read access to the Insights data for pages, applications, and domains the user owns.
+ */
+ READ_INSIGHTS,
+
+ /**
+ * Provides access to all the posts in the user's News Feed and enables your application to perform searches against the user's News Feed
+ */
+ READ_STREAM,
+
+ /**
+ * Provides access to the "About Me" section of the profile in the about property
+ */
+ USER_ABOUT_ME,
+
+ /**
+ * Provides access to the user's list of activities as the activities connection
+ */
+ USER_ACTIVITIES,
+
+ /**
+ * Provides access to the full birthday with year as the birthday_date property
+ */
+ USER_BIRTHDAY,
+
+ /**
+ * Provides access to education history as the education property
+ */
+ USER_EDUCATION_HISTORY,
+
+ /**
+ * Provides access to the list of events the user is attending as the events connection
+ */
+ USER_EVENTS,
+
+ /**
+ * Provides access to the list of groups the user is a member of as the groups connection
+ */
+ USER_GROUPS,
+
+ /**
+ * Provides access to the user's hometown in the hometown property
+ */
+ USER_HOMETOWN,
+
+ /**
+ * Provides access to the user's list of interests as the interests connection
+ */
+ USER_INTERESTS,
+
+ /**
+ * Provides access to the list of all of the pages the user has liked as the likes connection
+ */
+ USER_LIKES,
+
+ /**
+ * Provides access to the user's current location as the current_location property
+ */
+ USER_LOCATION,
+
+ /**
+ * Provides access to the user's notes as the notes connection
+ */
+ USER_NOTES,
+
+ /**
+ * Provides access to the user's online/offline presence
+ */
+ USER_ONLINE_PRESENCE,
+
+ /**
+ * Provides access to the photos the user has been tagged in as the photos connection
+ */
+ USER_PHOTO_VIDEO_TAGS,
+
+ /**
+ * Provides access to the photos the user has uploaded
+ */
+ USER_PHOTOS,
+
+ /**
+ * Provides access to the user's family and personal relationships and relationship status
+ */
+ USER_RELATIONSHIPS,
+
+ /**
+ * Provides access to the user's religious and political affiliations
+ */
+ USER_RELIGION_POLITICS,
+
+ /**
+ * Provides access to the user's most recent status message
+ */
+ USER_STATUS,
+
+ /**
+ * Provides access to the videos the user has uploaded
+ */
+ USER_VIDEOS,
+
+ /**
+ * Provides access to the user's web site URL
+ */
+ USER_WEBSITE,
+
+ /**
+ * Provides access to work history as the work propertyProvides access to work history as the work property
+ */
+ USER_WORK_HISTORY,
+
+ /**
+ * Provides read access to the user's friend lists
+ */
+ READ_FRIENDLISTS,
+
+ /**
+ * Provides read access to the user's friend requests
+ */
+ READ_REQUESTS,
+
+ FRIENDS_ABOUT_ME, FRIENDS_ACTIVITIES, FRIENDS_BIRTHDAY, FRIENDS_EDUCATION_HISTORY, FRIENDS_EVENTS, FRIENDS_GROUPS, FRIENDS_HOMETOWN, FRIENDS_INTERESTS,
+ FRIENDS_LIKES, FRIENDS_LOCATION, FRIENDS_NOTES, FRIENDS_ONLINE_PRESENCE, FRIENDS_PHOTO_VIDEO_TAGS, FRIENDS_PHOTOS, FRIENDS_RELATIONSHIPS, FRIENDS_RELIGION_POLITICS,
+ FRIENDS_STATUS, FRIENDS_VIDEOS, FRIENDS_WEBSITE, FRIENDS_WORK_HISTORY
+
+}
32 src/com/face4j/facebook/enums/PictureType.java
@@ -0,0 +1,32 @@
+package com.face4j.facebook.enums;
+
+public enum PictureType {
+
+ /**
+ * The url would return a 50X50 image
+ */
+ SQUARE("square"),
+
+ /**
+ * The url would return an image 50 pixels wide and of variable height
+ */
+ SMALL("small"),
+
+ /**
+ * The url would return an image 200 pixels wide and of variable height
+ */
+ LARGE("large");
+
+ private String type;
+
+ private PictureType(String type){
+ this.type = type;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+
+
+}
37 src/com/face4j/facebook/exception/FacebookException.java
@@ -0,0 +1,37 @@
+package com.face4j.facebook.exception;
+
+public class FacebookException extends Exception {
+
+ private static final long serialVersionUID = -3433466538055836949L;
+
+ private int statusCode;
+
+ public FacebookException(String msg) {
+ super(msg);
+ }
+
+ public FacebookException(Exception exception) {
+ super(exception);
+ }
+
+ public FacebookException(String msg, int statusCode) {
+ super(msg);
+ this.statusCode = statusCode;
+ }
+
+ public FacebookException(String msg, Exception exception) {
+ super(msg, exception);
+ }
+
+ public FacebookException(String msg, Exception exception, int statusCode) {
+ super(msg, exception);
+ this.statusCode = statusCode;
+
+ }
+
+ public int getStatusCode() {
+ return this.statusCode;
+ }
+
+
+}
129 src/com/face4j/facebook/factory/FacebookFactory.java
@@ -0,0 +1,129 @@
+package com.face4j.facebook.factory;
+
+import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.httpclient.NameValuePair;
+
+import com.face4j.facebook.Client;
+import com.face4j.facebook.Facebook;
+import com.face4j.facebook.OAuthAccessToken;
+import com.face4j.facebook.enums.Display;
+import com.face4j.facebook.enums.Permission;
+import com.face4j.facebook.http.APICaller;
+import com.face4j.facebook.util.Constants;
+
+/**
+ * You can create a singleton instance of this class. This class is thread safe and you can have
+ * one instance for every unique client id and client secret
+ * @author Nischal Shetty
+ */
+public class FacebookFactory extends OAuthFactory implements Serializable {
+
+ private static final long serialVersionUID = 8997415594842693532L;
+
+ private Client client;
+
+ Logger logger = Logger.getLogger(Facebook.class.getName());
+
+ public FacebookFactory(String clientId, String clientSecret){
+ this(new Client(clientId, clientSecret));
+ }
+
+ public FacebookFactory(Client client){
+ this.client = client;
+ }
+
+ /**
+ * Returns a new instance of Facebook pertaining to the authenticated user
+ * @param accessToken
+ * @return Facebook instance
+ */
+ public Facebook getInstance(OAuthAccessToken accessToken){
+ return new Facebook(accessToken);
+ }
+
+
+ /**
+ * This will return the redirect URL. You need to redirect the user to this URL.
+ * @return The URL to redirect the user to. If the callbackURL is null then the default URL as set is obtained
+ */
+ public String getRedirectURL(String callbackURL){
+
+ return getRedirectURL(callbackURL, null, new Permission[0]);
+
+ }
+
+ public String getRedirectURL(String callbackURL, Display display, Permission... permission){
+
+ String redirectURL = null;
+
+ try {
+ redirectURL = Constants.AUTHORIZE_URL+"?"+Constants.PARAM_CLIENT_ID+"="+client.getClientId()
+ + "&"+Constants.PARAM_REDIRECT_URI+"="+URLEncoder.encode(callbackURL,"UTF-8");
+
+ if(permission!=null && permission.length>0){
+ StringBuilder permissions = null;
+ for(Permission tempPermission: permission){
+ if(permissions!=null){
+ permissions.append(","+tempPermission.toString());
+ } else {
+ permissions = new StringBuilder(tempPermission.toString());
+ }
+ }
+ redirectURL += "&"+Constants.PARAM_PERMISSION + "=" + permissions.toString().toLowerCase();
+ }
+
+ if(display!=null){
+ redirectURL += "&"+Constants.PARAM_DISPLAY + "=" + display.toString().toLowerCase();
+ }
+
+ } catch (UnsupportedEncodingException e) {
+ logger.log(Level.SEVERE, "Unsupported encoding! Please use some other encoding", e);
+ }
+ return redirectURL;
+ }
+
+ /**
+ *
+ * @param code As passed by the authenticating site (facebook)
+ * @return
+ * @throws Exception
+ */
+ public OAuthAccessToken getOAuthAccessToken(String code, String callbackURL) throws Exception{
+
+ //We make a call with the provided code, client_id, client_secret and redirect_uri
+
+ NameValuePair[] nameValuePairs = getAccessTokenNameValuePairs(code, callbackURL);
+
+ APICaller client = APICaller.getInstance();
+ String rawData = client.getData(Constants.ACCESS_TOKEN_URL,nameValuePairs);
+
+ return getAccessToken(rawData);
+ }
+
+ private NameValuePair[] getAccessTokenNameValuePairs(String code, String callbackURL){
+
+ return new NameValuePair[]{
+ new NameValuePair(Constants.PARAM_CLIENT_ID,client.getClientId()),
+ new NameValuePair(Constants.PARAM_CLIENT_SECRET,client.getClientSecret()),
+ new NameValuePair(Constants.PARAM_REDIRECT_URI,callbackURL),
+ new NameValuePair(Constants.PARAM_CODE,code)
+ };
+ }
+
+ private OAuthAccessToken getAccessToken(String rawData) throws Exception{
+ OAuthAccessToken accessToken = null;
+
+ String finalSplit[] = rawData.split("access_token=")[1].split("&expires=");
+ accessToken = new OAuthAccessToken(finalSplit[0]);
+ if(finalSplit.length>1){
+ accessToken.setExpires(Long.parseLong(finalSplit[1]));
+ }
+ return accessToken;
+ }
+
+}
5 src/com/face4j/facebook/factory/OAuthFactory.java
@@ -0,0 +1,5 @@
+package com.face4j.facebook.factory;
+
+public abstract class OAuthFactory {
+
+}
89 src/com/face4j/facebook/http/APICaller.java
@@ -0,0 +1,89 @@
+package com.face4j.facebook.http;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.GetMethod;
+
+import com.face4j.facebook.exception.FacebookException;
+
+/**
+ * APICaller will make http rerquests, obtain that response and return it without processing. Basically, the raw response is returned by every method.
+ * @author nischal.shetty
+ *
+ */
+public class APICaller {
+
+ private static final APICaller caller = new APICaller();
+ private static HttpClient httpClient = null;
+
+ private APICaller(){
+
+ }
+
+ private synchronized static HttpClient getHttpClient() {
+ if(null==httpClient){
+ MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+ connectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 15);
+ connectionManager.getParams().setMaxTotalConnections(15);
+ httpClient = new HttpClient(connectionManager);
+ }
+ return httpClient;
+ }
+
+ public static APICaller getInstance(){
+ return caller;
+ }
+
+
+ public String getData(String url, NameValuePair[] nameValuePairs) throws FacebookException{
+ System.out.println(url);
+ HttpClient client = APICaller.getHttpClient();
+ String response = null;
+
+ GetMethod getMethod = null;
+ try{
+ getMethod = new GetMethod(url);
+
+ if(nameValuePairs!=null){
+ getMethod.setQueryString(nameValuePairs);
+ }
+
+ int statusCode = client.executeMethod(getMethod);
+ if (statusCode != HttpStatus.SC_OK) {
+ throw new FacebookException ("I guess you are not permitted to access this url. HTTP status code:" + statusCode,statusCode);
+ }
+ response = getMethod.getResponseBodyAsString();
+ } catch (HttpException e) {
+ throw new FacebookException("Http Exception while calling facebook!",e);
+ } catch (IOException e) {
+ throw new FacebookException("IO Exception while calling facebook!",e);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ return response;
+ }
+
+ //TODO: Post data and delete data methods
+
+ /*public static void main(String[] args) {
+
+ APICaller client = APICaller.getInstance();
+
+ //https://graph.facebook.com/me
+ //access_token=118528751499071|4d6f2914cdc4d0177cdb5a66-544232058|cIgUXhJ5bnj5i5orhdqqrVUh3TA.
+ try {
+ client.getData("https://graph.facebook.com/me?access_token=118528751499071|4d6f2914cdc4d0177cdb5a66-544232058|cIgUXhJ5bnj5i5orhdqqrVUh3TA.");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }*/
+
+
+
+}
48 src/com/face4j/facebook/util/Constants.java
@@ -0,0 +1,48 @@
+package com.face4j.facebook.util;
+
+public class Constants {
+
+ /**
+ * The user would be redirected to this URL where access to the 3rd party application needs to be provided
+ */
+ public static final String AUTHORIZE_URL = Constants.FACEBOOK_GRAPH_URL+"/oauth/authorize";
+
+ /**
+ * This URL will return the access token
+ */
+ public static final String ACCESS_TOKEN_URL = Constants.FACEBOOK_GRAPH_URL+"/oauth/access_token";
+
+ /**
+ * With the access token, FACEBOOK_GRAPH_URL is used to make calls for appropriate data
+ */
+ public static final String FACEBOOK_GRAPH_URL = "https://graph.facebook.com";
+
+ public static final String SELF_PROFILE_INFO = "me";
+
+
+ public static final String PARAM_CLIENT_ID = "client_id";
+
+ public static final String PARAM_CLIENT_SECRET = "client_secret";
+
+ public static final String PARAM_REDIRECT_URI = "redirect_uri";
+
+ public static final String PARAM_ACCESS_TOKEN = "access_token";
+
+ public static final String PARAM_CODE = "code";
+
+ public static final String PARAM_PERMISSION = "scope";
+
+ public static final String PARAM_DISPLAY = "display";
+
+ /**
+ * To get the image pic for any facebook object, the graph url should end with /picture
+ */
+ public static final String PICTURE = "picture";
+
+ /**
+ * This parameter needs to be sent along with the picture url to indicate the size of the pic to be returned. Not mandatory
+ */
+ public static final String PARAM_PICTURE_TYPE = "type";
+
+
+}
27 src/com/face4j/facebook/util/JSONToObjectTransformer.java
@@ -0,0 +1,27 @@
+package com.face4j.facebook.util;
+
+import com.face4j.facebook.entity.User;
+import com.google.gson.FieldNamingPolicy;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+/**
+ * Convert JSON to appropriate objects
+ * @author nischal.shetty
+ *
+ */
+public class JSONToObjectTransformer {
+
+ /**
+ * Gson would be singleton. Please take care not to include rules in the builder that aren't common for the entire API.
+ */
+ private static final Gson gson = new GsonBuilder()
+ .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
+ .create();
+
+ public static User getUser(String userJSON){
+ System.out.println("JSON is "+userJSON);
+ return gson.fromJson(userJSON, User.class);
+ }
+
+}
55 src/com/face4j/facebook/util/URLMaker.java
@@ -0,0 +1,55 @@
+package com.face4j.facebook.util;
+
+import com.face4j.facebook.enums.PictureType;
+
+/**
+ * All URL creation should happen in this class. This will help make changes easily when things change. Since we are dealing with 3rd party APIs,
+ * frequent changes are expected
+ * @author nischal.shetty
+ *
+ */
+public class URLMaker {
+
+ /**
+ * Returns the image url of a facebook object
+ * @param username
+ * @return
+ */
+ public static String getImageURL(String username){
+ return getImageURL(username, null);
+ }
+
+ /**
+ * Returns the image url of a facebook object
+ * @param id
+ * @return
+ */
+ public static String getImageURL(long id){
+ return getImageURL(""+id);
+ }
+
+ /**
+ * Returns the image url of a facebook object
+ * @param username
+ * @param pictureType
+ * @return
+ */
+ public static String getImageURL(String username, PictureType pictureType){
+ String tempURL = Constants.FACEBOOK_GRAPH_URL+"/"+username+"/"+Constants.PICTURE;
+ if(pictureType!=null){
+ tempURL += "?"+Constants.PARAM_PICTURE_TYPE+"="+pictureType.getType();
+ }
+ return tempURL;
+ }
+
+ /**
+ * Returns the image url of a facebook object
+ * @param id
+ * @param pictureType
+ * @return
+ */
+ public static String getImageURL(long id, PictureType pictureType){
+ return getImageURL(""+id, pictureType);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.