package kr.ac.kaist.resl.lilliput.core;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.ac.kaist.resl.lilliput.util.MongoHelper;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.Account;
import com.restfb.types.User;
/**
* Servlet implementation class FacebookLogInAndForwarding
*/
public class FacebookLogInAndForwarding extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FacebookLogInAndForwarding() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = (String)request.getParameter("name");
String accessToken = (String)request.getParameter("accessToken");
String result = getRegistrationStatus(accessToken, name);
request.setAttribute("result", result);
RequestDispatcher dispatcher = request.getRequestDispatcher("registration.jsp");
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
public String getRegistrationStatus(String accessToken, String name)
{
try
{
JSONObject returnJson = new JSONObject();
returnJson.put("UserAC", accessToken);
returnJson.put("UserName", name);
JSONObject returnHuman = new JSONObject();
//making facebookClient
FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
//fetch user
User user = facebookClient.fetchObject("me", User.class);
MongoHelper mHelper = new MongoHelper();
//Check whether human is already saved
String objectIdOfDB = mHelper.isHumanSavedInDB(user.getId());
if( objectIdOfDB == null )
{
returnHuman.put("isSaved", "false");
}
else
{
returnHuman.put("isSaved", "true");
JSONObject ret = mHelper.getInformationUsingFID("HumanInformation", user.getId());
JSONObject ret2 = mHelper.getInformationUsingFID("EPCStorage", user.getId());
if( ret.isNull("SyncTime") != true ) returnHuman.put("SyncTime", ret.getString("SyncTime"));
if( ret2.isNull("epc") != true ) returnHuman.put("epc", ret2.getString("epc"));
}
returnJson.put("Human", returnHuman);
//fetch Pages
Connection<Account> myPageConnections = facebookClient.fetchConnection("me/accounts", Account.class);
List<Account> myPages = myPageConnections.getData();
JSONArray pages = new JSONArray();
for(int i = 0 ; i < myPages.size() ; i ++ )
{
JSONObject pageJson = new JSONObject();
Account tempAcc = myPages.get(i);
if( tempAcc.getCategory().equals("Application"))
{
if( tempAcc.getName().equals("Lilliput-Jack") )
{
URL url = new URL("https://graph.facebook.com/oauth/access_token?client_id=136511403124173&client_secret=6d2d602142b59b0df5738c7a151c66cf&grant_type=client_credentials");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
String[] tokens = body.split("=");
returnJson.put("appAC", tokens[1]);
returnJson.put("appID", tempAcc.getId());
returnJson.put("appName", tempAcc.getName());
}
continue;
}
String tname = tempAcc.getName();
String PageaccessToken = tempAcc.getAccessToken();
String id = tempAcc.getId();
pageJson.put("name", tname);
pageJson.put("access_token", PageaccessToken);
pageJson.put("id", id);
pages.put(pageJson);
}
JSONArray registratedObject = new JSONArray();
JSONArray registratedPlace = new JSONArray();
JSONArray notRegistrated = new JSONArray();
for(int i = 0 ; i < pages.length() ; i++ )
{
JSONObject tempPage = pages.getJSONObject(i);
String uid = mHelper.isSavedInDB("EPCStorage", "fid", tempPage.getString("id"));
if( uid == null )//not registrated
{
notRegistrated.put(tempPage);
}
else
{ //urn:epc:id:sgtin:
//urn:epc:id:sgln:
String[] flags = uid.split(":");
if( flags.length < 4)
{
System.out.println( "Unexpected Error Occured : Abnormal EPC is stored in DB");
continue;
}
if( flags[3].equals("sgtin")) //object
{
JSONObject ret = mHelper.getInformationUsingFID("ObjectInformation", tempPage.getString("id"));
if( ret.isNull("SyncTime") != true ) tempPage.put("SyncTime", ret.getString("SyncTime"));
if( ret.isNull("epc") != true ) tempPage.put("epc", ret.getString("epc"));
registratedObject.put(tempPage);
}
else if( flags[3].equals("sgln")) //place
{
JSONObject ret = mHelper.getInformationUsingFID("SpaceInformation", tempPage.getString("id"));
if( ret.isNull("SyncTime") != true ) tempPage.put("SyncTime", ret.getString("SyncTime"));
if( ret.isNull("epc") != true ) tempPage.put("epc", ret.getString("epc"));
registratedPlace.put(tempPage);
}
}
}
returnJson.put("RegistratedObject", registratedObject);
returnJson.put("RegistratedPlace", registratedPlace);
returnJson.put("NotRegistrated", notRegistrated);
System.out.println("ddd");
return returnJson.toString();
}
catch( JSONException e)
{
return e.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
}
}