Skip to content

Commit

Permalink
Improved Device::getAbsoluteURL() to return the specified URL more co…
Browse files Browse the repository at this point in the history
…rrectly.

git-svn-id: https://cgupnpjava.svn.sourceforge.net/svnroot/cgupnpjava/trunk@179 8e852ebd-950f-0410-afcc-dbedaebff0b6
  • Loading branch information
skonno committed Mar 14, 2012
1 parent 37947b9 commit e57d06f
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 28 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2012-03-14 Satoshi Konno <[email protected]>
* Improved Device::getAbsoluteURL() to return the specified URL more correctly.

2011-02-08 Satoshi Konno <[email protected]>
* Changed the trunk directory into 'trunk' instead of 'trunk/cyberlink'
* Fixed some pom files to run normally.
Expand Down
110 changes: 82 additions & 28 deletions upnp-stack/src/main/java/org/cybergarage/upnp/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

////////////////////////////////////////////////
Expand Down
61 changes: 61 additions & 0 deletions upnp-stack/src/test/java/org/cybergarage/http/HTTPTest.java
Original file line number Diff line number Diff line change
@@ -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("https://192.168.0.1:80/", "/testURL");
assertTrue(urlString, urlString.equals("https://192.168.0.1:80/testURL"));

urlString = HTTP.getAbsoluteURL("https://192.168.0.1:80/", "testURL");
assertTrue(urlString, urlString.equals("https://192.168.0.1:80/testURL"));

urlString = HTTP.getAbsoluteURL("https://192.168.0.1:80", "/testURL");
assertTrue(urlString, urlString.equals("https://192.168.0.1:80/testURL"));
}
}
136 changes: 136 additions & 0 deletions upnp-stack/src/test/java/org/cybergarage/upnp/DeviceTest.java
Original file line number Diff line number Diff line change
@@ -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("https://192.168.0.1:80/serviceURL", null, null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.1:80/serviceURL"));

/* O:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("https://192.168.0.1:80/serviceURL", "https://192.168.0.2:80/", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.1:80/serviceURL"));

/* O:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("https://192.168.0.1:80/serviceURL", null, "https://192.168.0.3:80/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.1:80/serviceURL"));

/* O:serviceURLStr O:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("https://192.168.0.1:80/serviceURL", "https://192.168.0.2:80/", "https://192.168.0.3:80/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://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", "https://192.168.0.2:80/", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/serviceURL"));

/* X:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", "https://192.168.0.2:80", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/serviceURL"));

/* X:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", "https://192.168.0.2:80/", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/serviceURL"));

/* X:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("/serviceURL", "https://192.168.0.2:80", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/serviceURL"));

/********************************************************************************
* X:serviceURLStr O:baseURLStr -:locationURLStr (CASE02)
********************************************************************************/

/* X:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("/serviceURL", "https://192.168.0.2:80/device/", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/device/serviceURL"));

/* X:serviceURLStr O:baseURLStr -:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", "https://192.168.0.2:80/device/", null);
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.2:80/device/serviceURL"));

/********************************************************************************
* X:serviceURLStr -:baseURLStr O:locationURLStr (CASE01)
********************************************************************************/

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "https://192.168.0.3:80/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/serviceURL"));

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "https://192.168.0.3:80");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/serviceURL"));

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "https://192.168.0.3:80/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/serviceURL"));

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "https://192.168.0.3:80");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/serviceURL"));

/********************************************************************************
* X:serviceURLStr -:baseURLStr O:locationURLStr (CASE02)
********************************************************************************/

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("/serviceURL", null, "https://192.168.0.3:80/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/serviceURL"));

/* X:serviceURLStr -:baseURLStr O:locationURLStr */
devAbsUrl = dev.getAbsoluteURL("serviceURL", null, "https://192.168.0.3:80/device/");
assertTrue(devAbsUrl, devAbsUrl.equals("https://192.168.0.3:80/device/serviceURL"));
}
}
40 changes: 40 additions & 0 deletions upnp-stack/src/test/java/org/cybergarage/upnp/ServiceTest.java
Original file line number Diff line number Diff line change
@@ -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()
{
}
}

0 comments on commit e57d06f

Please sign in to comment.