1

The following sample attempts to use two different methods to perform path composition for nested resources. The first uses ClientResource.getChild(), while the second creates a new Reference from the original ClientResource's reference, and then adds two segments before constructing a new ClientResource for that path.

public static void main( String [] args ) {
    // I would expect that this:
    ClientResource res1 = new ClientResource( "http://localhost" );
    ClientResource res2 = res1.getChild( "foo" );
    ClientResource res3 = res2.getChild( "bar" );

    System.out.printf( "res3 points to %s\n", res3.getReference().getTargetRef().toString() );

    // Would be the same as this:
    Reference ref = new Reference( res1.getReference() );
    ref.addSegment( "foo" ).addSegment( "bar" );

    ClientResource res4 = new ClientResource( ref );

    System.out.printf( "res4 points to %s\n", res4.getReference().getTargetRef().toString() );
}

Here is the output:

res3 points to http://localhost/bar
res4 points to http://localhost/foo/bar

Interestingly, if I include a / when creating res2 (res1.getChild( "foo/" );), I get the result I was expecting. However, that doesn't help me if I'm first deriving resource foo, and later want to derive its child resource bar.

So is this a bug, or is there a purpose to this behavior?

Note: I'm using Restlet 2.3.0

Edit: This has proven very distressing to me, because the presence of getChild was one of the reasons I selected Restlet over Jersey. Between this apparent bug and the resource path conventions implied by Restlet, I believe I will be using Jersey for future projects.

Ben Schreiber
  • 377
  • 2
  • 13

0 Answers0