
org.xlcloud.rest.client.config.BaseWebResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-client Show documentation
Show all versions of rest-client Show documentation
This module provides base classes for creation of REST client (SDK).
Default client has preconfigured filters, Oauth2 token injection or even object mapper.
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* Licensed 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.xlcloud.rest.client.config;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
/**
* Provides common configuration for client implementations of XMS and XSA API
* including constants, names of resources and common configuration of REST
* client provided by method {@link #createResource()}.
*
* @author Tomek Adamczewski, AMG.net
* @author Piotr Kulasek-Szwed, AMG.net
*/
@ApplicationScoped
public abstract class BaseWebResource {
private static final Logger LOG = Logger.getLogger(BaseWebResource.class);
private static final int DEFAULT_READ_TIMEOUT = 3 * 60 * 1000;
private static final int DEFAULT_CONNECT_TIMEOUT = 3 * 60 * 1000;
// @Inject @ConfigParam
private Integer connectTimeout;
// @Inject @ConfigParam
private Integer readTimeout;
private final Client client;
public BaseWebResource() {
client = configureClient();
}
/**
* Produces web resource providing ready-to-use {{@link WebResource} ,
* defining its object mapping {{@link ObjectMapperProvider}), JSON parser (
* {@link JacksonJaxbJsonProvider}), way to handle exceptions (
* {@link ExceptionHandlingClientFilter}) and logging filter (
* {@link ClientLoggingFilter}).
*
* @return
*/
@Produces
public WebResource createResource() {
LOG.debug("Produced WebResource with Jersey client " + client.hashCode());
return client.resource(getApiUri());
}
protected Client configureClient() {
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(JacksonJaxbJsonProvider.class);
cc.getClasses().add(ObjectMapperProvider.class);
Client client = Client.create(cc);
client.setConnectTimeout(connectTimeout != null ? connectTimeout : DEFAULT_CONNECT_TIMEOUT);
client.setReadTimeout(readTimeout != null ? readTimeout : DEFAULT_READ_TIMEOUT);
client.addFilter(new ExceptionHandlingClientFilter());
client.addFilter(new ClientLoggingFilter());
LOG.debug("Created Jersey client " + client.hashCode());
return client;
}
public abstract String getApiUri();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy