Commit b6079a23 authored by Brian Long's avatar Brian Long
Browse files

removed CXF/Jersey

parent 91398d23
Loading
Loading
Loading
Loading
+2 −68
Original line number Diff line number Diff line

# Common ReST Client Library

This project provides a library for Spring and POJO-based REST client instantiation.  It includes special classes with classifiers for two popular JAXRS-based client frameworks: Apache CXF and Jersey.
This project provides a library for Spring and POJO-based REST client instantiation.

## Usage

@@ -15,7 +15,6 @@ First, you will need to include the library in your project.
        <dependency>
            <groupId>com.inteligr8</groupId>
            <artifactId>common-rest-client</artifactId>
            <classifier>...</classifier>
            <version>...</version>
        </dependency>
        ...
@@ -24,69 +23,4 @@ First, you will need to include the library in your project.
</project>
```

Valid `classifier` values are `cxf` or `jersey`.

### 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.cxf")  // 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 `ClientCxfConfiguration`) interface.

```java
@Configuration
public class ... implements ClientCxfConfiguration {
    ...
}
```

For Jersey implementations, just use `client.jersey` and `ClientJerseyConfiguration`.  If you want to provide one of each, then follow the instructions for multiple clients below.

#### 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 ClientCxfImpl {
    @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();
...
ClientCxfImpl client = new ClientCxfImpl(config);
MyJaxRsApi api = client.getApi(MyJaxRsApi.class);
```
See the `cxf` and `jersey` branches for examples and more documentation.

pom-release.xml

