Publishing:

- Added reauthorisation API for publishing channels
- Refactored OAuth1 channels to share common functionality (Twitter and Flickr)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29362 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Brian Remmington
2011-07-26 15:42:57 +00:00
parent 56177d3d3a
commit d5fe95520e
3 changed files with 113 additions and 121 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.publishing;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.springframework.social.oauth1.AuthorizedRequestToken;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
public abstract class AbstractOAuth1ChannelType extends AbstractChannelType
{
private NodeService nodeService;
public final void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
protected NodeService getNodeService()
{
return nodeService;
}
@Override
public String getAuthorisationUrl(Channel channel, String callbackUrl)
{
ParameterCheck.mandatory("channel", channel);
ParameterCheck.mandatory("callbackUrl", callbackUrl);
if (!getId().equals(channel.getChannelType().getId()))
{
throw new IllegalArgumentException("Invalid channel type: " + channel.getChannelType().getId());
}
OAuth1Operations oauthOperations = getOAuth1Operations();
OAuthToken requestToken = oauthOperations.fetchRequestToken(callbackUrl, null);
NodeRef channelNodeRef = channel.getNodeRef();
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_SECRET, requestToken.getSecret());
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_VALUE, requestToken.getValue());
return oauthOperations.buildAuthorizeUrl(requestToken.getValue(), OAuth1Parameters.NONE);
}
@Override
protected AuthStatus internalAcceptAuthorisation(Channel channel, Map<String, String[]> callbackHeaders,
Map<String, String[]> callbackParams)
{
AuthStatus authorised = AuthStatus.UNAUTHORISED;
String[] verifier = callbackParams.get(getOAuthVerifierParamName());
if (verifier != null)
{
OAuth1Operations oauthOperations = getOAuth1Operations();
NodeRef channelNodeRef = channel.getNodeRef();
Map<QName, Serializable> currentProps = nodeService.getProperties(channelNodeRef);
String tokenValue = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_VALUE);
String tokenSecret = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_SECRET);
OAuthToken token = new OAuthToken(tokenValue, tokenSecret);
OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(token, verifier[0]), null);
Map<QName, Serializable> newProps = new HashMap<QName, Serializable>();
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_VALUE, accessToken.getValue());
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_SECRET, accessToken.getSecret());
getChannelService().updateChannel(channel, newProps);
authorised = AuthStatus.AUTHORISED;
}
return authorised;
}
protected abstract OAuth1Operations getOAuth1Operations();
protected String getOAuthVerifierParamName()
{
return "oauth_verifier";
}
}

View File

@@ -20,13 +20,12 @@ package org.alfresco.repo.publishing.flickr;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.publishing.AbstractChannelType;
import org.alfresco.repo.publishing.AbstractOAuth1ChannelType;
import org.alfresco.repo.publishing.PublishingModel;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService;
@@ -34,15 +33,9 @@ import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.springframework.social.oauth1.AuthorizedRequestToken;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
public class FlickrChannelType extends AbstractChannelType
public class FlickrChannelType extends AbstractOAuth1ChannelType
{
public final static String ID = "flickr";
private final static Set<String> DEFAULT_SUPPORTED_MIME_TYPES = new TreeSet<String>();
@@ -54,16 +47,10 @@ public class FlickrChannelType extends AbstractChannelType
DEFAULT_SUPPORTED_MIME_TYPES.add(MimetypeMap.MIMETYPE_IMAGE_PNG);
}
private NodeService nodeService;
private FlickrPublishingHelper publishingHelper;
private ActionService actionService;
private Set<String> supportedMimeTypes = Collections.unmodifiableSet(DEFAULT_SUPPORTED_MIME_TYPES);
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setPublishingHelper(FlickrPublishingHelper flickrPublishingHelper)
{
this.publishingHelper = flickrPublishingHelper;
@@ -143,6 +130,7 @@ public class FlickrChannelType extends AbstractChannelType
public String getNodeUrl(NodeRef node)
{
String url = null;
NodeService nodeService = getNodeService();
if (node != null && nodeService.exists(node) && nodeService.hasAspect(node, FlickrPublishingModel.ASPECT_ASSET))
{
url = (String)nodeService.getProperty(node, PublishingModel.PROP_ASSET_URL);
@@ -151,50 +139,9 @@ public class FlickrChannelType extends AbstractChannelType
}
@Override
public String getAuthorisationUrl(Channel channel, String callbackUrl)
protected OAuth1Operations getOAuth1Operations()
{
ParameterCheck.mandatory("channel", channel);
ParameterCheck.mandatory("callbackUrl", callbackUrl);
if (!ID.equals(channel.getChannelType().getId()))
{
throw new IllegalArgumentException("Invalid channel type: " + channel.getChannelType().getId());
}
OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations();
OAuthToken requestToken = oauthOperations.fetchRequestToken(callbackUrl, null);
NodeRef channelNodeRef = channel.getNodeRef();
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_SECRET, requestToken.getSecret());
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_VALUE, requestToken.getValue());
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("perms", "delete");
OAuth1Parameters oauthParams = new OAuth1Parameters(callbackUrl, params);
return oauthOperations.buildAuthorizeUrl(requestToken.getValue(), oauthParams);
return publishingHelper.getConnectionFactory().getOAuthOperations();
}
@Override
protected AuthStatus internalAcceptAuthorisation(Channel channel, Map<String, String[]> callbackHeaders,
Map<String, String[]> callbackParams)
{
AuthStatus authorised = AuthStatus.UNAUTHORISED;
String[] verifier = callbackParams.get("oauth_verifier");
if (verifier != null)
{
OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations();
NodeRef channelNodeRef = channel.getNodeRef();
Map<QName, Serializable> currentProps = nodeService.getProperties(channelNodeRef);
String tokenValue = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_VALUE);
String tokenSecret = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_SECRET);
OAuthToken token = new OAuthToken(tokenValue, tokenSecret);
OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(token, verifier[0]), null);
Map<QName, Serializable> newProps = new HashMap<QName, Serializable>();
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_VALUE, accessToken.getValue());
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_SECRET, accessToken.getSecret());
getChannelService().updateChannel(channel, newProps);
authorised = AuthStatus.AUTHORISED;
}
return authorised;
}
}

