Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1345

Instances built from org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget must inherit of a snaphot of the parent configuration

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 3.0.17.Final
    • 3.0.13.Final, 3.0.14.Final, 3.0.15.Final, 3.0.16.Final
    • jaxrs
    • Hide
      public class ClientConfigurationSnapshotTest {
      
      	public static class MyFirstFilter implements ClientRequestFilter {
      
      		@Override
      		public void filter(ClientRequestContext requestContext) throws IOException {
      			System.out.println(MyFirstFilter.class);
      		}
      
      	}
      
      	public static class MySecondFilter implements ClientRequestFilter {
      
      		@Override
      		public void filter(ClientRequestContext requestContext) throws IOException {
      			System.out.println(MySecondFilter.class);
      		}
      
      	}
      
      	@Test
      	public void testClientConfigurationSnapshot() {
      		Client client = ClientBuilder.newClient();
      		try {
      
      			WebTarget webTarget1 = client.target("http://www.google.fr");
      
      			// Register MyFirstFilter (the same if registered as a class or
      			// singleton)
      			webTarget1.register(MyFirstFilter.class);
      			// Register a property
      			webTarget1.property("property1", "value1");
      
      			// According to the documentation following instance must inherit of
      			// webTarget1 snapshoted configuration
      			WebTarget webTarget2 = webTarget1.path("");
      			// webTarget2Configuration inherits of webTarget1 snapshoted
      			// configuration => OK
      			Configuration webTarget2Configuration = webTarget2.getConfiguration();
      			checkConfiguration(webTarget2Configuration);
      			// Only MyFirstFilter is used => OK
      			webTarget2.request().get().close();
      
      			// Register MySecondFilter
      			webTarget1.register(MySecondFilter.class);
      			// Register another property
      			webTarget1.property("property2", "value2");
      
      			// webTarget2Configuration actually inherits of more than the
      			// snapshot of webTarget1 configuration => NOK
      			checkConfiguration(webTarget2Configuration);
      			// Both MyFirstFilter and MySecondFilter are used => NOK
      			webTarget2.request().get().close();
      
      		} finally {
      			client.close();
      		}
      	}
      
      	private void checkConfiguration(Configuration configuration) {
      		Set<Class<?>> classes = configuration.getClasses();
      		// webTarget2Configuration must contain MyFirstFilter.class
      		Assert.assertTrue(classes.contains(MyFirstFilter.class));
      		// webTarget2Configuration must not contain MySecondFilter.class
      		Assert.assertFalse(classes.contains(MySecondFilter.class));
      
      		// webTarget2Configuration properties must only contain entry
      		// {property1=value1}
      		Assert.assertEquals(Collections.singletonMap("property1", "value1"), configuration.getProperties());
      		// webTarget2Configuration must only contain contracts for
      		// MyFirstFilter.class
      		Assert.assertFalse(configuration.getContracts(MyFirstFilter.class).isEmpty());
      		// webTarget2Configuration must not contain contracts for
      		// MySecondFilter.class
      		Assert.assertTrue(configuration.getContracts(MySecondFilter.class).isEmpty());
      	}
      
      }
      
      Show
      public class ClientConfigurationSnapshotTest { public static class MyFirstFilter implements ClientRequestFilter { @Override public void filter(ClientRequestContext requestContext) throws IOException { System .out.println(MyFirstFilter.class); } } public static class MySecondFilter implements ClientRequestFilter { @Override public void filter(ClientRequestContext requestContext) throws IOException { System .out.println(MySecondFilter.class); } } @Test public void testClientConfigurationSnapshot() { Client client = ClientBuilder.newClient(); try { WebTarget webTarget1 = client.target( "http: //www.google.fr" ); // Register MyFirstFilter (the same if registered as a class or // singleton) webTarget1.register(MyFirstFilter.class); // Register a property webTarget1.property( "property1" , "value1" ); // According to the documentation following instance must inherit of // webTarget1 snapshoted configuration WebTarget webTarget2 = webTarget1.path(""); // webTarget2Configuration inherits of webTarget1 snapshoted // configuration => OK Configuration webTarget2Configuration = webTarget2.getConfiguration(); checkConfiguration(webTarget2Configuration); // Only MyFirstFilter is used => OK webTarget2.request().get().close(); // Register MySecondFilter webTarget1.register(MySecondFilter.class); // Register another property webTarget1.property( "property2" , "value2" ); // webTarget2Configuration actually inherits of more than the // snapshot of webTarget1 configuration => NOK checkConfiguration(webTarget2Configuration); // Both MyFirstFilter and MySecondFilter are used => NOK webTarget2.request().get().close(); } finally { client.close(); } } private void checkConfiguration(Configuration configuration) { Set< Class <?>> classes = configuration.getClasses(); // webTarget2Configuration must contain MyFirstFilter.class Assert.assertTrue(classes.contains(MyFirstFilter.class)); // webTarget2Configuration must not contain MySecondFilter.class Assert.assertFalse(classes.contains(MySecondFilter.class)); // webTarget2Configuration properties must only contain entry // {property1=value1} Assert.assertEquals(Collections.singletonMap( "property1" , "value1" ), configuration.getProperties()); // webTarget2Configuration must only contain contracts for // MyFirstFilter.class Assert.assertFalse(configuration.getContracts(MyFirstFilter.class).isEmpty()); // webTarget2Configuration must not contain contracts for // MySecondFilter.class Assert.assertTrue(configuration.getContracts(MySecondFilter.class).isEmpty()); } }

      According to jax-RS specification all methods of any javax.ws.rs.client.WebTarget implementation that return a new WebTarget instance (such as path(),queryParam(),resolveTemplates()...) must ensure that : "A snapshot of the present configuration of the current (parent) target instance is taken and is inherited by the newly constructed (child) target instance."
      Instance of org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget does not seem to follow this rule (see ResteasyProviderFactory getClasses(), getInstances(), getContracts())

              rsigal@redhat.com Ronald Sigal
              nicones Nicolas NESMON
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: