diff options
Diffstat (limited to 'src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java')
-rw-r--r-- | src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java new file mode 100644 index 0000000..4170cbd --- /dev/null +++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * 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. + ******************************************************************************/ +/* + * Copyright (C) 2015 Square, Inc. + * + * 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.onap.msb.sdk.httpclient.convert.jackson; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; + +import okhttp3.RequestBody; +import okhttp3.ResponseBody; +import retrofit2.Converter; +import retrofit2.Retrofit; + +/** + * A {@linkplain Converter.Factory converter} which uses Jackson. + * <p> + * Because Jackson is so flexible in the types it supports, this converter assumes that it can + * handle all types. If you are mixing JSON serialization with something else (such as protocol + * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this + * instance} last to allow the other converters a chance to see their types. + */ +public final class JacksonConverterFactory extends Converter.Factory { + /** Create an instance using a default {@link ObjectMapper} instance for conversion. */ + public static JacksonConverterFactory create() { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); + return create(mapper); + } + + /** Create an instance using {@code mapper} for conversion. */ + public static JacksonConverterFactory create(ObjectMapper mapper) { + return new JacksonConverterFactory(mapper); + } + + private final ObjectMapper mapper; + + private JacksonConverterFactory(ObjectMapper mapper) { + if (mapper == null) + throw new NullPointerException("mapper == null"); + this.mapper = mapper; + } + + @Override + public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, + Retrofit retrofit) { + JavaType javaType = mapper.getTypeFactory().constructType(type); + ObjectReader reader = mapper.reader(javaType); + return new JacksonResponseBodyConverter<>(reader); + } + + @Override + public Converter<?, RequestBody> requestBodyConverter(Type type, + Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { + JavaType javaType = mapper.getTypeFactory().constructType(type); + ObjectWriter writer = mapper.writerWithType(javaType); + return new JacksonRequestBodyConverter<>(writer); + } +} |