msb http client
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / convert / jackson / JacksonResponseBodyConverter.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.io.IOException;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.fasterxml.jackson.databind.ObjectReader;
35
36 import okhttp3.ResponseBody;
37 import retrofit2.Converter;
38
39 final class JacksonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
40
41   private static final Logger logger = LoggerFactory.getLogger(JacksonResponseBodyConverter.class);
42
43   private final ObjectReader adapter;
44
45
46
47   JacksonResponseBodyConverter(ObjectReader adapter) {
48     this.adapter = adapter;
49   }
50
51   @Override
52   public T convert(ResponseBody value) throws IOException {
53     String src = null;
54     try {
55       src = value.string();
56       return adapter.readValue(src);
57     } catch (IOException e) {
58       logger.error("parse responseBody error,body:" + src);
59       throw e;
60     } finally {
61       value.close();
62     }
63   }
64 }