diff --git a/config/alfresco/repository.properties b/config/alfresco/repository.properties index 38227e8776..07c3403825 100644 --- a/config/alfresco/repository.properties +++ b/config/alfresco/repository.properties @@ -402,7 +402,6 @@ spaces.extension_webscripts.childname=cm:extensionwebscripts spaces.models.childname=app:models spaces.workflow.definitions.childname=app:workflow_defs - # ADM VersionStore Configuration version.store.deprecated.lightWeightVersionStore=workspace://lightWeightVersionStore version.store.version2Store=workspace://version2Store @@ -665,4 +664,11 @@ encryption.keystore.password.solr=TxHTtOnrwQ # # Web Publishing Properties # -publishing.default.environment=live \ No newline at end of file +publishing.default.environment=live + +# +# URL Shortening Properties +# +urlshortening.bitly.username=brianalfresco +urlshortening.bitly.api.key=R_ca15c6c89e9b25ccd170bafd209a0d4f +urlshortening.bitly.url.length=20 diff --git a/config/alfresco/web-publishing-context.xml b/config/alfresco/web-publishing-context.xml index e494746980..8a10778ca0 100644 --- a/config/alfresco/web-publishing-context.xml +++ b/config/alfresco/web-publishing-context.xml @@ -86,6 +86,7 @@ + @@ -103,4 +104,10 @@ + + + + + + diff --git a/source/java/org/alfresco/repo/publishing/PublishingEventProcessor.java b/source/java/org/alfresco/repo/publishing/PublishingEventProcessor.java index f7f7702556..551acd7b36 100644 --- a/source/java/org/alfresco/repo/publishing/PublishingEventProcessor.java +++ b/source/java/org/alfresco/repo/publishing/PublishingEventProcessor.java @@ -43,6 +43,7 @@ import org.alfresco.service.cmr.publishing.channels.ChannelService; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.urlshortening.UrlShortener; import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.util.GUID; @@ -60,6 +61,7 @@ public class PublishingEventProcessor private ChannelService channelService; private NodeService nodeService; private BehaviourFilter behaviourFilter; + private UrlShortener urlShortener; public void processEventNode(NodeRef eventNode) { @@ -100,7 +102,7 @@ public class PublishingEventProcessor String nodeUrl = publishChannel.getChannelType().getNodeUrl(node); if(nodeUrl != null) { - message += nodeUrl; + message += urlShortener.shortenUrl(nodeUrl); } } Set channels = update.getChannelNames(); @@ -234,7 +236,6 @@ public class PublishingEventProcessor } } - private void addAspects(NodeRef publishedNode, Collection aspects) { Set currentAspects = nodeService.getAspects(publishedNode); @@ -310,4 +311,12 @@ public class PublishingEventProcessor { this.behaviourFilter = behaviourFilter; } -} + + /** + * @param urlShortener the urlShortener to set + */ + public void setUrlShortener(UrlShortener urlShortener) + { + this.urlShortener = urlShortener; + } +} \ No newline at end of file diff --git a/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerImpl.java b/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerImpl.java new file mode 100644 index 0000000000..f5e321af74 --- /dev/null +++ b/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerImpl.java @@ -0,0 +1,111 @@ +package org.alfresco.repo.urlshortening; + +import java.util.ArrayList; +import java.util.List; + +import org.alfresco.service.cmr.urlshortening.UrlShortener; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class BitlyUrlShortenerImpl implements UrlShortener +{ + private static final Log log = LogFactory.getLog(BitlyUrlShortenerImpl.class); + + private int urlLength; + private String username; + private String apiKey = "R_ca15c6c89e9b25ccd170bafd209a0d4f"; + private HttpClient httpClient; + + public BitlyUrlShortenerImpl() + { + httpClient = new HttpClient(); + httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); + HostConfiguration hostConfiguration = new HostConfiguration(); + hostConfiguration.setHost("api-ssl.bitly.com", 443, Protocol.getProtocol("https")); + httpClient.setHostConfiguration(hostConfiguration); + } + + @Override + public String shortenUrl(String longUrl) + { + if (log.isDebugEnabled()) + { + log.debug("Shortening URL: " + longUrl); + } + String shortUrl = longUrl; + if (longUrl.length() > urlLength) + { + GetMethod getMethod = new GetMethod(); + getMethod.setPath("/v3/shorten"); + + List args = new ArrayList(); + args.add(new NameValuePair("login", username)); + args.add(new NameValuePair("apiKey", apiKey)); + args.add(new NameValuePair("longUrl", longUrl)); + args.add(new NameValuePair("format", "txt")); + getMethod.setQueryString(args.toArray(new NameValuePair[args.size()])); + + try + { + int resultCode = httpClient.executeMethod(getMethod); + if (resultCode == 200) + { + shortUrl = getMethod.getResponseBodyAsString(); + } + else + { + log.warn("Failed to shorten URL " + longUrl + " - response code == " + resultCode); + log.warn(getMethod.getResponseBodyAsString()); + } + } + catch (Exception ex) + { + log.error("Failed to shorten URL " + longUrl, ex); + } + if (log.isDebugEnabled()) + { + log.debug("URL " + longUrl + " has been shortened to " + shortUrl); + } + } + return shortUrl.trim(); + } + + /** + * {@inheritDoc} + */ + @Override + public int getUrlLength() + { + return urlLength; + } + + /** + * @param urlLength the urlLength to set + */ + public void setUrlLength(int urlLength) + { + this.urlLength = urlLength; + } + + /** + * @param username the username to set + */ + public void setUsername(String username) + { + this.username = username; + } + + /** + * @param apiKey the apiKey to set + */ + public void setApiKey(String apiKey) + { + this.apiKey = apiKey; + } +} \ No newline at end of file diff --git a/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerTest.java b/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerTest.java new file mode 100644 index 0000000000..98a5759a25 --- /dev/null +++ b/source/java/org/alfresco/repo/urlshortening/BitlyUrlShortenerTest.java @@ -0,0 +1,18 @@ +package org.alfresco.repo.urlshortening; + +import junit.framework.TestCase; + +public class BitlyUrlShortenerTest extends TestCase +{ + private BitlyUrlShortenerImpl shortener = new BitlyUrlShortenerImpl(); + + public void testShorten() + { + String url = "http://www.alfresco.com/"; + String shortUrl = shortener.shortenUrl(url); + assertNotNull(shortUrl); + assertFalse(shortUrl.isEmpty()); + assertFalse(url.equals(shortUrl)); + assertTrue(shortUrl.length()<=20); + } +} diff --git a/source/java/org/alfresco/service/cmr/urlshortening/UrlShortener.java b/source/java/org/alfresco/service/cmr/urlshortening/UrlShortener.java new file mode 100644 index 0000000000..ffb1254c71 --- /dev/null +++ b/source/java/org/alfresco/service/cmr/urlshortening/UrlShortener.java @@ -0,0 +1,8 @@ +package org.alfresco.service.cmr.urlshortening; + +public interface UrlShortener +{ + String shortenUrl(String longUrl); + + int getUrlLength(); +}