diff --git a/source/java/org/alfresco/repo/publishing/AbstractOAuth1ChannelType.java b/source/java/org/alfresco/repo/publishing/AbstractOAuth1ChannelType.java new file mode 100644 index 0000000000..b64358851e --- /dev/null +++ b/source/java/org/alfresco/repo/publishing/AbstractOAuth1ChannelType.java @@ -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 . + */ +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 callbackHeaders, + Map callbackParams) + { + AuthStatus authorised = AuthStatus.UNAUTHORISED; + String[] verifier = callbackParams.get(getOAuthVerifierParamName()); + if (verifier != null) + { + OAuth1Operations oauthOperations = getOAuth1Operations(); + NodeRef channelNodeRef = channel.getNodeRef(); + + Map 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 newProps = new HashMap(); + 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"; + } +} diff --git a/source/java/org/alfresco/repo/publishing/flickr/FlickrChannelType.java b/source/java/org/alfresco/repo/publishing/flickr/FlickrChannelType.java index 1102019f3d..c749b0a26f 100644 --- a/source/java/org/alfresco/repo/publishing/flickr/FlickrChannelType.java +++ b/source/java/org/alfresco/repo/publishing/flickr/FlickrChannelType.java @@ -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 DEFAULT_SUPPORTED_MIME_TYPES = new TreeSet(); @@ -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 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 params = new LinkedMultiValueMap(); - 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 callbackHeaders, - Map callbackParams) - { - AuthStatus authorised = AuthStatus.UNAUTHORISED; - String[] verifier = callbackParams.get("oauth_verifier"); - if (verifier != null) - { - OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations(); - NodeRef channelNodeRef = channel.getNodeRef(); - Map 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 newProps = new HashMap(); - 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; - } } diff --git a/source/java/org/alfresco/repo/publishing/twitter/TwitterChannelType.java b/source/java/org/alfresco/repo/publishing/twitter/TwitterChannelType.java index 583bb203d3..9a15585059 100644 --- a/source/java/org/alfresco/repo/publishing/twitter/TwitterChannelType.java +++ b/source/java/org/alfresco/repo/publishing/twitter/TwitterChannelType.java @@ -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 properties) { + //NO-OP } @Override public void unpublish(NodeRef nodeToUnpublish, Map 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 callbackHeaders, - Map callbackParams) - { - AuthStatus authorised = AuthStatus.UNAUTHORISED; - String[] verifier = callbackParams.get("oauth_verifier"); - if (verifier != null) - { - OAuth1Operations oauthOperations = publishingHelper.getConnectionFactory().getOAuthOperations(); - NodeRef channelNodeRef = channel.getNodeRef(); - - Map 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 newProps = new HashMap(); - 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(); } }