import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.*;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import com.facebook.api.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SocialJavaTutorial extends HttpServlet {
int count;
//Application API Key and application secret from creating app in FB
String appapikey = new String("7623a6ab94a7cc519b1fb73627afdda0");
String appsecret = new String("90e48- your secret here - df6d957e");
//Facebook loginPage to this application. Parameter canvas=true shows the result in Facebook canvas
String loginPage = "http://www.facebook.com/login.php?api_key="+"00b0049fc7cf0d1e4a4ba2ea3e55b269"+"&v=1.0&canvas=true";
String profilePage = "http://www.facebook.com/profile.php";
FacebookJsonRestClient facebook; // the facebook client, talks to REST Server
PrintWriter servletOutput; // output of servlet. HTML or FBML out
public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException{
servletOutput = res.getWriter(); // response is sent to ServletOutput
res.setContentType( "text/html" );
servletOutput.println("This is the doGet method");
}
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
servletOutput = res.getWriter(); // response is sent to ServletOutput
res.setContentType( "text/html" );
// do authentication
String user =null;
String sessionKey=null;
sessionKey = req.getParameter(FacebookParam.SESSION_KEY.toString()); // Session Key passed as request parameter
if (sessionKey==null) { // If there is not session key, they user not logged in
servletOutput.println("<fb:redirect url=" + loginPage + "/>"); // Facebook Redirect to login page
}
else{ // user is logged on
//get MockFBMLText from Profile as a parameter. If it exists, send it to servlet & skip the rest
String mockfbmltext = req.getParameter("mockfbmltext");
if (mockfbmltext!=null){
servletOutput.println(mockfbmltext);
}else{ // if MockFBMLText is set, don't do anything else
facebook = new FacebookJsonRestClient(appapikey, appsecret,sessionKey); // create Facebook Json Rest Client
try{
// Get user id as parameter
user = req.getParameter("fb_sig_user"); // get user as a string. User info passed as request parameter
Long userLong = new Long(user); // need user as a long for API calls
// get profiletext as parameter, if not null, then display on profile, redirect to profile page
String displayText = req.getParameter("profiletext");
if (displayText!=null){
facebook.profile_setFBML(displayText, userLong);
servletOutput.println("<fb:redirect url=" + profilePage + "/>"); // Facebook Redirect to login page
}else{
// render rest of page if displayText is not a parameter
// Display User ID
servletOutput.println("User is " + user +"<br>"); // displays numeric value
// use JDBC to store and update the count
Connection con;
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
servletOutput.println(ex +" error");
}
try {
con = DriverManager.getConnection("jdbc:mysql://HOST/DATABASE?" +
"user=USER_NAME&password=PASSWORD");
String query;
try {
stmt = con.createStatement();
query ="Select count from counter"; //counter is the table that contains count
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
int count = rs.getInt("count");
count++;
servletOutput.println("the count is " + count +" <br>");
query ="UPDATE counter SET count=" + count; //update count in Database
stmt.executeUpdate(query);
} else { // if it is the first time access
stmt = con.createStatement();
query ="INSERT INTO counter (count) VALUES (1)";
stmt.executeUpdate(query);
servletOutput.println("You are number 1");
}
con.close();
} catch(SQLException ex) {
servletOutput.println("SQLException: " + ex.getMessage());
}
} catch (SQLException ex) {
// handle any errors
servletOutput.println("SQLException: " + ex.getMessage());
servletOutput.println("SQLState: " + ex.getSQLState());
servletOutput.println("VendorError: " + ex.getErrorCode());
}
// Try FQL to get name
String query = "SELECT name FROM user WHERE uid=" + user;
org.json.JSONArray resultArray = (org.json.JSONArray)facebook.fql_query(query);
try{
JSONObject result = resultArray.getJSONObject(0);
servletOutput.println("User Name is " + result.getString("name"));
servletOutput.println("<br>");
} catch(JSONException jex){
servletOutput.println(">Error: JSON " + jex );
}
// Set up input form on canvas page
servletOutput.println("</pre>");
servletOutput.println("<form action=\"\" method=\"post\">");
servletOutput.println("<input name=\"profiletext\" type=\"text\" size=\"30\" value=\"\"><br>");
servletOutput.println("<input name=\"submit\" type=\"submit\" value=\"Display text on profile\">");
servletOutput.println("</form>");
// Set up mockajax form on profile page
String appCallBackUrl = "http://www.socialjava.com/servlet/SocialJavaTutorial/";
String mockAjax;
mockAjax="<form>";
mockAjax+="<input name=\"mockfbmltext\" type=\"text\" size=\"30\">";
mockAjax+="<br />";
mockAjax+="<input type=\"submit\"";
mockAjax+=" clickrewriteurl=\""+ appCallBackUrl +"\"";
mockAjax+=" clickrewriteid=\"preview\" value=\"Draw text below\"";
mockAjax+="/>";
mockAjax+="<br />";
mockAjax+="<div id=\"preview\" style=\"border-style: solid; border-color: black;";
mockAjax+=" border-width: 1px; padding: 5px;\">";
mockAjax+="</div>";
mockAjax+="</form>";
facebook.profile_setFBML(mockAjax.toString(), userLong);
}
}//try
catch( FacebookException ex )
{
servletOutput.println(">Error: Couldn't talk to Facebook> " + ex );
}
} //else user is logged in
}//else mockajax
servletOutput.close();
} // end doPost()
}
0 comments:
Post a Comment