79 lines
2.4 KiB
Java
79 lines
2.4 KiB
Java
package com.inteligr8.rs;
|
|
|
|
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.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
/**
|
|
* A class that provides pre-configured JAX-RS Client & WebTarget objects
|
|
* for Jersey.
|
|
*
|
|
* @author brian@inteligr8.com
|
|
*/
|
|
public abstract class ClientJerseyImpl extends Client implements InitializingBean {
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(ClientJerseyImpl.class);
|
|
|
|
@Override
|
|
protected abstract ClientJerseyConfiguration getConfig();
|
|
|
|
@Override
|
|
public void afterPropertiesSet() {
|
|
this.register();
|
|
}
|
|
|
|
/**
|
|
* This method registers the Jersey library as the default provider for the
|
|
* JAX-RS specification.
|
|
*/
|
|
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 authFilter A post-configuration authorization filter.
|
|
* @return A JAX-RS client.
|
|
*/
|
|
@Override
|
|
public javax.ws.rs.client.Client getClient(AuthorizationFilter authFilter) {
|
|
javax.ws.rs.client.Client client = super.getClient(authFilter);
|
|
|
|
if (!this.getConfig().isPutBodyRequired()) {
|
|
// allow PUT operations without body data
|
|
client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
|
|
}
|
|
|
|
return client;
|
|
}
|
|
|
|
/**
|
|
* 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 WebResourceFactory.newResource(apiClass, this.getTarget(authFilter));
|
|
}
|
|
|
|
}
|