mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Added getBaseUrl to Utils
This commit is contained in:
@@ -13,8 +13,10 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.rmi.dgc.VMID;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -414,6 +416,20 @@ public final class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the baseurl from a given URL string
|
||||
* @param urlString URL string
|
||||
* @return baseurl (without any context path) or null (if URL was invalid)
|
||||
*/
|
||||
public static String getBaseUrl(String urlString) {
|
||||
try {
|
||||
URL url = new URL(urlString);
|
||||
String baseUrl = url.getProtocol() + "://" + url.getHost();
|
||||
return baseUrl;
|
||||
} catch (MalformedURLException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the hostname from a given URI string
|
||||
|
@@ -22,6 +22,27 @@ import org.junit.Test;
|
||||
*/
|
||||
public class UtilsTest extends AbstractUnitTest {
|
||||
|
||||
/**
|
||||
* Test of getBaseUrl method, of class Utils
|
||||
*/
|
||||
@Test
|
||||
public void testGetBaseUrl() {
|
||||
assertEquals("Test remove /server", "http://dspace.org",
|
||||
Utils.getBaseUrl("http://dspace.org/server"));
|
||||
|
||||
assertEquals("Test remove /server/api/core/items", "https://dspace.org",
|
||||
Utils.getBaseUrl("https://dspace.org/server/api/core/items"));
|
||||
|
||||
assertEquals("Test remove trailing slash", "https://dspace.org",
|
||||
Utils.getBaseUrl("https://dspace.org/"));
|
||||
|
||||
assertEquals("Test keep url", "https://demo.dspace.org",
|
||||
Utils.getBaseUrl("https://demo.dspace.org"));
|
||||
|
||||
// This uses a bunch of reserved URI characters
|
||||
assertNull("Test invalid URI returns null", Utils.getBaseUrl("&+,?/@="));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getHostName method, of class Utils
|
||||
*/
|
||||
|
Reference in New Issue
Block a user