Files
alfresco-community-repo/source/java/org/alfresco/web/bean/wcm/DNSNameMangler.java
Jon Cox ed741873e5 Preserving caps in mangling for prettier autogenerated URLs.
TODO: Even with this change, the mangler still isn't quite right 
       because if we adopt the convention:

             o  Default mangled names approximate 
                the navigation path, when possible

        Then if you're on the Moo website, and you're in the main layer 
        of the "guest" sandbox viewing /my_webapp/xxx.jsp,  then the url 
        should look like this:

           http://www-Moo--guest.avm.localdomain.lan:8180/my_webapp/xxx.jsp

        Not like this:
             http://www-Moo--guest--main.avm.localdomain.lan:8180/my_webapp/xxx.jsp
 
        Put another way, if the "main" layer is imlicit for the user, 
        it should not appear in the mangled name (note, the 'preview' layer
        is non-default, thus explicit).
        
        Therefore 'guest' on Moo should have the following name-mangled urls 
        accessible:
        
          http://www-Moo--guest.avm.localdomain.lan:8180/my_webapp/xxx.jsp
          http://www-Moo--guest--preview.avm.localdomain.lan:8180/my_webapp/xxx.jsp


 NOTE: Consider how this would work with more complicated overlays.  

   Example 1:

       Suppose we had a website named   "Moo",
       and an author            name    "Alice"
       wanted to see template preview.

       We could autogenerate something like this:
           http://www-Moo--Alice--Preview.avm.localdomain.lan:8180/my_webapp/...

       The non-preview version would be in:
           http://www-Moo--Alice.avm.localdomain.lan:8180/my_webapp/...


       In the advanced GUI, just like you can see the Alice sandbox
       hanging off the Moo website, you'd be able to see/navigate 
       to the "Preview" layer  (though we would not expose such a thing
       by default in the simple gui).


   Example 2:

       Suppose we had a website named   "Moo",
       and a content reviewer   named   "John Smith"
       wanted to see a submission from  "Alice"

       We could autogenerate something like this:

           http://www-Moo--John-Smith--SUB-Alice.avm.localdomain.lan:8180/my_webapp/...

       (i.e.: autogenerated sumission layers could just be
              "SUB-" + DNS name of layer tha did the submit).
       



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3909 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2006-09-24 21:51:39 +00:00

92 lines
2.9 KiB
Java

/*
* 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-zA-Z0-9][a-zA-Z0-9-]{0,57}[a-zA-Z0-9]$");
private static final Pattern RX_ILLEGAL_CHARS =
Pattern.compile("[^a-zA-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)
{
// Preserve case for prettier auto-generated URLs
// 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;
}
}