Author: chris.laprun(a)jboss.com
Date: 2007-02-15 20:51:36 -0500 (Thu, 15 Feb 2007)
New Revision: 6305
Modified:
trunk/common/src/main/org/jboss/portal/common/util/Tools.java
trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
trunk/common/src/main/org/jboss/portal/test/common/StringTestCase.java
trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java
Log:
- Renamed replaceBoundedString to replaceAllInstancesOfBoundedString.
- Added version with more options.
- Added replaceServerPortInURL method.
- Added URLReplacementGenerator to replace the port in detected URLs.
- Updated tests.
Modified: trunk/common/src/main/org/jboss/portal/common/util/Tools.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/Tools.java 2007-02-16 01:07:08 UTC
(rev 6304)
+++ trunk/common/src/main/org/jboss/portal/common/util/Tools.java 2007-02-16 01:51:36 UTC
(rev 6305)
@@ -999,8 +999,14 @@
}
}
- public static String replaceBoundedString(String initial, String prefix, String
suffix, String replacement)
+ public static String replaceAllInstancesOfBoundedString(String initial, String prefix,
String suffix, String replacement)
{
+ return replaceBoundedString(initial, prefix, suffix, replacement, true, false);
+ }
+
+ public static String replaceBoundedString(String initial, String prefix, String
suffix, String replacement,
+ boolean replaceIfBoundedStringEmpty, boolean
keepBoundaries)
+ {
if (initial == null || initial.length() == 0)
{
return initial;
@@ -1021,14 +1027,25 @@
if (suffixIndex != -1)
{
- tmp.delete(prefixIndex, suffixIndex + suffixLength);
- tmp.insert(prefixIndex, replacement);
+ // we don't care about empty bounded strings or prefix and suffix
don't delimit an empty String => replace!
+ if (replaceIfBoundedStringEmpty || suffixIndex != prefixIndex +
prefixLength)
+ {
+ if (keepBoundaries)
+ {
+ tmp.delete(prefixIndex + prefixLength, suffixIndex);
+ tmp.insert(prefixIndex + prefixLength, replacement);
+ }
+ else
+ {
+ tmp.delete(prefixIndex, suffixIndex + suffixLength);
+ tmp.insert(prefixIndex, replacement);
+ }
+ }
}
prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
}
return tmp.toString();
-
}
}
\ No newline at end of file
Modified: trunk/common/src/main/org/jboss/portal/common/util/URLTools.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-02-16 01:07:08
UTC (rev 6304)
+++ trunk/common/src/main/org/jboss/portal/common/util/URLTools.java 2007-02-16 01:51:36
UTC (rev 6305)
@@ -40,18 +40,29 @@
public class URLTools
{
public static final String RE_EMAIL_VALIDATION =
"^([a-zA-Z0-9]+(([\\.\\-\\_]?[a-zA-Z0-9]+)+)?)\\(a)(([a-zA-Z0-9]+[\\.\\-\\_])+[a-zA-Z]{2,4})$";
- private static final Pattern LINK =
Pattern.compile("(?:href|action|src)\\s*=\\s*('|\")\\s*([^'\"]*)\\s*('|\")",
+ private static final Pattern LINK =
Pattern.compile("(?:href|action|src|location)\\s*=\\s*('|\")\\s*([^'\"]*)\\s*('|\")",
Pattern.CASE_INSENSITIVE);
public static final String HTTP_PREFIX = "http://";
public static final String HTTPS_PREFIX = "https://";
+ public static final String FTP_PREFIX = "ftp://";
public static final String FILE_PREFIX = "/";
public static boolean isURLAbsolute(String url)
{
- return url.startsWith(HTTP_PREFIX) || url.startsWith(HTTPS_PREFIX) ||
url.startsWith(FILE_PREFIX);
+ return isNetworkURL(url) || url.startsWith(FILE_PREFIX);
}
+ public static boolean isNetworkURL(String url)
+ {
+ if (url == null || url.length() == 0)
+ {
+ return false;
+ }
+
+ return url.startsWith(HTTP_PREFIX) || url.startsWith(HTTPS_PREFIX) ||
url.startsWith(FTP_PREFIX);
+ }
+
/**
* Enforces that the given URL is absolute
*
@@ -231,4 +242,48 @@
{
public abstract String getReplacementFor(int currentIndex, URLMatch currentMatch);
}
+
+ public static class PortReplacementGenerator extends URLReplacementGenerator
+ {
+ private int replacementPort;
+
+ public PortReplacementGenerator(int replacementPort)
+ {
+ this.replacementPort = replacementPort;
+ }
+
+ public String getReplacementFor(int currentIndex, URLMatch currentMatch)
+ {
+ return replaceServerPortInURL(currentMatch.getURLAsString(), replacementPort);
+ }
+ }
+
+ public static String replaceServerPortInURL(String url, int newPort)
+ {
+ if (!isNetworkURL(url))
+ {
+ return url;
+ }
+
+ StringBuffer buf = new StringBuffer(url);
+ int afterProtocol = url.indexOf("://") + 3;
+ int beforePort = url.indexOf(':', afterProtocol);
+ int afterPort;
+
+ if (beforePort != -1)
+ {
+ afterPort = url.indexOf('/', beforePort);
+ buf.delete(beforePort + 1, afterPort);
+ buf.insert(beforePort + 1, newPort);
+ }
+ else
+ {
+ // port number was not present
+ afterPort = url.indexOf('/', afterProtocol);
+ buf.insert(afterPort, ":" + newPort);
+ }
+
+ return buf.toString();
+ }
+
}
Modified: trunk/common/src/main/org/jboss/portal/test/common/StringTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/StringTestCase.java 2007-02-16
01:07:08 UTC (rev 6304)
+++ trunk/common/src/main/org/jboss/portal/test/common/StringTestCase.java 2007-02-16
01:51:36 UTC (rev 6305)
@@ -48,11 +48,13 @@
public void testReplaceBoundedString()
{
- assertEquals("", Tools.replaceBoundedString("",
"PREFIX", "SUFFIX", "REPLACEMENT"));
- assertEquals("REPLACEMENT",
Tools.replaceBoundedString("PREFIXSUFFIX", "PREFIX",
"SUFFIX", "REPLACEMENT"));
- assertEquals("aaaaREPLACEMENTccccc",
Tools.replaceBoundedString("aaaaPREFIXbbbbbSUFFIXccccc", "PREFIX",
"SUFFIX", "REPLACEMENT"));
- assertEquals("aaaPREFIXbbbbSUFF",
Tools.replaceBoundedString("aaaPREFIXbbbbSUFF", "PREFIX",
"SUFFIX", "REPLACEMENT"));
- assertEquals("aRcccReeeR",
Tools.replaceBoundedString("aPbbScccPdSeeePS", "P", "S",
"R"));
+ assertEquals("", Tools.replaceAllInstancesOfBoundedString("",
"PREFIX", "SUFFIX", "REPLACEMENT"));
+ assertEquals("REPLACEMENT",
Tools.replaceAllInstancesOfBoundedString("PREFIXSUFFIX", "PREFIX",
"SUFFIX", "REPLACEMENT"));
+ assertEquals("PREFIXSUFFIX",
Tools.replaceBoundedString("PREFIXSUFFIX", "PREFIX",
"SUFFIX", "REPLACEMENT", false, true));
+ assertEquals("PREFIXSUFFIX",
Tools.replaceBoundedString("PREFIXSUFFIX", "PREFIX",
"SUFFIX", "REPLACEMENT", false, false));
+ assertEquals("aaaaREPLACEMENTccccc",
Tools.replaceAllInstancesOfBoundedString("aaaaPREFIXbbbbbSUFFIXccccc",
"PREFIX", "SUFFIX", "REPLACEMENT"));
+ assertEquals("aaaPREFIXbbbbSUFF",
Tools.replaceAllInstancesOfBoundedString("aaaPREFIXbbbbSUFF",
"PREFIX", "SUFFIX", "REPLACEMENT"));
+ assertEquals("aRcccReeeR",
Tools.replaceAllInstancesOfBoundedString("aPbbScccPdSeeePS", "P",
"S", "R"));
+ assertEquals("PSaPScccReeePS",
Tools.replaceBoundedString("PSaPScccPdSeeePS", "P", "S",
"R", false, false));
}
-
}
Modified: trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java 2007-02-16
01:07:08 UTC (rev 6304)
+++ trunk/common/src/main/org/jboss/portal/test/common/URLToolsTestCase.java 2007-02-16
01:51:36 UTC (rev 6305)
@@ -23,6 +23,7 @@
package org.jboss.portal.test.common;
import junit.framework.TestCase;
+import org.jboss.portal.common.util.URLTools;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -52,9 +53,9 @@
{
String markup = MARKUP;
- org.jboss.portal.common.util.URLTools.URLMatch[] links =
org.jboss.portal.common.util.URLTools.extractURLsFrom(markup);
+ URLTools.URLMatch[] links = URLTools.extractURLsFrom(markup);
assertEquals(4, links.length);
- org.jboss.portal.common.util.URLTools.URLMatch link = links[0];
+ URLTools.URLMatch link = links[0];
assertEquals("wsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=rO0ABXNyACdvcmcuamJvc3MucG9ydGFsLnNl"
+
"cnZlci51dGlsLlBhcmFtZXRlcnOJoAlMQZGhngIAAUwAA21hcHQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAEWphdmEudXRpbC5IYXNoTWFwBQ"
+
"fawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA_QAAAAAAADHcIAAAAEAAAAAF0AARuYW1ldXIAE1tMamF2YS5sYW5nLlN0"
+
@@ -66,7 +67,7 @@
String url =
"wsrp_rewrite?wsrp-urlType=render&wsrp-mode=help/wsrp_rewrite";
markup = "12345href='" + url + "'76";
- links = org.jboss.portal.common.util.URLTools.extractURLsFrom(markup);
+ links = URLTools.extractURLsFrom(markup);
link = links[0];
int startIndex = 11;
assertEquals(startIndex, link.getStart());
@@ -77,18 +78,18 @@
public void testReplaceURLs()
{
- String markup = org.jboss.portal.common.util.URLTools.replaceURLsBy(MARKUP, new
String[]{"foo", "bar", "baz", "buz"});
+ String markup = URLTools.replaceURLsBy(MARKUP, new String[]{"foo",
"bar", "baz", "buz"});
String replaced = "Hello, Anonymous!\nCounter: 0<a href='foo'>My
name is Julien</a><a href='bar'>My name is Roy</a>" +
"<action='baz'>counter++</a><a
href='buz'>counter--</a>";
assertEquals(replaced, markup);
- assertEquals(replaced,
org.jboss.portal.common.util.URLTools.replaceURLsBy(replaced, (String[])null));
+ assertEquals(replaced, URLTools.replaceURLsBy(replaced, (String[])null));
String mixed = "<a
href='wsrp_rewrite?wsrp-urlType=render&wsrp-mode=help/wsrp_rewrite'>My
name is Julien</a>" +
"<a href='bar'>My name is Roy</a>";
assertEquals("<a href='foo'>My name is Julien</a><a
href='bar'>My name is Roy</a>",
- org.jboss.portal.common.util.URLTools.replaceURLsBy(mixed, new
org.jboss.portal.common.util.URLTools.URLReplacementGenerator()
+ URLTools.replaceURLsBy(mixed, new URLTools.URLReplacementGenerator()
{
- public String getReplacementFor(int currentIndex,
org.jboss.portal.common.util.URLTools.URLMatch currentMatch)
+ public String getReplacementFor(int currentIndex, URLTools.URLMatch
currentMatch)
{
String urlAsString = currentMatch.getURLAsString();
if (urlAsString.startsWith("wsrp_rewrite"))
@@ -99,4 +100,64 @@
}
}));
}
+
+ public void testReplaceAllPorts()
+ {
+ String original = "<wsdl:definitions
targetNamespace='urn:oasis:names:tc:wsrp:v1:wsdl'\n" +
+ "
xmlns:bind='urn:oasis:names:tc:wsrp:v1:bind'\n" +
+ "
xmlns='http://schemas.xmlsoap.org/wsdl/'\n"
+
+ "
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'\n" +
+ "
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'\n" +
+ "
xmlns:intf='urn:oasis:names:tc:wsrp:v1:intf'\n" +
+ "
xmlns:tns='urn:oasis:names:tc:wsrp:v1:wsdl'>\n" +
+ " <import namespace='urn:oasis:names:tc:wsrp:v1:bind'
location='wsrp_v1_bindings.wsdl'/>\n" +
+ " <wsdl:service name='WSRPService'>\n" +
+ " <wsdl:port binding='bind:WSRP_v1_Markup_Binding_SOAP'
name='WSRPBaseService'>\n" +
+ " <soap:address
location='http://localhost/portal-wsrp/ServiceDescriptionService'/>\n" +
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_ServiceDescription_Binding_SOAP'
name='WSRPServiceDescriptionService'>\n" +
+ " <soap:address
location='http://localhost/portal-wsrp/MarkupService'/>\n" +
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_Registration_Binding_SOAP'
name='WSRPRegistrationService'>\n" +
+ " <soap:address
location='http://localhost/portal-wsrp/RegistrationService'/>\n" +
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_PortletManagement_Binding_SOAP'
name='WSRPPortletManagementService'>\n" +
+ " <soap:address
location='http://localhost/portal-wsrp/PortletManagementService'/>\n" +
+ " </wsdl:port>\n" +
+ " </wsdl:service>\n" +
+ "</wsdl:definitions>";
+ String result = "<wsdl:definitions
targetNamespace='urn:oasis:names:tc:wsrp:v1:wsdl'\n" +
+ "
xmlns:bind='urn:oasis:names:tc:wsrp:v1:bind'\n" +
+ "
xmlns='http://schemas.xmlsoap.org/wsdl/'\n"
+
+ "
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'\n" +
+ "
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'\n" +
+ "
xmlns:intf='urn:oasis:names:tc:wsrp:v1:intf'\n" +
+ "
xmlns:tns='urn:oasis:names:tc:wsrp:v1:wsdl'>\n" +
+ " <import namespace='urn:oasis:names:tc:wsrp:v1:bind'
location='wsrp_v1_bindings.wsdl'/>\n" +
+ " <wsdl:service name='WSRPService'>\n" +
+ " <wsdl:port binding='bind:WSRP_v1_Markup_Binding_SOAP'
name='WSRPBaseService'>\n" +
+ " <soap:address
location='http://localhost:8888/portal-wsrp/ServiceDescriptionService'/>\n"
+
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_ServiceDescription_Binding_SOAP'
name='WSRPServiceDescriptionService'>\n" +
+ " <soap:address
location='http://localhost:8888/portal-wsrp/MarkupService'/>\n" +
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_Registration_Binding_SOAP'
name='WSRPRegistrationService'>\n" +
+ " <soap:address
location='http://localhost:8888/portal-wsrp/RegistrationService'/>\n" +
+ " </wsdl:port>\n" +
+ " <wsdl:port
binding='bind:WSRP_v1_PortletManagement_Binding_SOAP'
name='WSRPPortletManagementService'>\n" +
+ " <soap:address
location='http://localhost:8888/portal-wsrp/PortletManagementService'/>\n"
+
+ " </wsdl:port>\n" +
+ " </wsdl:service>\n" +
+ "</wsdl:definitions>";
+
+ assertEquals(result, URLTools.replaceURLsBy(original, new
URLTools.PortReplacementGenerator(8888)));
+ }
+
+ public void testReplaceServerPort()
+ {
+ assertEquals("http://hostname:8088/some/path",
URLTools.replaceServerPortInURL("http://hostname:8080/some/path", 8088));
+ assertEquals("https://hostname:8088/some/path",
URLTools.replaceServerPortInURL("https://hostname:8080/some/path", 8088));
+ assertEquals("http://hostname:8088/some/path",
URLTools.replaceServerPortInURL("http://hostname/some/path", 8088));
+ assertEquals("https://hostname:8088/some/path",
URLTools.replaceServerPortInURL("https://hostname/some/path", 8088));
+ }
}