This is an automated email from the ASF dual-hosted git repository. johndament pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 637391f93ee921240d722764114f2dbdd3683076 Author: Andy McCright <[email protected]> AuthorDate: Wed Dec 6 19:05:08 2017 -0600 Config handling for MP rest client (#352) --- .../client/CxfTypeSafeClientBuilder.java | 54 +++++++------ .../client/CxfTypeSafeClientBuilderTest.java | 94 ++++++++++++++++++++++ .../client/mock/HighPriorityClientReqFilter.java | 36 +++++++++ .../microprofile/client/mock/HighPriorityMBW.java | 69 ++++++++++++++++ .../client/mock/LowPriorityClientReqFilter.java | 36 +++++++++ .../cxf/microprofile/client/mock/MyClient.java | 29 +++++++ .../cxf/microprofile/client/mock/MyObject.java | 34 ++++++++ 7 files changed, 329 insertions(+), 23 deletions(-) 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 4e231aa..8fc7b54 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 @@ package org.apache.cxf.microprofile.client; 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 class CxfTypeSafeClientBuilder implements RestClientBuilder { @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 class CxfTypeSafeClientBuilder implements RestClientBuilder { 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 0000000..8d6d1a0 --- /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 0000000..0f1ebf6 --- /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 0000000..65cc859 --- /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 0000000..67a73b3 --- /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 0000000..c380845 --- /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 0000000..5b56f7c --- /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; + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
