From 0cd128581b298d5073d82187a0e07e92d7ce3e15 Mon Sep 17 00:00:00 2001 From: skonno Date: Wed, 14 Mar 2012 10:37:42 +0000 Subject: [PATCH] Improved Device::getAbsoluteURL() to return the specified URL more correctly. git-svn-id: https://svn.code.sf.net/p/cgupnpjava/code/trunk@179 8e852ebd-950f-0410-afcc-dbedaebff0b6 --- ChangeLog | 3 + .../java/org/cybergarage/upnp/Device.java | 110 ++++++++++---- .../java/org/cybergarage/http/HTTPTest.java | 61 ++++++++ .../java/org/cybergarage/upnp/DeviceTest.java | 136 ++++++++++++++++++ .../org/cybergarage/upnp/ServiceTest.java | 40 ++++++ 5 files changed, 322 insertions(+), 28 deletions(-) create mode 100644 upnp-stack/src/test/java/org/cybergarage/http/HTTPTest.java create mode 100644 upnp-stack/src/test/java/org/cybergarage/upnp/DeviceTest.java create mode 100644 upnp-stack/src/test/java/org/cybergarage/upnp/ServiceTest.java diff --git a/ChangeLog b/ChangeLog index 6a0210a8..0190cd25 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2012-03-14 Satoshi Konno + * Improved Device::getAbsoluteURL() to return the specified URL more correctly. + 2011-02-08 Satoshi Konno * Changed the trunk directory into 'trunk' instead of 'trunk/cyberlink' * Fixed some pom files to run normally. diff --git a/upnp-stack/src/main/java/org/cybergarage/upnp/Device.java b/upnp-stack/src/main/java/org/cybergarage/upnp/Device.java index 2457b360..1126c5c1 100644 --- a/upnp-stack/src/main/java/org/cybergarage/upnp/Device.java +++ b/upnp-stack/src/main/java/org/cybergarage/upnp/Device.java @@ -263,41 +263,95 @@ public void unlock() // getAbsoluteURL //////////////////////////////////////////////// - public String getAbsoluteURL(String urlString) - { + public String getAbsoluteURL(String urlString, String baseURLStr, String locationURLStr) + { + if ((urlString == null) || (urlString.length() <= 0)) + return ""; + try { URL url = new URL(urlString); return url.toString(); } catch (Exception e) {} - + + if ((baseURLStr == null) || (baseURLStr.length() <= 0)) { + if ((locationURLStr != null) && (0 < locationURLStr.length())) { + if (!locationURLStr.endsWith("/") || !urlString.startsWith("/")) { + String absUrl = locationURLStr + urlString; + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + } + else { + String absUrl = locationURLStr + urlString.substring(1); + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + } + + String absUrl = HTTP.getAbsoluteURL(locationURLStr, urlString); + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + + // Thanks for Steven Yen (2003/09/03) + Device rootDev = getRootDevice(); + if (rootDev != null) { + String location = rootDev.getLocation(); + String locationHost = HTTP.getHost(location); + int locationPort = HTTP.getPort(location); + baseURLStr = HTTP.getRequestHostURL(locationHost, locationPort); + } + } + } + + if ((baseURLStr != null) && (0 < baseURLStr.length())) { + if (!baseURLStr.endsWith("/") || !urlString.startsWith("/")) { + String absUrl = baseURLStr + urlString; + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + } + else { + String absUrl = baseURLStr + urlString.substring(1); + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + } + + String absUrl = HTTP.getAbsoluteURL(baseURLStr, urlString); + try { + URL url = new URL(absUrl); + return url.toString(); + } + catch (Exception e) {} + } + + return urlString; + } + + public String getAbsoluteURL(String urlString) + { + String baseURLStr = null; + String locationURLStr = null; + Device rootDev = getRootDevice(); - String urlBaseStr = rootDev.getURLBase(); - - // Thanks for Steven Yen (2003/09/03) - if (urlBaseStr == null || urlBaseStr.length() <= 0) { - String location = rootDev.getLocation(); - String locationHost = HTTP.getHost(location); - int locationPort = HTTP.getPort(location); - urlBaseStr = HTTP.getRequestHostURL(locationHost, locationPort); - } + if (rootDev != null) { + baseURLStr = rootDev.getURLBase(); + locationURLStr = rootDev.getLocation(); + } - urlString = HTTP.toRelativeURL(urlString); - String absUrl = urlBaseStr + urlString; - try { - URL url = new URL(absUrl); - return url.toString(); - } - catch (Exception e) {} - - absUrl = HTTP.getAbsoluteURL(urlBaseStr, urlString); - try { - URL url = new URL(absUrl); - return url.toString(); - } - catch (Exception e) {} - - return ""; + return getAbsoluteURL(urlString, baseURLStr, locationURLStr); } //////////////////////////////////////////////// diff --git a/upnp-stack/src/test/java/org/cybergarage/http/HTTPTest.java b/upnp-stack/src/test/java/org/cybergarage/http/HTTPTest.java new file mode 100644 index 00000000..e97f34d8 --- /dev/null +++ b/upnp-stack/src/test/java/org/cybergarage/http/HTTPTest.java @@ -0,0 +1,61 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2012 +* +* This is licensed under BSD-style license, see file COPYING. +* +******************************************************************/ + +package org.cybergarage.http; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class HTTPTest extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public HTTPTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( HTTPTest.class ); + } + + public void testToRelativeURL() + { + String urlString; + + urlString = HTTP.toRelativeURL("/testURL"); + assertTrue(urlString, urlString.equals("/testURL")); + + urlString = HTTP.toRelativeURL("testURL"); + assertTrue(urlString, urlString.equals("/testURL")); + } + + public void testGetAbsoluteURL() + { + String urlString; + + urlString = HTTP.getAbsoluteURL("http://192.168.0.1:80/", "/testURL"); + assertTrue(urlString, urlString.equals("http://192.168.0.1:80/testURL")); + + urlString = HTTP.getAbsoluteURL("http://192.168.0.1:80/", "testURL"); + assertTrue(urlString, urlString.equals("http://192.168.0.1:80/testURL")); + + urlString = HTTP.getAbsoluteURL("http://192.168.0.1:80", "/testURL"); + assertTrue(urlString, urlString.equals("http://192.168.0.1:80/testURL")); + } +} diff --git a/upnp-stack/src/test/java/org/cybergarage/upnp/DeviceTest.java b/upnp-stack/src/test/java/org/cybergarage/upnp/DeviceTest.java new file mode 100644 index 00000000..5132bdb1 --- /dev/null +++ b/upnp-stack/src/test/java/org/cybergarage/upnp/DeviceTest.java @@ -0,0 +1,136 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2012 +* +* This is licensed under BSD-style license, see file COPYING. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class DeviceTest extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public DeviceTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( DeviceTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testDeviceAbsoluteURL() + { + Device dev = new Device(); + String devAbsUrl; + + /******************************************************************************** + * O:serviceURLStr ?:baseURLStr ?:locationURLStr + ********************************************************************************/ + + // O:serviceURLStr -:baseURLStr -:locationURLStr + devAbsUrl = dev.getAbsoluteURL("http://192.168.0.1:80/serviceURL", null, null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.1:80/serviceURL")); + + /* O:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("http://192.168.0.1:80/serviceURL", "http://192.168.0.2:80/", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.1:80/serviceURL")); + + /* O:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("http://192.168.0.1:80/serviceURL", null, "http://192.168.0.3:80/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.1:80/serviceURL")); + + /* O:serviceURLStr O:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("http://192.168.0.1:80/serviceURL", "http://192.168.0.2:80/", "http://192.168.0.3:80/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.1:80/serviceURL")); + + /******************************************************************************** + * X:serviceURLStr X:baseURLStr X:locationURLStr + ********************************************************************************/ + + devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, null); + assertTrue(devAbsUrl, devAbsUrl.equals("/serviceURL")); + + /******************************************************************************** + * X:serviceURLStr O:baseURLStr -:locationURLStr (CASE01) + ********************************************************************************/ + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", "http://192.168.0.2:80/", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/serviceURL")); + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", "http://192.168.0.2:80", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/serviceURL")); + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", "http://192.168.0.2:80/", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/serviceURL")); + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", "http://192.168.0.2:80", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/serviceURL")); + + /******************************************************************************** + * X:serviceURLStr O:baseURLStr -:locationURLStr (CASE02) + ********************************************************************************/ + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", "http://192.168.0.2:80/device/", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/device/serviceURL")); + + /* X:serviceURLStr O:baseURLStr -:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", "http://192.168.0.2:80/device/", null); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.2:80/device/serviceURL")); + + /******************************************************************************** + * X:serviceURLStr -:baseURLStr O:locationURLStr (CASE01) + ********************************************************************************/ + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "http://192.168.0.3:80/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/serviceURL")); + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "http://192.168.0.3:80"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/serviceURL")); + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "http://192.168.0.3:80/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/serviceURL")); + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "http://192.168.0.3:80"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/serviceURL")); + + /******************************************************************************** + * X:serviceURLStr -:baseURLStr O:locationURLStr (CASE02) + ********************************************************************************/ + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "http://192.168.0.3:80/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/serviceURL")); + + /* X:serviceURLStr -:baseURLStr O:locationURLStr */ + devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "http://192.168.0.3:80/device/"); + assertTrue(devAbsUrl, devAbsUrl.equals("http://192.168.0.3:80/device/serviceURL")); + } +} diff --git a/upnp-stack/src/test/java/org/cybergarage/upnp/ServiceTest.java b/upnp-stack/src/test/java/org/cybergarage/upnp/ServiceTest.java new file mode 100644 index 00000000..e166f7ac --- /dev/null +++ b/upnp-stack/src/test/java/org/cybergarage/upnp/ServiceTest.java @@ -0,0 +1,40 @@ +/****************************************************************** +* +* CyberLink for Java +* +* Copyright (C) Satoshi Konno 2002-2012 +* +* This is licensed under BSD-style license, see file COPYING. +* +******************************************************************/ + +package org.cybergarage.upnp; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class ServiceTest extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public ServiceTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( ServiceTest.class ); + } + + public void testServiceAbsoluteURL() + { + } +}