First cut of Facebook integration:

1) Web Script framework extensions for supporting the development of Alfresco based Facebook applications
2) Sample Document Library Facebook application
3) FormData enhancements for supporting request args retrieval for multipart/form-data encoding
4) Sort order added to Search Javascript API

Note: Stuff needs commenting, tidy up, but at least it's in SVN.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7259 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-10-30 18:06:00 +00:00
parent 9774a4b23d
commit 4ed645e960
25 changed files with 1247 additions and 169 deletions

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.web.config.ServerConfigElement;
import org.alfresco.web.scripts.WebScriptMatch;
import org.alfresco.web.scripts.WebScriptRegistry;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptServletAuthenticator;
import org.alfresco.web.scripts.WebScriptServletRuntime;
/**
* HTTP Servlet Web Script Runtime
*
* @author davidc
*/
public class FacebookAPIRuntime extends WebScriptServletRuntime
{
/**
* Construct
*
* @param registry
* @param serviceRegistry
* @param authenticator
* @param req
* @param res
*/
public FacebookAPIRuntime(WebScriptRegistry registry, ServiceRegistry serviceRegistry, WebScriptServletAuthenticator authenticator,
HttpServletRequest req, HttpServletResponse res, ServerConfigElement serverConfig)
{
super(registry, serviceRegistry, authenticator, req, res, serverConfig);
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptRuntime#createRequest(org.alfresco.web.scripts.WebScriptMatch)
*/
@Override
protected WebScriptRequest createRequest(WebScriptMatch match)
{
return new FacebookServletRequest(serverConfig, req, match, getScriptUrl());
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.web.scripts.WebScriptRuntime;
import org.alfresco.web.scripts.WebScriptServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Web Script API entry point (with Facebook authentication)
*
* @author davidc
*/
public class FacebookAPIServlet extends WebScriptServlet
{
private static final long serialVersionUID = 4209892938069597860L;
// Logger
private static final Log logger = LogFactory.getLog(FacebookAPIServlet.class);
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
if (logger.isDebugEnabled())
logger.debug("Processing facebook api request (" + req.getMethod() + ") " + req.getRequestURL() + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
WebScriptRuntime runtime = new FacebookAPIRuntime(registry, serviceRegistry, authenticator, req, res, serverConfig);
runtime.executeScript();
}
/**
* @return default authenticator (bean name)
*/
@Override
protected String getDefaultAuthenticator()
{
return "facebook.authenticator";
}
}

View File

@@ -0,0 +1,40 @@
package org.alfresco.web.scripts.facebook;
public class FacebookAppModel
{
String appId;
String apiKey;
String secretKey;
public FacebookAppModel(String apiKey)
{
this.apiKey = apiKey;
}
public String getApiKey()
{
return apiKey;
}
public String getSecret()
{
return secretKey;
}
public void setSecret(String secretKey)
{
this.secretKey = secretKey;
}
public String getId()
{
return appId;
}
public void setId(String appId)
{
this.appId = appId;
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import java.io.IOException;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptServletAuthenticator;
import org.alfresco.web.scripts.WebScriptServletRequest;
import org.alfresco.web.scripts.WebScriptServletResponse;
import org.alfresco.web.scripts.WebScriptDescription.RequiredAuthentication;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class FacebookAuthenticator implements WebScriptServletAuthenticator
{
// Logger
private static final Log logger = LogFactory.getLog(FacebookAuthenticator.class);
// FBML for Facebook login redirect
private static final String LOGIN_REDIRECT = "<fb:redirect url=\"http://www.facebook.com/login.php?api_key=%s&v=1.0%s\">";
/* (non-Javadoc)
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public boolean authenticate(RequiredAuthentication required, boolean isGuest, WebScriptServletRequest req, WebScriptServletResponse res)
{
// TODO: Refactor with Web Script F/W Extraction
FacebookServletRequest fbreq = (FacebookServletRequest)req;
String sessionKey = fbreq.getSessionKey();
String user = fbreq.getUserId();
if (logger.isDebugEnabled())
{
logger.debug("fb_sig_session_key = '" + sessionKey + "'");
logger.debug("fb_sig_user = '" + user + "'");
}
if ((sessionKey == null || sessionKey.length() == 0) || (user == null || user.length() == 0))
{
// session has not been established, redirect to login
String apiKey = fbreq.getApiKey();
String canvas = (fbreq.isInCanvas()) ? "&canvas" : "";
if (logger.isDebugEnabled())
{
logger.debug("fb_sig_api_key = '" + apiKey + "'");
logger.debug("fb_sig_in_canvas = '" + canvas + "'");
}
try
{
String redirect = String.format(LOGIN_REDIRECT, apiKey, canvas);
if (logger.isDebugEnabled())
logger.debug("Facebook session not established; redirecting via " + redirect);
res.getWriter().write(redirect);
}
catch (IOException e)
{
throw new WebScriptException("Redirect to login failed", e);
}
return false;
}
if (logger.isDebugEnabled())
logger.debug("Facebook session established; authenticating as user " + user);
// session has been established, authenticate as Facebook user id
AuthenticationUtil.setCurrentUser(user);
return true;
}
}

View File

@@ -0,0 +1,133 @@
package org.alfresco.web.scripts.facebook;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.facebook.api.FacebookException;
import com.facebook.api.FacebookRestClient;
public class FacebookModel
{
// Logger
private static final Log logger = LogFactory.getLog(FacebookModel.class);
private FacebookServletRequest req;
private FacebookRestClient client;
private String[] friends;
public FacebookModel(FacebookServletRequest req)
{
this.req = req;
}
private FacebookRestClient getClient()
{
if (client == null)
{
String apiKey = req.getApiKey();
String secretKey = req.getSecretKey();
String sessionKey = req.getSessionKey();
if (sessionKey == null)
{
client = new FacebookRestClient(apiKey, secretKey);
}
else
{
client = new FacebookRestClient(apiKey, secretKey, sessionKey);
}
}
return client;
}
public String[] getFriends()
{
if (friends == null)
{
friends = req.getFriends();
if (friends == null)
{
try
{
Document friendsDoc = getClient().friends_get();
NodeList uids = friendsDoc.getElementsByTagName("uid");
friends = new String[uids.getLength()];
for (int i = 0; i < uids.getLength(); i++)
{
friends[i] = uids.item(i).getTextContent();
}
}
catch(Exception e)
{
friends = new String[0];
}
}
}
return friends;
}
public String getUser()
{
return req.getUserId();
}
public String getAppId()
{
return req.getAppId();
}
public String getSessionKey()
{
return req.getSessionKey();
}
public String getApiKey()
{
return req.getApiKey();
}
public String getCanvasPath()
{
return req.getCanvasPath();
}
public String getCanvasURL()
{
return "http://apps.facebook.com/" + getCanvasPath();
}
public String getPageURL()
{
return "http://apps.facebook.com" + req.getPathInfo();
}
public String getSecret()
{
return req.getSecretKey();
}
public boolean postUserAction(String title, String body)
{
try
{
Document result = getClient().feed_publishActionOfUser(title, body);
// TODO: check result
return true;
}
catch (FacebookException e)
{
if (logger.isDebugEnabled())
logger.debug("Failed to post user action [title=" + title + ", body=" + body + "] due to " + e.toString());
}
catch (IOException e)
{
if (logger.isDebugEnabled())
logger.debug("Failed to post user action [title=" + title + ", body=" + body + "] due to " + e.toString());
}
return false;
}
}

View File

@@ -0,0 +1,118 @@
package org.alfresco.web.scripts.facebook;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.alfresco.service.cmr.repository.ScriptLocation;
import org.alfresco.web.scripts.WebScriptContext;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRegistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class FacebookService
{
// Logger
private static final Log logger = LogFactory.getLog(FacebookService.class);
// Facebook Application Cache
private Map<String, FacebookAppModel> apps = new HashMap<String, FacebookAppModel>();
private ReentrantReadWriteLock appsLock = new ReentrantReadWriteLock();
private WebScriptRegistry registry;
private WebScriptContext context;
public void setRegistry(WebScriptRegistry registry)
{
this.registry = registry;
}
FacebookAppModel getAppModel(String apiKey)
{
FacebookAppModel facebookApp = null;
appsLock.readLock().lock();
try
{
facebookApp = apps.get(apiKey);
if (facebookApp == null)
{
// Upgrade read lock to write lock
appsLock.readLock().unlock();
appsLock.writeLock().lock();
try
{
// Check again
facebookApp = apps.get(apiKey);
if (facebookApp == null)
{
if (logger.isDebugEnabled())
logger.debug("Initialising Facebook Application '" + apiKey + "'");
// Locate app initialisation script in web script store
String appPath = "/com/facebook/_apps/app." + apiKey + ".js";
ScriptLocation appScript = registry.getScriptProcessor().findScript(appPath);
if (appScript == null)
{
throw new WebScriptException("Unable to locate application initialisation script '" + appPath + "'");
}
// Execute app initialisation script
Map<String, Object> model = new HashMap<String, Object>();
FacebookAppModel app = new FacebookAppModel(apiKey);
model.put("app", app);
registry.getScriptProcessor().executeScript(appScript, model);
// Validate initialisation
if (app.getSecret() == null)
{
throw new WebScriptException("Secret key for application '" + apiKey + "' has not been specified.");
}
if (app.getApiKey() == null)
{
throw new WebScriptException("Application Id for application '" + apiKey + "' has not been specified.");
}
apps.put(apiKey, app);
facebookApp = app;
}
}
finally
{
// Downgrade lock to read
appsLock.readLock().lock();
appsLock.writeLock().unlock();
}
}
return facebookApp;
}
finally
{
appsLock.readLock().unlock();
}
}
public Map<String, FacebookAppModel> getAppModels()
{
return apps;
}
public void reset()
{
appsLock.writeLock().lock();
try
{
apps.clear();
}
finally
{
appsLock.writeLock().unlock();
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.web.scripts.WebScriptRuntime;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
/**
* Facebook Canvas Page Requests
*
* @author davidc
*/
public class FacebookServlet extends FacebookAPIServlet
{
// Logger
private static final Log logger = LogFactory.getLog(FacebookServlet.class);
// Component Dependencies
protected FacebookService facebookService;
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
if (logger.isDebugEnabled())
logger.debug("Processing facebook canvas (" + req.getMethod() + ") " + req.getRequestURL() + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
WebScriptRuntime runtime = new FacebookServletRuntime(registry, serviceRegistry, authenticator, req, res, serverConfig, facebookService);
runtime.executeScript();
}
@Override
protected void initServlet(ApplicationContext context)
{
facebookService = (FacebookService)context.getBean("facebook.service");
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.web.config.ServerConfigElement;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptMatch;
import org.alfresco.web.scripts.WebScriptServletRequest;
/**
* HTTP Servlet Web Script Request
*
* @author davidc
*/
public class FacebookServletRequest extends WebScriptServletRequest
{
private String appId;
private String secretKey;
private String pathInfo;
/**
* Construct
*
* @param serverConfig
* @param req
* @param serviceMatch
*/
public FacebookServletRequest(ServerConfigElement serverConfig, HttpServletRequest req, WebScriptMatch serviceMatch, String pathInfo)
{
super(serverConfig, req, serviceMatch);
this.pathInfo = pathInfo;
}
/*package*/ void setSecretKey(String secretKey)
{
this.secretKey = secretKey;
}
/*package*/ void setAppId(String appId)
{
this.appId = appId;
}
public String getApiKey()
{
return getParameter("fb_sig_api_key");
}
public String getUserId()
{
return getParameter("fb_sig_user");
}
public String getSessionKey()
{
return getParameter("fb_sig_session_key");
}
public boolean isInCanvas()
{
String canvas = getParameter("fb_sig_api_key");
return (canvas == null || canvas.equals("1"));
}
public String getSecretKey()
{
return secretKey;
}
public String getAppId()
{
return appId;
}
public String getCanvasPath()
{
String pathInfo = getPathInfo();
String[] pathSegments = pathInfo.split("/");
if (pathSegments.length < 3)
{
throw new WebScriptException("Cannot establish Facebook Canvas Page URL from request " + getURL());
}
return pathSegments[2];
}
public String[] getFriends()
{
String[] friends;
String friendsStr = getParameter("fb_sig_friends");
friends = (friendsStr == null) ? new String[0] : friendsStr.split(",");
return friends;
}
@Override
public String getPathInfo()
{
return pathInfo;
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.web.scripts.facebook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.web.config.ServerConfigElement;
import org.alfresco.web.scripts.WebScriptMatch;
import org.alfresco.web.scripts.WebScriptRegistry;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptServletAuthenticator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* HTTP Servlet Web Script Runtime
*
* @author davidc
*/
public class FacebookServletRuntime extends FacebookAPIRuntime
{
// Logger
private static final Log logger = LogFactory.getLog(FacebookServletRuntime.class);
protected FacebookService facebookService;
/**
* Construct
*
* @param registry
* @param serviceRegistry
* @param authenticator
* @param req
* @param res
*/
public FacebookServletRuntime(WebScriptRegistry registry, ServiceRegistry serviceRegistry, WebScriptServletAuthenticator authenticator,
HttpServletRequest req, HttpServletResponse res, ServerConfigElement serverConfig, FacebookService facebookService)
{
super(registry, serviceRegistry, authenticator, req, res, serverConfig);
this.facebookService = facebookService;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptRuntime#createRequest(org.alfresco.web.scripts.WebScriptMatch)
*/
@Override
protected WebScriptRequest createRequest(WebScriptMatch match)
{
FacebookServletRequest fbreq = new FacebookServletRequest(serverConfig, req, match, getScriptUrl());
if (match != null)
{
FacebookAppModel appModel = facebookService.getAppModel(fbreq.getApiKey());
fbreq.setSecretKey(appModel.getSecret());
fbreq.setAppId(appModel.getId());
}
if (logger.isDebugEnabled())
logger.debug("Facebook request [apiKey=" + fbreq.getApiKey() + ", user=" + fbreq.getUserId() + ", session=" + fbreq.getSessionKey() + ", secret=" + fbreq.getSecretKey() + "]");
return fbreq;
}
@Override
protected String getScriptUrl()
{
return "/facebook" + super.getScriptUrl();
}
@Override
protected String getStatusCodeTemplate(int statusCode)
{
return "/fbml." + statusCode + ".ftl";
}
@Override
protected String getStatusTemplate()
{
return "/fbml.status.ftl";
}
}