View File

@@ -20,35 +20,22 @@ package org.alfresco.repo.publishing.twitter;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.alfresco.repo.publishing.AbstractChannelType;
import org.alfresco.repo.publishing.PublishingModel;
import org.alfresco.repo.publishing.AbstractOAuth1ChannelType;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.springframework.social.connect.Connection;
import org.springframework.social.oauth1.AuthorizedRequestToken;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
import org.springframework.social.twitter.api.Twitter;
public class TwitterChannelType extends AbstractChannelType
public class TwitterChannelType extends AbstractOAuth1ChannelType
{
public final static String ID = "twitter";
private NodeService nodeService;
private TwitterPublishingHelper publishingHelper;
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setPublishingHelper(TwitterPublishingHelper twitterPublishingHelper)
{
this.publishingHelper = twitterPublishingHelper;
@@ -99,11 +86,13 @@ public class TwitterChannelType extends AbstractChannelType
@Override
public void publish(NodeRef nodeToPublish, Map<QName, Serializable> properties)
{
//NO-OP
}
@Override
public void unpublish(NodeRef nodeToUnpublish, Map<QName, Serializable> properties)
{
//NO-OP
}
@Override
@@ -116,57 +105,12 @@ public class TwitterChannelType extends AbstractChannelType
@Override
public String getNodeUrl(NodeRef node)
{
String url = null;
if (node != null && nodeService.exists(node) && nodeService.hasAspect(node, TwitterPublishingModel.ASPECT_ASSET))
{
url = (String)nodeService.getProperty(node, TwitterPublishingModel.PROP_ASSET_URL);
}
return url;
throw new UnsupportedOperationException();
}
@Override
public String getAuthorisationUrl(Channel channel, String callbackUrl)
protected OAuth1Operations getOAuth1Operations()
{
ParameterCheck.mandatory("channel", channel);
ParameterCheck.mandatory("callbackUrl", callbackUrl);
if (!ID.equals(channel.getChannelType().getId()))
{
throw new IllegalArgumentException("Invalid channel type: " + channel.getChannelType().getId());
}
OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations();
OAuthToken requestToken = oauthOperations.fetchRequestToken(callbackUrl, null);
NodeRef channelNodeRef = channel.getNodeRef();
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_SECRET, requestToken.getSecret());
nodeService.setProperty(channelNodeRef, PublishingModel.PROP_OAUTH1_TOKEN_VALUE, requestToken.getValue());
return oauthOperations.buildAuthorizeUrl(requestToken.getValue(), OAuth1Parameters.NONE);
}
@Override
protected AuthStatus internalAcceptAuthorisation(Channel channel, Map<String, String[]> callbackHeaders,
Map<String, String[]> callbackParams)
{
AuthStatus authorised = AuthStatus.UNAUTHORISED;
String[] verifier = callbackParams.get("oauth_verifier");
if (verifier != null)
{
OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations();
NodeRef channelNodeRef = channel.getNodeRef();
Map<QName, Serializable> currentProps = nodeService.getProperties(channelNodeRef);
String tokenValue = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_VALUE);
String tokenSecret = (String) currentProps.get(PublishingModel.PROP_OAUTH1_TOKEN_SECRET);
OAuthToken token = new OAuthToken(tokenValue, tokenSecret);
OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(token, verifier[0]), null);
Map<QName, Serializable> newProps = new HashMap<QName, Serializable>();
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_VALUE, accessToken.getValue());
newProps.put(PublishingModel.PROP_OAUTH1_TOKEN_SECRET, accessToken.getSecret());
getChannelService().updateChannel(channel, newProps);
authorised = AuthStatus.AUTHORISED;
}
return authorised;
return publishingHelper.getConnectionFactory().getOAuthOperations();
}
}