Commit c9dc8f51 by Spencer Gibb

use boots HttpMessageConverters class rather than copied list of converters

parent e52949e5
package org.springframework.cloud.netflix.feign;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.xml.transform.Source;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.feed.AtomFeedHttpMessageConverter;
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* @author Spencer Gibb
*/
public class FeignBase {
public static final boolean romePresent =
ClassUtils.isPresent("com.sun.syndication.feed.WireFeed", FeignBase.class.getClassLoader());
public static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", FeignBase.class.getClassLoader());
public static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", FeignBase.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", FeignBase.class.getClassLoader());
public static final boolean jacksonPresent =
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", FeignBase.class.getClassLoader()) &&
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", FeignBase.class.getClassLoader());
protected final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
public FeignBase() {
addDefaultConverters(messageConverters);
}
/**
* Create a new instance of the {@link SpringDecoder} using the given list of
* {@link HttpMessageConverter} to use
* @param messageConverters the list of {@link HttpMessageConverter} to use
*/
public FeignBase(List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
this.messageConverters.addAll(messageConverters);
}
@SuppressWarnings("deprecation")
protected void addDefaultConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
messageConverters.add(new MappingJackson2HttpMessageConverter());
}
else if (jacksonPresent) {
messageConverters.add(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter());
}
}
/**
* Set the message body converters to use.
* <p>These converters are used to convert from and to HTTP requests and responses.
*/
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
this.messageConverters.clear();
this.messageConverters.addAll(messageConverters);
}
public List<HttpMessageConverter<?>> getMessageConverters() {
return messageConverters;
}
protected HttpHeaders getHttpHeaders(Map<String, Collection<String>> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
httpHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
return httpHeaders;
}
}
package org.springframework.cloud.netflix.feign;
import org.springframework.http.HttpHeaders;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
/**
* @author Spencer Gibb
*/
public class FeignUtils {
static HttpHeaders getHttpHeaders(Map<String, Collection<String>> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
httpHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
return httpHeaders;
}
}
......@@ -3,12 +3,12 @@ package org.springframework.cloud.netflix.feign;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.HttpMessageConverterExtractor;
import feign.FeignException;
......@@ -16,25 +16,25 @@ import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import static org.springframework.cloud.netflix.feign.FeignUtils.getHttpHeaders;
/**
* @author Spencer Gibb
*/
public class SpringDecoder extends FeignBase implements Decoder {
public class SpringDecoder implements Decoder {
public SpringDecoder() {
}
@Autowired
HttpMessageConverters messageConverters;
public SpringDecoder(List<HttpMessageConverter<?>> messageConverters) {
super(messageConverters);
public SpringDecoder() {
}
@Override
public Object decode(final Response response, Type type) throws IOException,
DecodeException, FeignException {
public Object decode(final Response response, Type type) throws IOException, FeignException {
if (type instanceof Class) {
@SuppressWarnings({ "unchecked", "rawtypes" })
HttpMessageConverterExtractor<?> extractor = new HttpMessageConverterExtractor(
(Class<?>) type, getMessageConverters());
(Class<?>) type, messageConverters.getConverters());
Object data = extractor.extractData(new FeignResponseAdapter(response));
return data;
......
......@@ -8,6 +8,8 @@ import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
......@@ -17,19 +19,19 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import static org.springframework.cloud.netflix.feign.FeignUtils.getHttpHeaders;
/**
* @author Spencer Gibb
*/
public class SpringEncoder extends FeignBase implements Encoder {
public class SpringEncoder implements Encoder {
private static final Logger logger = LoggerFactory.getLogger(SpringEncoder.class);
public SpringEncoder() {
}
@Autowired
HttpMessageConverters messageConverters;
public SpringEncoder(List<HttpMessageConverter<?>> messageConverters) {
super(messageConverters);
public SpringEncoder() {
}
@Override
......@@ -46,7 +48,7 @@ public class SpringEncoder extends FeignBase implements Encoder {
requestContentType = MediaType.valueOf(type);
}
for (HttpMessageConverter<?> messageConverter : getMessageConverters()) {
for (HttpMessageConverter<?> messageConverter : messageConverters.getConverters()) {
if (messageConverter.canWrite(requestType, requestContentType)) {
if (logger.isDebugEnabled()) {
if (requestContentType != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment