Re: URL context constructor broken?
On Thu, 30 Dec 2010 05:20:39 -0800, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
There in a URL constructor that takes an context url and a relative
String. It is supposed to merge them into a new URL. It mostly
works, but fails on this list of real world examples with a
MalformedURLException. I am trying to analyse the location field in an
http redirect and apply it to the original URL.
I created the following SSCCE to explore this, and oddly one of the
URLS that failed is now behaving. I tried both under Java.exe and Jet.
I am going to do more tests.
/*
* @(#)TestURL.java
*
* Summary: Test the two-parameter URL constructor.
*
* Copyright: (c) 2010 Roedy Green, Canadian Mind Products,
http://mindprod.com
*
* Licence: This software may be copied and used freely for any
purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.6+
*
* Created with: IntelliJ IDEA IDE.
*
* Version History:
* 1.0 2010-12-30 - initial version
*/
package com.mindprod.example;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import static java.lang.System.out;
/**
* Test the two-parameter URL constructor, and the equivalent
URI.resolve.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-12-30 - initial version
* @since 2010-12-30
*/
public final class TestURL
{
// -------------------------- STATIC METHODS
--------------------------
/**
* tes the URL constructor with two parms, and the equivalent
URI.resolve.
*
* @param originalURLString original context URL
* @param location partial URL to override the original
* @param expectedURLString What we expect the merged result to
be.
* @param notes notes
*/
private static void test( final String originalURLString,
final String location,
final String expectedURLString,
final String notes )
{
testURL( originalURLString, location, expectedURLString, notes );
testURI( originalURLString, location, expectedURLString, notes );
}
/**
* test URI.resolve.
*
* @param originalURIString original context URL
* @param location partial URL to override the original
* @param expectedURIString What we expect the merged result to
be.
* @param notes notes
*/
private static void testURI( final String originalURIString,
final String location,
final String expectedURIString,
final String notes )
{
out.println( "\n>>>testing URI.resolve: ["
+ originalURIString
+ "] ["
+ location
+ "] " + notes );
final URI originalURI;
try
{
originalURI = new URI( originalURIString );
}
catch ( URISyntaxException e )
{
out.println( " original URI failed with URISyntaxException"
);
return;
}
final URI mergedURI = originalURI.resolve( location );
final String mergedURIString = mergedURI.toString();
if ( mergedURIString.equals( expectedURIString ) )
{
out.println( " OK: [" + mergedURIString + "]" );
}
else
{
out.println( " unexpected resolved URI: ["
+ mergedURIString
+ "] expected URI: ["
+ expectedURIString
+ "]" );
}
}
/**
* testURL the URL constructor with two parms.
*
* @param originalURLString original context URL
* @param location partial URL to override the original
* @param expectedURLString What we expect the merged result to
be.
* @param notes notes
*/
private static void testURL( final String originalURLString,
final String location,
final String expectedURLString,
final String notes )
{
out.println( "\n>>>testing URL constructor: ["
+ originalURLString
+ "] ["
+ location
+ "] "
+ notes );
final URL originalURL;
try
{
originalURL = new URL( originalURLString );
}
catch ( MalformedURLException e )
{
out.println( " original URL failed with
MalformedURLException" );
return;
}
final URL resolvedURL;
try
{
resolvedURL = new URL( originalURL, location );
}
catch ( MalformedURLException e )
{
out.println( " resolve failed with MalformedURLException" );
return;
}
final String resolvedURLString = resolvedURL.toString();
if ( resolvedURLString.equals( expectedURLString ) )
{
out.println( " OK: [" + resolvedURLString + "]" );
}
else
{
out.println( " unexpected resolved URL: ["
+ resolvedURLString
+ "] expected URL: ["
+ expectedURLString
+ "]" );
}
}
// --------------------------- main() method
---------------------------
/**
* Test the URL constructor. It is producing what I consider
anomalous results.
*
* @param args not used
*/
public static void main( final String[] args )
{
test( "http://mindprod.com/jgloss.html",
"/jgloss/jgloss.html",
"http://mindprod.com/jgloss/jgloss.html",
"common redirect pattern" );
test( "http://mindprod.com/",
"/index.html",
"http://mindprod.com/index.html",
"common redirect pattern" );
test( "http://new.myfonts.com/fonts/linotype/frutiger/",
"/fonts/adobe/frutiger/",
"http://new.myfonts.com/fonts/adobe/frutiger/",
"real world redirect pattern, trailing slashes" );
}
}
--
Roedy Green Canadian Mind Products
http://mindprod.com
A short order cook is a master of multitasking. Every movement is
optimised from years of practice. Yet when a computer executes a
multitasking program, it approaches the task as if for the first time.