Publishing:

- Finish support for posting status updates to Facebook

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29110 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Brian Remmington
2011-07-17 15:24:58 +00:00
parent 6018d8defa
commit dfa3e42072
10 changed files with 105 additions and 64 deletions

View File

@@ -28,5 +28,6 @@
<import resource="classpath*:alfresco/twitter-publishing-context.xml" /> <import resource="classpath*:alfresco/twitter-publishing-context.xml" />
<import resource="classpath*:alfresco/slideshare-publishing-context.xml" /> <import resource="classpath*:alfresco/slideshare-publishing-context.xml" />
<import resource="classpath*:alfresco/flickr-publishing-context.xml" /> <import resource="classpath*:alfresco/flickr-publishing-context.xml" />
<import resource="classpath*:alfresco/facebook-publishing-context.xml" />
<import resource="classpath*:alfresco/domain/*-context.xml" /> <import resource="classpath*:alfresco/domain/*-context.xml" />
</beans> </beans>

View File

@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
<property name="models">
<list>
<value>alfresco/model/facebookPublishingModel.xml</value>
</list>
</property>
</bean>
<bean id="facebookDeliveryChannelType" class="org.alfresco.repo.publishing.facebook.FacebookChannelType" parent="baseChannelType" >
<property name="publishingHelper" ref="facebookPublishingHelper" />
</bean>
<bean id="facebookPublishingHelper" class="org.alfresco.repo.publishing.facebook.FacebookPublishingHelper">
<property name="nodeService" ref="NodeService" />
<property name="connectionFactory">
<bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
<constructor-arg value="172471752819337" />
<constructor-arg value="663c15276462894fd3f0e94b41a4e2e1" />
</bean>
</property>
</bean>
</beans>

View File

@@ -32,22 +32,7 @@
<aspect name="facebook:DeliveryChannelAspect"> <aspect name="facebook:DeliveryChannelAspect">
<title>Facebook Delivery Channel Aspect</title> <title>Facebook Delivery Channel Aspect</title>
<description>Applied to a node that represents a Facebook delivery channel</description> <description>Applied to a node that represents a Facebook delivery channel</description>
<parent>pub:UserPasswordDeliveryChannelAspect</parent> <parent>pub:OAuth2DeliveryChannelAspect</parent>
</aspect>
<aspect name="facebook:AssetAspect">
<title>Facebook Asset</title>
<description>Applied to a node that has been published to Facebook</description>
<properties>
<property name="facebook:assetId">
<title>Facebook Asset Id</title>
<type>d:text</type>
</property>
<property name="facebook:assetUrl">
<title>Facebook Asset URL</title>
<type>d:text</type>
</property>
</properties>
</aspect> </aspect>
</aspects> </aspects>
</model> </model>

View File

@@ -321,6 +321,18 @@
</properties> </properties>
</aspect> </aspect>
<aspect name="pub:OAuth2DeliveryChannelAspect">
<title>OAuth2 Authenticated Delivery Channel</title>
<description>Applied to delivery channels that use OAuth2</description>
<properties>
<property name="pub:oauth2Token">
<title>The value of the OAuth2 token</title>
<type>d:text</type>
<multiple>false</multiple>
</property>
</properties>
</aspect>
<aspect name="pub:UserPasswordDeliveryChannelAspect"> <aspect name="pub:UserPasswordDeliveryChannelAspect">
<title>Username and Password Authenticated Delivery Channel</title> <title>Username and Password Authenticated Delivery Channel</title>
<description>Applied to delivery channels that use OAuth1</description> <description>Applied to delivery channels that use OAuth1</description>
@@ -338,5 +350,19 @@
</properties> </properties>
</aspect> </aspect>
<aspect name="pub:AssetAspect">
<title>Published Asset</title>
<description>Applied to a node that has been published to an external delivery service</description>
<properties>
<property name="pub:assetId">
<title>Published Asset Id</title>
<type>d:text</type>
</property>
<property name="pub:assetUrl">
<title>Published Asset URL</title>
<type>d:text</type>
</property>
</properties>
</aspect>
</aspects> </aspects>
</model> </model>

View File

@@ -83,7 +83,7 @@ public class PublishingEventProcessor
{ {
publishEvent(channel, event); publishEvent(channel, event);
updateStatus(channel, environment, event.getStatusUpdate()); updateStatus(channel, environment, event.getStatusUpdate());
String completedStatus = PublishingEvent.Status.COMPLETE.name(); String completedStatus = PublishingEvent.Status.COMPLETED.name();
nodeService.setProperty(eventNode, PublishingModel.PROP_PUBLISHING_EVENT_STATUS, completedStatus); nodeService.setProperty(eventNode, PublishingModel.PROP_PUBLISHING_EVENT_STATUS, completedStatus);
} }
} }

View File

