From 798ed97aef9b87e6ca398429c5f89301e96ff6a4 Mon Sep 17 00:00:00 2001 From: Britt Park Date: Fri, 22 Sep 2006 01:56:38 +0000 Subject: [PATCH] Sandbox store to dns name mangler. It's hooked into the website creation wizard. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3880 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../web/bean/wcm/CreateWebsiteWizard.java | 13 ++- .../alfresco/web/bean/wcm/DNSNameMangler.java | 89 +++++++++++++++++++ .../web/bean/wcm/DNSNameManglerTest.java | 57 ++++++++++++ 3 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 source/java/org/alfresco/web/bean/wcm/DNSNameMangler.java create mode 100644 source/java/org/alfresco/web/bean/wcm/DNSNameManglerTest.java diff --git a/source/java/org/alfresco/web/bean/wcm/CreateWebsiteWizard.java b/source/java/org/alfresco/web/bean/wcm/CreateWebsiteWizard.java index d3f34e5b77..28d0b76c90 100644 --- a/source/java/org/alfresco/web/bean/wcm/CreateWebsiteWizard.java +++ b/source/java/org/alfresco/web/bean/wcm/CreateWebsiteWizard.java @@ -279,8 +279,7 @@ public class CreateWebsiteWizard extends BaseWizardBean new PropertyValue(DataTypeDefinition.TEXT, null)); // tag the store with the DNS name property - tagStoreDNSPath(stagingStore); - + tagStoreDNSPath(stagingStore, name, "staging"); // create the 'preview' store for the website String previewStore = AVMConstants.buildAVMStagingPreviewStoreName(name); @@ -300,7 +299,7 @@ public class CreateWebsiteWizard extends BaseWizardBean new PropertyValue(DataTypeDefinition.TEXT, null)); // tag the store with the DNS name property - tagStoreDNSPath(previewStore); + tagStoreDNSPath(previewStore, name, "preview"); // tag all related stores to indicate that they are part of a single sandbox @@ -360,7 +359,7 @@ public class CreateWebsiteWizard extends BaseWizardBean new PropertyValue(DataTypeDefinition.TEXT, null)); // tag the store with the DNS name property - tagStoreDNSPath(userStore); + tagStoreDNSPath(userStore, name, username, "main"); // create the user 'preview' store @@ -385,7 +384,7 @@ public class CreateWebsiteWizard extends BaseWizardBean new PropertyValue(DataTypeDefinition.TEXT, null)); // tag the store with the DNS name property - tagStoreDNSPath(previewStore); + tagStoreDNSPath(previewStore, name, username, "preview"); // tag all related stores to indicate that they are part of a single sandbox @@ -402,11 +401,11 @@ public class CreateWebsiteWizard extends BaseWizardBean * * @param store Name of the store to tag */ - private void tagStoreDNSPath(String store) + private void tagStoreDNSPath(String store, String... components) { String path = store + ":/" + AVMConstants.DIR_APPBASE + '/' + AVMConstants.DIR_WEBAPPS; // TODO: DNS name mangle the property name - can only contain value DNS characters! - String dnsProp = AVMConstants.PROP_DNS + store; + String dnsProp = AVMConstants.PROP_DNS + DNSNameMangler.MakeDNSName(components); this.avmService.setStoreProperty(store, QName.createQName(null, dnsProp), new PropertyValue(DataTypeDefinition.TEXT, path)); } diff --git a/source/java/org/alfresco/web/bean/wcm/DNSNameMangler.java b/source/java/org/alfresco/web/bean/wcm/DNSNameMangler.java new file mode 100644 index 0000000000..eb7b7d9cff --- /dev/null +++ b/source/java/org/alfresco/web/bean/wcm/DNSNameMangler.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2006 Alfresco, Inc. + * + * Licensed under the Mozilla Public License version 1.1 + * with a permitted attribution clause. You may obtain a + * copy of the License at + * + * http://www.alfresco.org/legal/license.txt + * + * 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.alfresco.web.bean.wcm; + +import java.util.regex.Pattern; + +import org.alfresco.util.GUID; + +/** + * Utility to convert sandbox store names into DNS save names. + * @author britt + */ +class DNSNameMangler +{ + // Component Separator. + private static final String SEPARATOR = "--"; + + // Regular expressions. + private static final Pattern RX_DNS_LEGAL = + Pattern.compile("^[a-z0-9][a-z0-9-]{0,57}[a-z0-9]$"); + private static final Pattern RX_ILLEGAL_CHARS = + Pattern.compile("[^a-z0-9]"); + private static final Pattern RX_HYPHENS = + Pattern.compile("\\-+"); + private static final Pattern RX_LEADING_HYPHEN = + Pattern.compile("^\\-"); + private static final Pattern RX_TRAILING_HYPHEN = + Pattern.compile("\\-$"); + + /** + * Make a DNS readable name related to the components passed in. + * @param components The Strings from which to synthesize the DNS name. + * @return A Valid DNS name. + */ + static String MakeDNSName(String... components) + { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < components.length - 1; i++) + { + builder.append(MangleOne(components[i])); + builder.append(SEPARATOR); + } + builder.append(MangleOne(components[components.length - 1])); + String result = builder.toString(); + if (RX_DNS_LEGAL.matcher(result).matches()) + { + return result; + } + // Otherwise more drastic measures are needed. + result = components[0] + "--" + GUID.generate(); + result = MangleOne(result); + if (RX_DNS_LEGAL.matcher(result).matches()) + { + return result; + } + // Finally this cannot fail. + return MangleOne(GUID.generate()); + } + + /** + * Mangle one component of a DNS legal name. + * @param name The component. + * @return The mangled component. + */ + static String MangleOne(String name) + { + name = name.toLowerCase(); + name = RX_ILLEGAL_CHARS.matcher(name).replaceAll("-"); + name = RX_HYPHENS.matcher(name).replaceAll("-"); + name = RX_LEADING_HYPHEN.matcher(name).replaceAll("x"); + name = RX_TRAILING_HYPHEN.matcher(name).replaceAll("x"); + return name; + } +} diff --git a/source/java/org/alfresco/web/bean/wcm/DNSNameManglerTest.java b/source/java/org/alfresco/web/bean/wcm/DNSNameManglerTest.java new file mode 100644 index 0000000000..194f8ecea6 --- /dev/null +++ b/source/java/org/alfresco/web/bean/wcm/DNSNameManglerTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2006 Alfresco, Inc. + * + * Licensed under the Mozilla Public License version 1.1 + * with a permitted attribution clause. You may obtain a + * copy of the License at + * + * http://www.alfresco.org/legal/license.txt + * + * 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.alfresco.web.bean.wcm; + +import junit.framework.TestCase; + +/** + * Test the DNSNameMangler. + * @author britt + */ +public class DNSNameManglerTest extends TestCase +{ + /** + * Test it. + */ + public void testIt() + { + try + { + String mangled = DNSNameMangler.MakeDNSName("website", "britt", "main"); + System.out.println(mangled); + assertTrue(mangled.length() <= 59); + mangled = DNSNameMangler.MakeDNSName("website", "Foodle Dee dOO", "main"); + System.out.println(mangled); + assertTrue(mangled.length() <= 59); + mangled = DNSNameMangler.MakeDNSName("website-thinkl$", "winky_froo", "orkle"); + System.out.println(mangled); + assertTrue(mangled.length() <= 59); + mangled = DNSNameMangler.MakeDNSName("fork", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZZxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "winkle"); + System.out.println(mangled); + assertTrue(mangled.length() <= 59); + mangled = DNSNameMangler.MakeDNSName("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "Frederick", "preview"); + System.out.println(mangled); + assertTrue(mangled.length() <= 59); + } + catch (Exception e) + { + e.printStackTrace(System.err); + fail(); + } + } +}