Author: andreas
Date: Mon Oct 29 04:05:54 2007
New Revision: 589580
URL: http://svn.apache.org/viewvc?rev=589580&view=rev
Log:
Fixed relative URl generation, added test for relative URLs with OutgoingLinkRewriter, see bug 43681.
Added:
lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/OutgoingLinkRewriterTest.java
Modified:
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/OutgoingLinkRewriter.java
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/OutgoingLinkRewriter.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/OutgoingLinkRewriter.java?rev=589580&r1=589579&r2=589580&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/OutgoingLinkRewriter.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/OutgoingLinkRewriter.java Mon Oct 29 04:05:54 2007
@@ -98,12 +98,12 @@
this.policyManager = accessController.getPolicyManager();
}
}
-
+
Publication[] pubs = this.factory.getPublications();
for (int i = 0; i < pubs.length; i++) {
this.publicationCache.put(pubs[i].getId(), pubs[i]);
}
-
+
} catch (final Exception e) {
throw new RuntimeException(e);
} finally {
@@ -182,17 +182,16 @@
String normalizedUrl;
if (url.indexOf("..") > -1) {
normalizedUrl = new URI(url).normalize().toString();
- }
- else {
+ } else {
normalizedUrl = url;
}
return normalizedUrl;
}
private String requestUrl;
-
+
private Map pubId2areaList = new HashMap();
-
+
/**
* Checks if a publication has an area by using a cache for performance reasons.
* @param pub The publication.
@@ -235,27 +234,44 @@
}
protected String getRelativeUrlTo(String webappUrl) {
- List sourceSteps = toList(this.requestUrl);
- List targetSteps = toList(webappUrl);
-
- while (!sourceSteps.isEmpty() && !targetSteps.isEmpty()
- && sourceSteps.get(0).equals(targetSteps.get(0))) {
- sourceSteps.remove(0);
- targetSteps.remove(0);
+ String relativeUrl;
+ if (this.requestUrl.equals(webappUrl)) {
+ relativeUrl = webappUrl.substring(webappUrl.lastIndexOf("/") + 1);
}
+ else {
+ List sourceSteps = toList(this.requestUrl);
+ List targetSteps = toList(webappUrl);
- String upDots = "";
- if (sourceSteps.size() > 1) {
- String[] upDotsArray = new String[sourceSteps.size() - 1];
- Arrays.fill(upDotsArray, "..");
- upDots = StringUtil.join(upDotsArray, "/") + "/";
- }
+ while (!sourceSteps.isEmpty() && !targetSteps.isEmpty()
+ && sourceSteps.get(0).equals(targetSteps.get(0))) {
+ sourceSteps.remove(0);
+ targetSteps.remove(0);
+ }
- String[] targetArray = (String[]) targetSteps.toArray(new String[targetSteps.size()]);
- String targetPath = StringUtil.join(targetArray, "/");
+ String prefix = "";
+ if (targetSteps.isEmpty()) {
+ prefix = generateUpDots(sourceSteps.size());
+ }
+ else if (sourceSteps.isEmpty()) {
+ prefix = "./";
+ }
+ else if (sourceSteps.size() > 1) {
+ prefix = generateUpDots(sourceSteps.size()) + "/";
+ }
- String relativeUrl = upDots + targetPath;
+ String[] targetArray = (String[]) targetSteps.toArray(new String[targetSteps.size()]);
+ String targetPath = StringUtil.join(targetArray, "/");
+ relativeUrl = prefix + targetPath;
+ }
return relativeUrl;
+ }
+
+ protected String generateUpDots(int length) {
+ String upDots;
+ String[] upDotsArray = new String[length];
+ Arrays.fill(upDotsArray, "..");
+ upDots = StringUtil.join(upDotsArray, "/");
+ return upDots;
}
protected List toList(String url) {
Added: lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/OutgoingLinkRewriterTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/OutgoingLinkRewriterTest.java?rev=589580&view=auto
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/OutgoingLinkRewriterTest.java (added)
+++ lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/OutgoingLinkRewriterTest.java Mon Oct 29 04:05:54 2007
@@ -0,0 +1,38 @@
+/*
+ * 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;
+import org.apache.lenya.cms.repository.Session;
+
+public class OutgoingLinkRewriterTest extends AbstractAccessControlTest {
+
+ public void testRelativeUrls() throws Exception {
+ Session session = login("lenya");
+ String url = "/aaa/bbb/ccc";
+ OutgoingLinkRewriter rewriter = new OutgoingLinkRewriter(getManager(), session, url, false, false, true);
+
+ assertEquals(rewriter.rewrite("/aaa/bbb/foo"), "foo");
+ assertEquals(rewriter.rewrite("/aaa/bbb"), "..");
+ assertEquals(rewriter.rewrite("/aaa/bbb/ccc/ddd"), "./ddd");
+ assertEquals(rewriter.rewrite("/aaa/foo"), "../../foo");
+ assertEquals(rewriter.rewrite("/aaa/foo/bar"), "../../foo/bar");
+ assertEquals(rewriter.rewrite("/foo/bar"), "../../../foo/bar");
+ }
+
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.