You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2009/01/26 12:29:00 UTC
svn commit: r737673 - in /lenya/trunk/src/modules-core/linking/java:
src/org/apache/lenya/cms/linking/ test/org/apache/lenya/cms/linking/
Author: andreas
Date: Mon Jan 26 11:28:58 2009
New Revision: 737673
URL: http://svn.apache.org/viewvc?rev=737673&view=rev
Log:
Adding relative-to-absolute link rewriter and chain link rewriter.
Added:
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/ChainLinkRewriter.java
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriter.java
lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriterTest.java
Added: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/ChainLinkRewriter.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/ChainLinkRewriter.java?rev=737673&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/ChainLinkRewriter.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/ChainLinkRewriter.java Mon Jan 26 11:28:58 2009
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.lenya.util.Assert;
+
+/**
+ * A chain of link rewriters. The output of one rewriter serves as the input of the next one. If a
+ * rewriter doesn't match a link, it is skipped.
+ */
+public class ChainLinkRewriter implements LinkRewriter {
+
+ private List rewriters = new ArrayList();
+
+ /**
+ * Adds a rewriter to the end of the chain.
+ * @param rewriter The rewriter.
+ */
+ public void add(LinkRewriter rewriter) {
+ Assert.notNull("rewriter", rewriter);
+ this.rewriters.add(rewriter);
+ }
+
+ public boolean matches(String url) {
+ return true;
+ }
+
+ public String rewrite(String url) {
+ String link = url;
+ for (Iterator i = this.rewriters.iterator(); i.hasNext();) {
+ LinkRewriter rewriter = (LinkRewriter) i.next();
+ if (rewriter.matches(link)) {
+ link = rewriter.rewrite(link);
+ }
+ }
+ return link;
+ }
+
+}
Added: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriter.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriter.java?rev=737673&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriter.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriter.java Mon Jan 26 11:28:58 2009
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.lenya.util.Assert;
+
+public class RelativeToAbsoluteLinkRewriter implements LinkRewriter {
+
+ private String sourceUri;
+
+ protected static final String[] PROTOCOLS = { "/", "http:", "https:", "ftp:", "ftps:",
+ "mailto:", "file:" };
+
+ public RelativeToAbsoluteLinkRewriter(String sourceUri) {
+ Assert.notNull("source URI", sourceUri);
+ this.sourceUri = sourceUri;
+ }
+
+ public boolean matches(String url) {
+ Assert.notNull("url", url);
+ for (int i = 0; i < PROTOCOLS.length; i++) {
+ if (url.startsWith(PROTOCOLS[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public String rewrite(final String url) {
+ try {
+ final int lastSlashIndex = this.sourceUri.lastIndexOf('/');
+ final String prefix = lastSlashIndex < 0 ? "" : this.sourceUri.substring(0,
+ lastSlashIndex + 1);
+ final String newUrl = prefix + url;
+ final String normalizedUrl = new URI(newUrl).normalize().toString();
+ return normalizedUrl;
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Added: lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriterTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriterTest.java?rev=737673&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriterTest.java (added)
+++ lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/RelativeToAbsoluteLinkRewriterTest.java Mon Jan 26 11:28:58 2009
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import org.apache.lenya.ac.impl.AbstractAccessControlTest;
+
+/**
+ * Test case for relative-to-absolute link rewriting.
+ */
+public class RelativeToAbsoluteLinkRewriterTest extends AbstractAccessControlTest {
+
+ private static final String SOURCE_URI = "/foo/bar";
+
+ /**
+ * Test case for relative-to-absolute link rewriting.
+ */
+ public void testLinks() {
+ RelativeToAbsoluteLinkRewriter rewriter = new RelativeToAbsoluteLinkRewriter(SOURCE_URI);
+ assertEquals(rewriter.rewrite("baz"), "/foo/baz");
+ assertEquals(rewriter.rewrite("bar/baz"), "/foo/bar/baz");
+ assertEquals(rewriter.rewrite("../baz"), "/baz");
+ assertEquals(rewriter.rewrite(".."), "/");
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org