remove not required docs and .readthedocs.yaml
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / convert / jackson / JacksonConverterFactory.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 /*
15  * Copyright (C) 2015 Square, Inc.
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
18  * in compliance with the License. You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software distributed under the License
23  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
24  * or implied. See the License for the specific language governing permissions and limitations under
25  * the License.
26  */
27 package org.onap.msb.sdk.httpclient.convert.jackson;
28
29 import java.lang.annotation.Annotation;
30 import java.lang.reflect.Type;
31
32 import com.fasterxml.jackson.databind.DeserializationFeature;
33 import com.fasterxml.jackson.databind.JavaType;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.ObjectReader;
36 import com.fasterxml.jackson.databind.ObjectWriter;
37 import com.fasterxml.jackson.databind.SerializationFeature;
38
39 import okhttp3.RequestBody;
40 import okhttp3.ResponseBody;
41 import retrofit2.Converter;
42 import retrofit2.Retrofit;
43
44 /**
45  * A {@linkplain Converter.Factory converter} which uses Jackson.
46  * <p>
47  * Because Jackson is so flexible in the types it supports, this converter assumes that it can
48  * handle all types. If you are mixing JSON serialization with something else (such as protocol
49  * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this
50  * instance} last to allow the other converters a chance to see their types.
51  */
52 public final class JacksonConverterFactory extends Converter.Factory {
53   /** Create an instance using a default {@link ObjectMapper} instance for conversion. */
54   public static JacksonConverterFactory create() {
55     ObjectMapper mapper = new ObjectMapper();
56     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
57     mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
58     mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
59     return create(mapper);
60   }
61
62   /** Create an instance using {@code mapper} for conversion. */
63   public static JacksonConverterFactory create(ObjectMapper mapper) {
64     return new JacksonConverterFactory(mapper);
65   }
66
67   private final ObjectMapper mapper;
68
69   private JacksonConverterFactory(ObjectMapper mapper) {
70     if (mapper == null)
71       throw new NullPointerException("mapper == null");
72     this.mapper = mapper;
73   }
74
75   @Override
76   public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
77       Retrofit retrofit) {
78     JavaType javaType = mapper.getTypeFactory().constructType(type);
79     ObjectReader reader = mapper.reader(javaType);
80     return new JacksonResponseBodyConverter<>(reader);
81   }
82
83   @Override
84   public Converter<?, RequestBody> requestBodyConverter(Type type,
85       Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
86     JavaType javaType = mapper.getTypeFactory().constructType(type);
87     ObjectWriter writer = mapper.writerWithType(javaType);
88     return new JacksonRequestBodyConverter<>(writer);
89   }
90 }