Permalink
Browse files

Handle text conversion

  • Loading branch information...
1 parent e4532dd commit 5fd577429399d722aef92acb039ec8cc8ff9db7d @ColinHebert ColinHebert committed Aug 2, 2012
@@ -2,12 +2,15 @@
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.content.api.ContentCopyInterceptorRegistry;
+import org.sakaiproject.content.api.ContentCopyTextInterceptor;
import org.sakaiproject.content.api.ContentCopyUrlInterceptor;
import org.sakaiproject.shortenedurl.api.ShortenedUrlService;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -16,15 +19,25 @@
*
* @author Colin Hebert
*/
-public class ShortenedUrlInterceptor implements ContentCopyUrlInterceptor{
+public class ShortenedInterceptor implements ContentCopyUrlInterceptor, ContentCopyTextInterceptor {
private static Pattern PATH_PATTERN = Pattern.compile("/+x/+(.+)");
+ private Pattern shortenedUrlPattern;
private ShortenedUrlService shortenedUrlService;
private ServerConfigurationService scs;
private Collection<String> serverNames;
private ContentCopyInterceptorRegistry registry;
public void init() {
registry.registerUrlInterceptor(this);
+ registry.registerTextInterceptor(this);
+
+ StringBuilder servers = new StringBuilder();
+ for (Iterator<String> iterator = getServerNames().iterator(); iterator.hasNext(); ) {
+ servers.append(Pattern.quote(iterator.next()));
+ if (iterator.hasNext())
+ servers.append('|');
+ }
+ shortenedUrlPattern = Pattern.compile("(https?://)?(?:\" + servers + \")(?::\\d+)?/+x/+\\w+", Pattern.CASE_INSENSITIVE);
}
public boolean isUrlHandled(String url) {
@@ -68,6 +81,30 @@ public void setScs(ServerConfigurationService scs) {
return serverNames;
}
+ public String runTextInterceptor(String interceptedText, Map<String, String> replacements) {
+ Matcher matcher = shortenedUrlPattern.matcher(interceptedText);
+ StringBuffer sb = new StringBuffer(interceptedText.length());
+
+ while (matcher.find()) {
+ String matchedUrl = matcher.group(0);
+ if(matcher.group(1) == null)
+ matchedUrl = "http://"+matchedUrl;
+ matcher.appendReplacement(sb, updateTextShortenedUrl(matchedUrl, replacements));
+ }
+ matcher.appendTail(sb);
+ return sb.toString();
+ }
+
+ private String updateTextShortenedUrl(String shortenedUrl, Map<String, String> replacements){
+ String newUrl = convertUrl(shortenedUrl);
+
+ for(Map.Entry<String, String> replacement: replacements.entrySet()){
+ newUrl = newUrl.replace(replacement.getKey(), replacement.getValue());
+ }
+
+ return convertProcessedUrl(newUrl);
+ }
+
public void setRegistry(ContentCopyInterceptorRegistry registry) {
this.registry = registry;
}
@@ -95,7 +95,7 @@
</bean>
<bean id="org.sakaiproject.shortenedurl.impl.ShortenedUrlInterceptor"
- class="org.sakaiproject.shortenedurl.impl.ShortenedUrlInterceptor" init-method="init">
+ class="org.sakaiproject.shortenedurl.impl.ShortenedInterceptor" init-method="init">
<property name="registry" ref="org.sakaiproject.content.api.ContentCopyInterceptorRegistry" />
<property name="shortenedUrlService" ref="org.sakaiproject.shortenedurl.api.ShortenedUrlService" />
<property name="scs" ref="org.sakaiproject.component.api.ServerConfigurationService" />

0 comments on commit 5fd5774

Please sign in to comment.