Author: fmeschbe
Date: Tue Jun 16 20:56:06 2009
New Revision: 785394
URL: http://svn.apache.org/viewvc?rev=785394&view=rev
Log:
SLING-1008 Support configuring mappings without explicit port numbers for
well known port numbers (80 for http and 443 for https)
Added:
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
(with props)
Modified:
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
Modified:
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java?rev=785394&r1=785393&r2=785394&view=diff
==============================================================================
---
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
(original)
+++
incubator/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntry.java
Tue Jun 16 20:56:06 2009
@@ -39,13 +39,21 @@
*/
public class MapEntry implements Comparable<MapEntry> {
+ private static final Pattern[] URL_WITH_PORT_MATCH = {
+ Pattern.compile("http/([^/]+)(\\.[^\\d/]+)(/.*)?$"),
+ Pattern.compile("https/([^/]+)(\\.[^\\d/]+)(/.*)?$") };
+
+ private static final String[] URL_WITH_PORT_REPLACEMENT = {
+ "http/$1$2.80$3", "https/$1$2.443$3" };
+
private static final Pattern[] PATH_TO_URL_MATCH = {
Pattern.compile("http/([^/]+)\\.80(/.*)?$"),
Pattern.compile("https/([^/]+)\\.443(/.*)?$"),
- Pattern.compile("([^/]+)/([^/]+)\\.(\\d+)(/.*)?$") };
+ Pattern.compile("([^/]+)/([^/]+)\\.(\\d+)(/.*)?$"),
+ Pattern.compile("([^/]+)/([^/]+)(/.*)?$") };
private static final String[] PATH_TO_URL_REPLACEMENT = { "http://$1$2",
- "https://$1$2", "$1://$2:$3$4" };
+ "https://$1$2", "$1://$2:$3$4", "$1://$2$3" };
private final Pattern urlPattern;
@@ -86,6 +94,17 @@
return sb.toString();
}
+ public static String fixUriPath(String uriPath) {
+ for (int i = 0; i < URL_WITH_PORT_MATCH.length; i++) {
+ Matcher m = URL_WITH_PORT_MATCH[i].matcher(uriPath);
+ if (m.find()) {
+ return m.replaceAll(URL_WITH_PORT_REPLACEMENT[i]);
+ }
+ }
+
+ return uriPath;
+ }
+
public static URI toURI(String uriPath) {
for (int i = 0; i < PATH_TO_URL_MATCH.length; i++) {
Matcher m = PATH_TO_URL_MATCH[i].matcher(uriPath);
@@ -106,6 +125,10 @@
boolean trailingSlash) {
ValueMap props = resource.adaptTo(ValueMap.class);
if (props != null) {
+
+ // ensure the url contains a port number (if possible)
+ url = fixUriPath(url);
+
String redirect = props.get(
JcrResourceResolver2.PROP_REDIRECT_EXTERNAL, String.class);
if (redirect != null) {
@@ -141,7 +164,7 @@
endHook = "$";
url = url.substring(0, url.length()-1);
}
-
+
String[] internalRedirect = props.get(
JcrResourceResolver2.PROP_REDIRECT_INTERNAL, String[].class);
if (internalRedirect != null) {
Added:
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java?rev=785394&view=auto
==============================================================================
---
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
(added)
+++
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
Tue Jun 16 20:56:06 2009
@@ -0,0 +1,147 @@
+/*
+ * 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.sling.jcr.resource.internal.helper;
+
+import java.net.URI;
+
+import junit.framework.TestCase;
+
+public class MapEntryTest extends TestCase {
+
+ public void test_to_url_http_80() {
+ assertEqualUri("http://sling.apache.org", "http/sling.apache.org.80");
+ assertEqualUri("http://sling.apache.org/",
"http/sling.apache.org.80/");
+ assertEqualUri("http://sling.apache.org/site/index.html",
+ "http/sling.apache.org.80/site/index.html");
+ }
+
+ public void test_to_url_https_443() {
+ assertEqualUri("https://sling.apache.org",
"https/sling.apache.org.443");
+ assertEqualUri("https://sling.apache.org/",
+ "https/sling.apache.org.443/");
+ assertEqualUri("https://sling.apache.org/site/index.html",
+ "https/sling.apache.org.443/site/index.html");
+ }
+
+ public void test_to_url_any_999() {
+ // http with arbitrary port
+ assertEqualUri("http://sling.apache.org:123",
+ "http/sling.apache.org.123");
+ assertEqualUri("http://sling.apache.org:456/",
+ "http/sling.apache.org.456/");
+ assertEqualUri("http://sling.apache.org:456/site/index.html",
+ "http/sling.apache.org.456/site/index.html");
+
+ // https with arbitrary port
+ assertEqualUri("https://sling.apache.org:987",
+ "https/sling.apache.org.987");
+ assertEqualUri("https://sling.apache.org:654/",
+ "https/sling.apache.org.654/");
+ assertEqualUri("https://sling.apache.org:321/site/index.html",
+ "https/sling.apache.org.321/site/index.html");
+
+ // any scheme with arbitrary port
+ assertEqualUri("gurk://sling.apache.org:987",
+ "gurk/sling.apache.org.987");
+ assertEqualUri("gurk://sling.apache.org:654/",
+ "gurk/sling.apache.org.654/");
+ assertEqualUri("gurk://sling.apache.org:321/site/index.html",
+ "gurk/sling.apache.org.321/site/index.html");
+ }
+
+ public void test_to_url_any() {
+ // http without port
+ assertEqualUri("http://sling.apache.org", "http/sling.apache.org");
+ assertEqualUri("http://sling.apache.org/", "http/sling.apache.org/");
+ assertEqualUri("http://sling.apache.org/site/index.html",
+ "http/sling.apache.org/site/index.html");
+
+ // https without port
+ assertEqualUri("https://sling.apache.org", "https/sling.apache.org");
+ assertEqualUri("https://sling.apache.org/", "https/sling.apache.org/");
+ assertEqualUri("https://sling.apache.org/site/index.html",
+ "https/sling.apache.org/site/index.html");
+
+ // any scheme without port
+ assertEqualUri("gurk://sling.apache.org", "gurk/sling.apache.org");
+ assertEqualUri("gurk://sling.apache.org/", "gurk/sling.apache.org/");
+ assertEqualUri("gurk://sling.apache.org/site/index.html",
+ "gurk/sling.apache.org/site/index.html");
+ }
+
+ public void test_fixUriPath() {
+ // http without port
+ assertEqualUriPath("http/sling.apache.org.80",
"http/sling.apache.org");
+ assertEqualUriPath("http/sling.apache.org.80/",
+ "http/sling.apache.org/");
+ assertEqualUriPath("http/sling.apache.org.80/site/index.html",
+ "http/sling.apache.org/site/index.html");
+
+ // http with port
+ assertEqualUriPath("http/sling.apache.org.80",
+ "http/sling.apache.org.80");
+ assertEqualUriPath("http/sling.apache.org.80/",
+ "http/sling.apache.org.80/");
+ assertEqualUriPath("http/sling.apache.org.80/site/index.html",
+ "http/sling.apache.org.80/site/index.html");
+
+ // https without port
+ assertEqualUriPath("https/sling.apache.org.443",
+ "https/sling.apache.org");
+ assertEqualUriPath("https/sling.apache.org.443/",
+ "https/sling.apache.org/");
+ assertEqualUriPath("https/sling.apache.org.443/site/index.html",
+ "https/sling.apache.org/site/index.html");
+
+ // https with port
+ assertEqualUriPath("https/sling.apache.org.443",
+ "https/sling.apache.org.443");
+ assertEqualUriPath("https/sling.apache.org.443/",
+ "https/sling.apache.org.443/");
+ assertEqualUriPath("https/sling.apache.org.443/site/index.html",
+ "https/sling.apache.org.443/site/index.html");
+
+ // anything without port
+ assertEqualUriPath("gurk/sling.apache.org", "gurk/sling.apache.org");
+ assertEqualUriPath("gurk/sling.apache.org/", "gurk/sling.apache.org/");
+ assertEqualUriPath("gurk/sling.apache.org/site/index.html",
+ "gurk/sling.apache.org/site/index.html");
+
+ // http with port
+ assertEqualUriPath("gurk/sling.apache.org.123",
+ "gurk/sling.apache.org.123");
+ assertEqualUriPath("gurk/sling.apache.org.456/",
+ "gurk/sling.apache.org.456/");
+ assertEqualUriPath("gurk/sling.apache.org.789/site/index.html",
+ "gurk/sling.apache.org.789/site/index.html");
+
+ }
+
+ private void assertEqualUri(String expected, String uriPath) {
+ URI uri = MapEntry.toURI(uriPath);
+ assertNotNull("Failed converting " + uriPath, uri);
+ assertEquals(expected, uri.toString());
+ }
+
+ private void assertEqualUriPath(String expected, String uriPath) {
+ String fixed = MapEntry.fixUriPath(uriPath);
+ assertNotNull(fixed);
+ assertEquals(expected, fixed);
+ }
+}
Propchange:
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/MapEntryTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev Url