deleted100644 → 0
+0 −162
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>com.inteligr8</groupId>
	<artifactId>common-rest-client</artifactId>
	<version>2.0-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>ReST API Client for Java</name>
	<description>A common library for building REST API clients that support both the CXF and Jersey frameworks</description>
	<url>https://bitbucket.org/inteligr8/common-rest-client</url>

	<licenses>
		<license>
			<name>GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007</name>
			<url>https://www.gnu.org/licenses/lgpl-3.0.txt</url>
		</license>
	</licenses>

	<scm>
		<connection>scm:git:https://bitbucket.org/inteligr8/common-rest-client.git</connection>
		<developerConnection>scm:git:git@bitbucket.org:inteligr8/common-rest-client.git</developerConnection>
		<url>https://bitbucket.org/inteligr8/common-rest-client</url>
	</scm>
	<organization>
		<name>Inteligr8</name>
		<url>https://www.inteligr8.com</url>
	</organization>
	<developers>
		<developer>
			<id>brian.long</id>
			<name>Brian Long</name>
			<email>brian@inteligr8.com</email>
			<url>https://twitter.com/brianmlong</url>
		</developer>
	</developers>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-invoker-plugin</artifactId>
				<version>3.3.0</version>
				<configuration>
					<goals><goal>package</goal></goals>
					<pom>pom.xml</pom>
					<streamLogs>true</streamLogs>
				</configuration>
				<executions>
					<execution>
						<id>cxf</id>
						<phase>package</phase>
						<goals><goal>run</goal></goals>
						<configuration>
							<properties>
								<jaxrs.impl>cxf</jaxrs.impl>
							</properties>
						</configuration>
					</execution>
					<execution>
						<id>jersey</id>
						<phase>package</phase>
						<goals><goal>run</goal></goals>
						<configuration>
							<properties>
								<jaxrs.impl>jersey</jaxrs.impl>
							</properties>
						</configuration>
					</execution>
					<execution>
						<id>common</id>
						<phase>package</phase>
						<goals><goal>run</goal></goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>3.3.0</version>
				<executions>
					<execution>
						<id>attach-artifacts</id>
						<phase>package</phase>
						<goals><goal>attach-artifact</goal></goals>
						<configuration>
							<artifacts>
								<artifact>
									<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
									<type>jar</type>
								</artifact>
								<artifact>
									<file>${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar</file>
									<classifier>javadoc</classifier>
									<type>jar</type>
								</artifact>
								<artifact>
									<file>${project.build.directory}/${project.artifactId}-${project.version}-sources.jar</file>
									<classifier>sources</classifier>
									<type>jar</type>
								</artifact>
								<artifact>
									<file>${project.build.directory}/${project.artifactId}-${project.version}-cxf.jar</file>
									<classifier>cxf</classifier>
									<type>jar</type>
								</artifact>
								<artifact>
									<file>${project.build.directory}/${project.artifactId}-${project.version}-jersey.jar</file>
									<classifier>jersey</classifier>
									<type>jar</type>
								</artifact>
							</artifacts>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-install-plugin</artifactId>
				<configuration>
					<pomFile>pom.xml</pomFile>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<profiles>
		<profile>
			<id>ossrh-release</id>
			<build>
				<plugins>
					<plugin>
						<artifactId>maven-gpg-plugin</artifactId>
						<executions>
							<execution>
								<id>sign</id>
								<phase>verify</phase>
								<goals><goal>sign</goal></goals>
							</execution>
						</executions>
					</plugin>
					<plugin>
						<groupId>org.sonatype.plugins</groupId>
						<artifactId>nexus-staging-maven-plugin</artifactId>
						<version>1.6.13</version>
						<extensions>true</extensions>
						<configuration>
							<serverId>ossrh</serverId>
							<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
							<autoReleaseAfterClose>true</autoReleaseAfterClose>
						</configuration>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>
</project>
+1 −95
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
	<packaging>jar</packaging>

	<name>ReST API Client for Java</name>
	<description>A common library for building REST API clients that support both the CXF and Jersey frameworks</description>
	<description>A common library for building REST API clients</description>
	<url>https://bitbucket.org/inteligr8/common-rest-client</url>

	<licenses>
@@ -102,37 +102,6 @@
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>3.2.0</version>
				<executions>
					<execution>
						<id>add-jaxrs-src</id>
						<goals><goal>add-source</goal></goals>
						<configuration>
							<sources>
								<source>src/main/${jaxrs.impl}</source>
							</sources>
						</configuration>
					</execution>
					<execution>
						<id>add-test-src</id>
						<goals><goal>add-test-source</goal></goals>
						<configuration>
							<sources>
								<source>src/test/${jaxrs.impl}</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<classifier>${jaxrs.impl}</classifier>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M5</version>
@@ -180,67 +149,4 @@
			</plugin>
		</plugins>
	</build>
	
	<profiles>
		<profile>
			<id>jaxrs-jersey</id>
			<activation>
				<property>
					<name>jaxrs.impl</name>
					<value>jersey</value>
				</property>
			</activation>
			<properties>
				<jaxrs.impl>jersey</jaxrs.impl>
			</properties>
			<dependencies>
				<dependency>
					<groupId>org.glassfish.jersey.ext</groupId>
					<artifactId>jersey-proxy-client</artifactId>
					<version>${jersey.version}</version>
				</dependency>
				<dependency>
					<groupId>org.glassfish.jersey.core</groupId>
					<artifactId>jersey-client</artifactId>
					<version>${jersey.version}</version>
				</dependency>
				<dependency>
					<groupId>org.glassfish.jersey.media</groupId>
					<artifactId>jersey-media-multipart</artifactId>
					<version>${jersey.version}</version>
				</dependency>
				<dependency>
					<groupId>org.glassfish.jersey.inject</groupId>
					<artifactId>jersey-hk2</artifactId>
					<version>${jersey.version}</version>
					<scope>test</scope>
				</dependency>
				<dependency>
					<groupId>org.glassfish.jersey.media</groupId>
					<artifactId>jersey-media-json-jackson</artifactId>
					<version>${jersey.version}</version>
					<scope>test</scope>
				</dependency>
			</dependencies>
		</profile>
		<profile>
			<id>jaxrs-cxf</id>
			<activation>
				<property>
					<name>jaxrs.impl</name>
					<value>cxf</value>
				</property>
			</activation>
			<properties>
				<jaxrs.impl>cxf</jaxrs.impl>
			</properties>
			<dependencies>
				<dependency>
					<groupId>org.apache.cxf</groupId>
					<artifactId>cxf-rt-rs-client</artifactId>
					<version>${cxf.version}</version>
				</dependency>
			</dependencies>
		</profile>
	</profiles>
</project>
+0 −38
Original line number Diff line number Diff line
/*
 * 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 <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.rs;

/**
 * This interface defines additional configurations specific to the Apache CXF
 * JAX-RS library and its nuances.
 * 
 * @author brian@inteligr8.com
 */
public interface ClientCxfConfiguration extends ClientConfiguration {
	
	/**
	 * Apache CXF uses a global bus configuration where interceptors could
	 * wreck havoc on your implementation.  This method allows you to
	 * explicitly by-pass the default bus.
	 * 
	 * See https://cxf.apache.org/docs/bus-configuration.html.
	 * 
	 * @return true to use the default bus; false otherwise.
	 */
	default boolean isDefaultBusEnabled() {
		return true;
	}

}
+0 −142
Original line number Diff line number Diff line
/*
 * 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 <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.rs;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ws.rs.ext.RuntimeDelegate;

import org.apache.cxf.BusFactory;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

/**
 * A class that provides pre-configured JAX-RS Client &amp; WebTarget &amp;
 * CXF WebClient objects.
 * 
 * @author brian@inteligr8.com
 */
@Component("client.cxf")
public class ClientCxfImpl extends Client {
	
	private final Logger logger = LoggerFactory.getLogger(ClientCxfImpl.class);
	
	private ClientCxfConfiguration config;
	
	/**
	 * This constructor is for Spring or POJO use.
	 * @param config The client configuration.
	 */
	@Autowired
	public ClientCxfImpl(ClientCxfConfiguration config) {
		this.config = config;
	}
	
	/**
	 * This method registers the Apache CXF 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 CXF library");
			RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
		} else if (RuntimeDelegate.getInstance() instanceof RuntimeDelegateImpl) {
			this.logger.info("JAX-RS runtime delegate already the CXF library");
		} else {
			this.logger.warn("Setting JAX-RS runtime delegate to the CXF library; was: " + RuntimeDelegate.getInstance().getClass().getName());
			RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
		}
		
		if (this.logger.isInfoEnabled())
			this.logger.info("API Base URL: " + this.getConfig().getBaseUrl());
	}
	
	/**
	 * @return A CXF client (not JAX-RS).
	 */
	public WebClient getCxfClient() {
		return this.getCxfClient(null);
	}
	
	/**
	 * @param authFilter A post-configuration authorization filter.
	 * @return A CXF client (not JAX-RS).
	 */
	public WebClient getCxfClient(AuthorizationFilter authFilter) {
		List<Object> providersAndFilters = new LinkedList<Object>();
		providersAndFilters.add(new JacksonJaxbJsonProvider());
		providersAndFilters.add(new CxfLoggingFilter());
		providersAndFilters.add(new CxfMultipartProvider());
		
		if (authFilter == null)
			authFilter = this.getConfig().createAuthorizationFilter();
		if (authFilter != null)
			providersAndFilters.add(authFilter);
		
		this.addProvidersAndFilters(providersAndFilters);

		// we can't use JAXRSClientFactory with a JAXRS client (duh!)
		// so we need to create a CXF client
		WebClient client = WebClient.create(this.getConfig().getBaseUrl(), providersAndFilters);

		if (!this.getConfig().isDefaultBusEnabled()) {
			// Some applications (like ACS) add interceptors to the default bus
			// those interceptors may treat all messages as SOAP messages (like ACS), resulting in ClassCastExceptions
			// we need to ignore the default bus
			org.apache.cxf.jaxrs.client.ClientConfiguration config = WebClient.getConfig(client);
			config.setBus(BusFactory.newInstance().createBus());
		}
		
		return client;
	}
	
	/**
	 * @param providersAndFilters A list of JAX-RS and CXF providers.
	 */
	public void addProvidersAndFilters(List<Object> providersAndFilters) {
		// for extension purposes
	}
	
	/**
	 * @return The client configuration.
	 */
	public ClientCxfConfiguration 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> T getApi(AuthorizationFilter authFilter, Class<T> apiClass) {
		return JAXRSClientFactory.fromClient(this.getCxfClient(authFilter), apiClass);
	}

}
Loading