From df402790b4648c5360f1cc347c4224d12eac4652 Mon Sep 17 00:00:00 2001 From: Brian Long Date: Sun, 2 Oct 2022 17:50:48 -0400 Subject: [PATCH] added Jersey impl --- README.md | 67 ++++++++++- pom.xml | 59 +++++++++- .../rs/ClientJerseyConfiguration.java | 35 ++++++ .../com/inteligr8/rs/ClientJerseyImpl.java | 105 ++++++++++++++++++ 4 files changed, 260 insertions(+), 6 deletions(-) create mode 100644 src/main/jersey/com/inteligr8/rs/ClientJerseyConfiguration.java create mode 100644 src/main/jersey/com/inteligr8/rs/ClientJerseyImpl.java diff --git a/README.md b/README.md index 3c80237..4e3f6a3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Common ReST Client Library -This project provides a library for Spring and POJO-based REST client instantiation. +This project provides a library for Spring and POJO-based REST client instantiation. It includes special classes for the Jersey JAXRS-based client frameworks. ## Usage @@ -15,7 +15,7 @@ First, you will need to include the library in your project. com.inteligr8 common-rest-client - ... + ...-jersey ... @@ -23,4 +23,65 @@ First, you will need to include the library in your project. ``` -See the `cxf` and `jersey` branches for examples and more documentation. +### Spring Framework + +#### Single Client + +If you will only be declaring a single client in your Spring context, this is easy. You will just need two things. First, inject the single client into any of your Spring beans. You may do inject it into more than one. An example is below: + +```java +@Component +public class ... { + @Autowired + @Qualifier("client.jersey") // may be optional + private Client client; +} +``` + +Next, you need to configure that client. You can do that by providing a single implementation of the `ClientConfiguration` (or `ClientJerseyConfiguration`) interface. + +```java +@Configuration +public class ... implements ClientJerseyConfiguration { + ... +} +``` + +#### Multiple Clients + +If you will or may have multiple clients in your Spring context, there is an extra step. You will still need to define a `ClientConfiguration` for each. On top of that, you will need to create specialized implementations of each client. That special implementation will reference the configuration directly. An example is below. + +```java +@Component("my.client") +public class MyClient extends ClientJerseyImpl { + @Autowired + public MyClient(MyClientConfiguration config) { + super(config); + } +} +``` + +You can then inject your client(s) into your Spring beans. Like the example below: + +```java +@Component +public class ... { + @Autowired + private MyClient client; + + @PostConstruct + public void init() { + MyJaxRsApi api = this.client.getApi(MyJaxRsApi.class); + } +} + +### POJO + +You do not have to use the Spring framework to use these classes. You can instantiate them directly. But you wil still need to create a `ClientConfiguration` as mentioned above. + +```java +MyClientConfiguration config = new MyClientConfiguration(); +... +ClientJerseyImpl client = new ClientJerseyImpl(config); +MyJaxRsApi api = client.getApi(MyJaxRsApi.class); +``` diff --git a/pom.xml b/pom.xml index f514d94..a756cd6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,11 +6,11 @@ com.inteligr8 common-rest-client - 2.0-SNAPSHOT + 2.0-SNAPSHOT-jersey jar ReST API Client for Java - A common library for building REST API clients + A common library for building Jersey REST API clients https://bitbucket.org/inteligr8/common-rest-client @@ -46,7 +46,6 @@ 5.7.2 5.2.14.RELEASE 2.35 - 3.3.2 @@ -98,10 +97,64 @@ 4.5.9 test + + + + org.glassfish.jersey.ext + jersey-proxy-client + ${jersey.version} + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + + + org.glassfish.jersey.media + jersey-media-multipart + ${jersey.version} + + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey.version} + test + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${jersey.version} + test + + + org.codehaus.mojo + build-helper-maven-plugin + 3.2.0 + + + add-jaxrs-src + add-source + + + src/main/jersey + + + + + add-test-src + add-test-source + + + src/test/jersey + + + + + maven-surefire-plugin 3.0.0-M5 diff --git a/src/main/jersey/com/inteligr8/rs/ClientJerseyConfiguration.java b/src/main/jersey/com/inteligr8/rs/ClientJerseyConfiguration.java new file mode 100644 index 0000000..ffdff70 --- /dev/null +++ b/src/main/jersey/com/inteligr8/rs/ClientJerseyConfiguration.java @@ -0,0 +1,35 @@ +/* + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package com.inteligr8.rs; + +/** + * This interface defines additional configurations specific to the Jersey + * JAX-RS library and its nuances. + * + * @author brian@inteligr8.com + */ +public interface ClientJerseyConfiguration extends ClientConfiguration { + + /** + * Jersey is automatically strict in its adherence to the ReST API + * specifications. It requires a body to PUT calls by default. + * + * @return true to require body in PUT calls; false to make it optional + */ + default boolean isPutBodyRequired() { + return true; + } + +} diff --git a/src/main/jersey/com/inteligr8/rs/ClientJerseyImpl.java b/src/main/jersey/com/inteligr8/rs/ClientJerseyImpl.java new file mode 100644 index 0000000..98514c2 --- /dev/null +++ b/src/main/jersey/com/inteligr8/rs/ClientJerseyImpl.java @@ -0,0 +1,105 @@ +/* + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package com.inteligr8.rs; + +import javax.annotation.PostConstruct; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.ext.RuntimeDelegate; + +import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.proxy.WebResourceFactory; +import org.glassfish.jersey.internal.RuntimeDelegateImpl; +import org.glassfish.jersey.media.multipart.MultiPartFeature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * A class that provides pre-configured JAX-RS Client & WebTarget objects + * for Jersey. + * + * @author brian@inteligr8.com + */ +@Component("client.jersey") +public class ClientJerseyImpl extends Client { + + private final Logger logger = LoggerFactory.getLogger(ClientJerseyImpl.class); + + private ClientJerseyConfiguration config; + + /** + * This constructor is for Spring or POJO use. + * @param config The client configuration. + */ + @Autowired + public ClientJerseyImpl(ClientJerseyConfiguration config) { + this.config = config; + } + + /** + * This method registers the Jersey library as the default provider for the + * JAX-RS specification. + */ + @PostConstruct + public void register() { + if (RuntimeDelegate.getInstance() == null) { + this.logger.info("Setting JAX-RS runtime delegate to the Jersey library"); + RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); + } else if (RuntimeDelegate.getInstance() instanceof RuntimeDelegateImpl) { + this.logger.info("JAX-RS runtime delegate already the Jersey library"); + } else { + this.logger.warn("Setting JAX-RS runtime delegate to the Jersey library; was: " + RuntimeDelegate.getInstance().getClass().getName()); + RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); + } + + if (this.logger.isInfoEnabled()) + this.logger.info("API Base URL: " + this.getConfig().getBaseUrl()); + } + + /** + * @param clientBuilder A client builder. + */ + @Override + public void buildClient(ClientBuilder clientBuilder) { + clientBuilder.register(MultiPartFeature.class); + + if (!this.getConfig().isPutBodyRequired()) { + // allow PUT operations without body data + clientBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true); + } + } + + /** + * @return The client configuration. + */ + public ClientJerseyConfiguration getConfig() { + return this.config; + } + + /** + * This method retrieves a JAX-RS implementation of the specified API with + * the specified authorization. + * + * @param authFilter A dynamic authorization filter. + * @param apiClass A JAX-RS annotation API class. + * @return An instance of the API class. + */ + @Override + public T getApi(AuthorizationFilter authFilter, Class apiClass) { + return WebResourceFactory.newResource(apiClass, this.getTarget(authFilter)); + } + +}