@@ -45,6 +45,8 @@ public interface PublishingModel
public static final QName ASPECT_CHANNEL_INFO= QName.createQName(NAMESPACE, "channelInfo"); public static final QName ASPECT_CHANNEL_INFO= QName.createQName(NAMESPACE, "channelInfo");
public static final QName ASPECT_PUBLISHED = QName.createQName(NAMESPACE, "published"); public static final QName ASPECT_PUBLISHED = QName.createQName(NAMESPACE, "published");
public static final QName ASPECT_OAUTH1_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "OAuth1DeliveryChannelAspect"); public static final QName ASPECT_OAUTH1_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "OAuth1DeliveryChannelAspect");
public static final QName ASPECT_OAUTH2_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "OAuth2DeliveryChannelAspect");
public static final QName ASPECT_ASSET = QName.createQName(NAMESPACE, "AssetAspect");
public static final QName PROP_CHANNEL = QName.createQName(NAMESPACE, "channel"); public static final QName PROP_CHANNEL = QName.createQName(NAMESPACE, "channel");
public static final QName PROP_CHANNEL_TYPE = QName.createQName(NAMESPACE, "channelType"); public static final QName PROP_CHANNEL_TYPE = QName.createQName(NAMESPACE, "channelType");
@@ -66,6 +68,9 @@ public interface PublishingModel
public static final QName PROP_OAUTH1_TOKEN_SECRET = QName.createQName(NAMESPACE, "oauth1TokenSecret"); public static final QName PROP_OAUTH1_TOKEN_SECRET = QName.createQName(NAMESPACE, "oauth1TokenSecret");
public static final QName PROP_CHANNEL_USERNAME = QName.createQName(NAMESPACE, "channelUsername"); public static final QName PROP_CHANNEL_USERNAME = QName.createQName(NAMESPACE, "channelUsername");
public static final QName PROP_CHANNEL_PASSWORD = QName.createQName(NAMESPACE, "channelPassword"); public static final QName PROP_CHANNEL_PASSWORD = QName.createQName(NAMESPACE, "channelPassword");
public static final QName PROP_OAUTH2_TOKEN = QName.createQName(NAMESPACE, "oauth2Token");
public static final QName PROP_ASSET_ID = QName.createQName(NAMESPACE, "assetId");
public static final QName PROP_ASSET_URL = QName.createQName(NAMESPACE, "assetUrl");
// Publishing Connection Properties // Publishing Connection Properties

View File

@@ -20,6 +20,7 @@ package org.alfresco.repo.publishing.facebook;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -27,36 +28,33 @@ import org.alfresco.repo.publishing.AbstractChannelType;
import org.alfresco.repo.publishing.PublishingModel; import org.alfresco.repo.publishing.PublishingModel;
import org.alfresco.service.cmr.publishing.channels.Channel; import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.springframework.social.connect.Connection; import org.springframework.social.connect.Connection;
import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.oauth1.AuthorizedRequestToken; import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
import org.springframework.social.oauth2.GrantType; import org.springframework.social.oauth2.GrantType;
import org.springframework.social.oauth2.OAuth2Operations; import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.oauth2.OAuth2Parameters; import org.springframework.social.oauth2.OAuth2Parameters;
import org.springframework.social.twitter.api.Twitter;
public class FacebookChannelType extends AbstractChannelType public class FacebookChannelType extends AbstractChannelType
{ {
public final static String ID = "facebook"; public final static String ID = "facebook";
private NodeService nodeService; public final static String DEFAULT_REDIRECT_URI = "http://cognite.net";
private FacebookPublishingHelper publishingHelper;
public void setNodeService(NodeService nodeService) private FacebookPublishingHelper publishingHelper;
{ private String redirectUri = DEFAULT_REDIRECT_URI;
this.nodeService = nodeService;
}
public void setPublishingHelper(FacebookPublishingHelper facebookPublishingHelper) public void setPublishingHelper(FacebookPublishingHelper facebookPublishingHelper)
{ {
this.publishingHelper = facebookPublishingHelper; this.publishingHelper = facebookPublishingHelper;
} }
public void setRedirectUri(String redirectUri)
{
this.redirectUri = redirectUri;
}
@Override @Override
public boolean canPublish() public boolean canPublish()
{ {
@@ -119,12 +117,7 @@ public class FacebookChannelType extends AbstractChannelType
@Override @Override
public String getNodeUrl(NodeRef node) public String getNodeUrl(NodeRef node)
{ {
String url = null; return null;
if (node != null && nodeService.exists(node) && nodeService.hasAspect(node, FacebookPublishingModel.ASPECT_ASSET))
{
url = (String)nodeService.getProperty(node, FacebookPublishingModel.PROP_ASSET_URL);
}
return url;
} }
@Override @Override
@@ -137,8 +130,13 @@ public class FacebookChannelType extends AbstractChannelType
throw new IllegalArgumentException("Invalid channel type: " + channel.getChannelType().getId()); throw new IllegalArgumentException("Invalid channel type: " + channel.getChannelType().getId());
} }
NodeRef channelRef = channel.getNodeRef();
StringBuilder authStateBuilder = new StringBuilder(channelRef.getStoreRef().getProtocol()).append('.').append(
channelRef.getStoreRef().getIdentifier()).append('.').append(channelRef.getId());
OAuth2Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations(); OAuth2Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations();
return oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, new OAuth2Parameters(callbackUrl)); OAuth2Parameters params = new OAuth2Parameters(redirectUri,
"publish_stream,offline_access,user_photos,user_videos", authStateBuilder.toString(), null);
return oauthOperations.buildAuthorizeUrl(GrantType.IMPLICIT_GRANT, params);
} }
@Override @Override
@@ -146,23 +144,27 @@ public class FacebookChannelType extends AbstractChannelType
Map<String, String[]> callbackParams) Map<String, String[]> callbackParams)
{ {
boolean authorised = false; boolean authorised = false;
//FIXME: BJR: 20110708: Write this.
// String[] verifier = callbackParams.get("oauth_verifier"); String accessToken = null;
// if (verifier != null) if (callbackParams.containsKey("access_token"))
// { {
// OAuth2Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations(); //We have been given the access token directly.
// NodeRef channelNodeRef = channel.getNodeRef(); accessToken = callbackParams.get("access_token")[0];
// }
// Map<QName, Serializable> props = nodeService.getProperties(channelNodeRef); else if (callbackParams.containsKey("code"))
// String tokenValue = (String) props.get(PublishingModel.PROP_OAUTH1_TOKEN_VALUE); {
// String tokenSecret = (String) props.get(PublishingModel.PROP_OAUTH1_TOKEN_SECRET); //We have been passed an authorisation code that needs to be exchanged for a token
// OAuthToken token = new OAuthToken(tokenValue, tokenSecret); OAuth2Operations oauthOps = publishingHelper.getConnectionFactory().getOAuthOperations();
// OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(token, verifier[0]), null); AccessGrant grant = oauthOps.exchangeForAccess(callbackParams.get("code")[0], redirectUri, null);
// nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_VALUE, accessToken.getValue()); accessToken = grant.getAccessToken();
// nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_SECRET, accessToken.getSecret()); }
// if (accessToken != null)
// authorised = true; {
// } Map<QName,Serializable> channelProps = new HashMap<QName, Serializable>();
channelProps.put(PublishingModel.PROP_OAUTH2_TOKEN, accessToken);
getChannelService().updateChannel(channel, channelProps);
authorised = true;
}
return authorised; return authorised;
} }
} }

