msb http client
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / convert / jackson / JacksonRequestBodyConverter.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 com.fasterxml.jackson.databind.ObjectWriter;
32
33 import okhttp3.MediaType;
34 import okhttp3.RequestBody;
35 import retrofit2.Converter;
36
37 final class JacksonRequestBodyConverter<T> implements Converter<T, RequestBody> {
38   private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
39
40   private final ObjectWriter adapter;
41
42   JacksonRequestBodyConverter(ObjectWriter adapter) {
43     this.adapter = adapter;
44   }
45
46   @Override
47   public RequestBody convert(T value) throws IOException {
48     byte[] bytes = adapter.writeValueAsBytes(value);
49     return RequestBody.create(MEDIA_TYPE, bytes);
50   }
51 }