com.jslsolucoes.jax.rs.client.se.api.impl.DefaultJaxRsApiClient Maven / Gradle / Ivy
package com.jslsolucoes.jax.rs.client.se.api.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClient;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientRequest;
import com.jslsolucoes.jax.rs.provider.se.JaxRsObjectMapper;
import com.jslsolucoes.jax.rs.provider.se.JsonContentTypeResponseFilter;
public class DefaultJaxRsApiClient implements JaxRsApiClient {
private JerseyClient jerseyClient;
private String target;
private List paths = new ArrayList<>();
private Map queryParams = new HashMap<>();
private Integer connectTimeout = 10000;
private Integer readTimeout = 20000;
private Boolean debug = Boolean.FALSE;
private Level level = Level.INFO;
private static final Logger logger = LoggerFactory.getLogger(DefaultJaxRsApiClient.class);
@Override
public JaxRsApiClientRequest request() {
return new DefaultJaxRsApiClientRequest(this,webTarget());
}
private WebTarget webTarget() {
WebTarget webTarget = jerseyClient().target(target);
for (String path : Clonables.clone(paths)) {
webTarget = webTarget.path(path);
}
for (Entry entry : Clonables.clone(queryParams).entrySet()) {
webTarget = webTarget.queryParam(entry.getKey(), entry.getValue());
}
return webTarget;
}
private Client jerseyClient() {
if(jerseyClient == null) {
jerseyClient = JerseyClientBuilder.createClient(jerseyClientConfig());
logger.debug("Opening client {} ...",jerseyClient);
}
return jerseyClient;
}
private ClientConfig jerseyClientConfig() {
ClientConfig clientConfig = new ClientConfig()
.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout)
.property(ClientProperties.READ_TIMEOUT, readTimeout)
.register(new JsonContentTypeResponseFilter())
.register(new JaxRsObjectMapper())
.register(MultiPartFeature.class);
if(debug) {
clientConfig.register(new LoggingFeature(java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME),
level, LoggingFeature.Verbosity.PAYLOAD_ANY, 10000));
}
return clientConfig;
}
@Override
public JaxRsApiClient target(final String target) {
this.target = target;
return this;
}
@Override
public JaxRsApiClient path(final String path) {
this.paths.add(path);
return this;
}
@Override
public JaxRsApiClient queryParam(final String name, final Object value) {
return queryParam(name, new Object[] { value });
}
@Override
public JaxRsApiClient queryParam(final String name, final Object... values) {
queryParams.put(name, values);
return this;
}
@Override
public void close() {
if(jerseyClient != null && !jerseyClient.isClosed()) {
logger.debug("Closing client {} ...",jerseyClient);
jerseyClient.close();
} else {
logger.debug("Client not created or already closed");
}
}
@Override
public JaxRsApiClient connectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
@Override
public JaxRsApiClient readTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
return this;
}
@Override
public JaxRsApiClient debug(Boolean debug) {
this.debug = debug;
return this;
}
@Override
public JaxRsApiClient level(Level level) {
this.level = level;
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy