johnament closed pull request #352: Config handling for MP rest client URL: https://github.com/apache/cxf/pull/352
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilder.java b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilder.java index 4e231aaf3f8..8fc7b54de89 100644 --- a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilder.java +++ b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilder.java @@ -21,24 +21,20 @@ import java.net.URL; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import javax.ws.rs.core.Configurable; import javax.ws.rs.core.Configuration; import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; +import org.apache.cxf.jaxrs.client.spec.ClientConfigurableImpl; import org.eclipse.microprofile.rest.client.RestClientBuilder; import org.eclipse.microprofile.rest.client.annotation.RegisterProviders; public class CxfTypeSafeClientBuilder implements RestClientBuilder { private String baseUri; - private Map<String, Object> properties; - private List<Object> jaxrsProviders; - - public CxfTypeSafeClientBuilder() { - this.properties = new HashMap<>(); - this.jaxrsProviders = new ArrayList<>(); - } + private final Configurable<CxfTypeSafeClientBuilder> configImpl = + new ClientConfigurableImpl(this); @Override public RestClientBuilder baseUrl(URL url) { @@ -48,9 +44,14 @@ public RestClientBuilder baseUrl(URL url) { @Override public <T> T build(Class<T> aClass) { + if (baseUri == null) { + throw new IllegalStateException("baseUrl not set"); + } RegisterProviders providers = aClass.getAnnotation(RegisterProviders.class); List<Object> providerClasses = new ArrayList<>(); - providerClasses.addAll(this.jaxrsProviders); + Configuration config = configImpl.getConfiguration(); + providerClasses.addAll(config.getClasses()); + providerClasses.addAll(config.getInstances()); if (providers != null) { providerClasses.addAll(Arrays.asList(providers.value())); } @@ -58,60 +59,67 @@ public RestClientBuilder baseUrl(URL url) { bean.setAddress(baseUri); bean.setServiceClass(aClass); bean.setProviders(providerClasses); - bean.setProperties(properties); + bean.setProperties(config.getProperties()); return bean.create(aClass); } @Override public Configuration getConfiguration() { - return null; + return configImpl.getConfiguration(); } @Override - public RestClientBuilder property(String s, Object o) { - this.properties.put(s, o); + public RestClientBuilder property(String key, Object value) { + configImpl.property(key, value); return this; } @Override - public RestClientBuilder register(Class<?> providerClass) { - this.jaxrsProviders.add(providerClass); + public RestClientBuilder register(Class<?> componentClass) { + configImpl.register(componentClass); return this; } @Override - public RestClientBuilder register(Class<?> aClass, int i) { + public RestClientBuilder register(Object component) { + configImpl.register(component); return this; } @Override - public RestClientBuilder register(Class<?> aClass, Class<?>... classes) { + public RestClientBuilder register(Class<?> componentClass, int priority) { + configImpl.register(componentClass, priority); return this; } @Override - public RestClientBuilder register(Class<?> aClass, Map<Class<?>, Integer> map) { + public RestClientBuilder register(Class<?> componentClass, Class<?>... contracts) { + configImpl.register(componentClass, contracts); return this; } @Override - public RestClientBuilder register(Object o) { - this.jaxrsProviders.add(o); + public RestClientBuilder register(Class<?> componentClass, Map<Class<?>, Integer> contracts) { + configImpl.register(componentClass, contracts); return this; } @Override - public RestClientBuilder register(Object o, int i) { + public RestClientBuilder register(Object component, int priority) { + configImpl.register(component, priority); return this; } @Override - public RestClientBuilder register(Object o, Class<?>... classes) { + public RestClientBuilder register(Object component, Class<?>... contracts) { + configImpl.register(component, contracts); return this; } @Override - public RestClientBuilder register(Object o, Map<Class<?>, Integer> map) { + public RestClientBuilder register(Object component, Map<Class<?>, Integer> contracts) { + configImpl.register(component, contracts); return this; } + } diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilderTest.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilderTest.java new file mode 100644 index 00000000000..8d6d1a0bc95 --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/CxfTypeSafeClientBuilderTest.java @@ -0,0 +1,94 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client; +import java.net.URL; +import javax.ws.rs.core.Response; +import org.apache.cxf.microprofile.client.mock.HighPriorityClientReqFilter; +import org.apache.cxf.microprofile.client.mock.HighPriorityMBW; +import org.apache.cxf.microprofile.client.mock.LowPriorityClientReqFilter; +import org.apache.cxf.microprofile.client.mock.MyClient; +import org.eclipse.microprofile.rest.client.RestClientBuilder; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +public class CxfTypeSafeClientBuilderTest extends Assert { + + @Test + public void testConfigMethods() { + RestClientBuilder builder = RestClientBuilder.newBuilder(); + + assertEquals("y", builder.property("x", "y").getConfiguration().getProperty("x")); + + assertTrue(builder.register(HighPriorityMBW.class).getConfiguration().isRegistered(HighPriorityMBW.class)); + + HighPriorityMBW mbw = new HighPriorityMBW(1); + assertTrue(builder.register(mbw).getConfiguration().isRegistered(mbw)); + + } + + @Ignore + @Test + public void testConfigPriorityOverrides() throws Exception { + RestClientBuilder builder = RestClientBuilder.newBuilder(); + builder.register(HighPriorityClientReqFilter.class); // annotation priority of 10 + builder.register(LowPriorityClientReqFilter.class, 5); // overriding priority to be 5 (preferred) + MyClient c = builder.baseUrl(new URL("http://localhost/null")).build(MyClient.class); + Response r = c.get(); + assertEquals("low", r.readEntity(String.class)); + } +/** using for test coverage + @Override + public RestClientBuilder register(Class<?> componentClass, int priority) { + configImpl.register(componentClass, priority); + return this; + } + + @Override + public RestClientBuilder register(Class<?> componentClass, Class<?>... contracts) { + configImpl.register(componentClass, contracts); + return this; + } + + @Override + public RestClientBuilder register(Class<?> componentClass, Map<Class<?>, Integer> contracts) { + configImpl.register(componentClass, contracts); + return this; + } + + @Override + public RestClientBuilder register(Object component, int priority) { + configImpl.register(component, priority); + return this; + } + + @Override + public RestClientBuilder register(Object component, Class<?>... contracts) { + configImpl.register(component, contracts); + return this; + } + + @Override + public RestClientBuilder register(Object component, Map<Class<?>, Integer> contracts) { + configImpl.register(component, contracts); + return this; + } +**/ +} diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityClientReqFilter.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityClientReqFilter.java new file mode 100644 index 00000000000..0f1ebf6818c --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityClientReqFilter.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client.mock; + +import java.io.IOException; + +import javax.annotation.Priority; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.core.Response; + +@Priority(10) +public class HighPriorityClientReqFilter implements ClientRequestFilter { + + @Override + public void filter(ClientRequestContext paramClientRequestContext) throws IOException { + paramClientRequestContext.abortWith(Response.ok("high").build()); + } + +} diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityMBW.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityMBW.java new file mode 100644 index 00000000000..65cc8596b20 --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/HighPriorityMBW.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client.mock; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.annotation.Priority; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; + +@Priority(10) +public class HighPriorityMBW implements MessageBodyWriter<MyObject> { + + private final int id; + + public HighPriorityMBW() { + this(0); + } + + public HighPriorityMBW(int id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + return o instanceof HighPriorityMBW && ((HighPriorityMBW)o).id == this.id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public void writeTo(MyObject t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, + WebApplicationException { + entityStream.write(t.toString().getBytes()); + } +} diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/LowPriorityClientReqFilter.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/LowPriorityClientReqFilter.java new file mode 100644 index 00000000000..67a73b30cd2 --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/LowPriorityClientReqFilter.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client.mock; + +import java.io.IOException; + +import javax.annotation.Priority; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.core.Response; + +@Priority(6000) +public class LowPriorityClientReqFilter implements ClientRequestFilter { + + @Override + public void filter(ClientRequestContext paramClientRequestContext) throws IOException { + paramClientRequestContext.abortWith(Response.ok("low").build()); + } + +} diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyClient.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyClient.java new file mode 100644 index 00000000000..c3808456f6e --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyClient.java @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client.mock; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +@Path("/") +public interface MyClient { + @GET + Response get(); +} diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyObject.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyObject.java new file mode 100644 index 00000000000..5b56f7c3fdd --- /dev/null +++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/mock/MyObject.java @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.microprofile.client.mock; + +public class MyObject { + private String name; + private int id; + + public MyObject(String name, int id) { + this.name = name; + this.id = id; + } + + @Override + public String toString() { + return "name=\"" + name + "\" : id=" + id; + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services