View File

@@ -52,13 +52,12 @@ public class FacebookPublishingHelper
if (nodeService.exists(channelNode) if (nodeService.exists(channelNode)
&& nodeService.hasAspect(channelNode, FacebookPublishingModel.ASPECT_DELIVERY_CHANNEL)) && nodeService.hasAspect(channelNode, FacebookPublishingModel.ASPECT_DELIVERY_CHANNEL))
{ {
String tokenValue = (String) nodeService.getProperty(channelNode, PublishingModel.PROP_OAUTH1_TOKEN_VALUE); String tokenValue = (String) nodeService.getProperty(channelNode, PublishingModel.PROP_OAUTH2_TOKEN);
String tokenSecret = (String) nodeService.getProperty(channelNode, PublishingModel.PROP_OAUTH1_TOKEN_SECRET);
Boolean danceComplete = (Boolean) nodeService.getProperty(channelNode, PublishingModel.PROP_AUTHORISATION_COMPLETE); Boolean danceComplete = (Boolean) nodeService.getProperty(channelNode, PublishingModel.PROP_AUTHORISATION_COMPLETE);
if (danceComplete) if (danceComplete)
{ {
AccessGrant token = new AccessGrant(" "); AccessGrant token = new AccessGrant(tokenValue);
connection = connectionFactory.createConnection(token); connection = connectionFactory.createConnection(token);
} }
} }

View File

@@ -33,8 +33,4 @@ public interface FacebookPublishingModel
public static final QName TYPE_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "DeliveryChannel"); public static final QName TYPE_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "DeliveryChannel");
public static final QName ASPECT_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "DeliveryChannelAspect"); public static final QName ASPECT_DELIVERY_CHANNEL = QName.createQName(NAMESPACE, "DeliveryChannelAspect");
public static final QName ASPECT_ASSET = QName.createQName(NAMESPACE, "AssetAspect");
public static final QName PROP_ASSET_ID = QName.createQName(NAMESPACE, "assetId");
public static final QName PROP_ASSET_URL = QName.createQName(NAMESPACE, "assetUrl");
} }

View File

@@ -29,7 +29,7 @@ import java.util.Date;
*/ */
public interface PublishingEvent public interface PublishingEvent
{ {
enum Status {SCHEDULED, IN_PROGRESS, CANCEL_REQUESTED, COMPLETE, FAILED} enum Status {SCHEDULED, IN_PROGRESS, CANCEL_REQUESTED, COMPLETED, FAILED}
String getId(); String getId();