View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.gbif.ws.client;
15  
16  import java.lang.reflect.Type;
17  
18  import com.fasterxml.jackson.databind.ObjectMapper;
19  
20  import feign.RequestTemplate;
21  import feign.codec.EncodeException;
22  import feign.codec.Encoder;
23  import feign.jackson.JacksonEncoder;
24  
25  public class ClientEncoder implements Encoder {
26  
27    private JacksonEncoder jacksonEncoder;
28  
29    public ClientEncoder(ObjectMapper objectMapper) {
30      this.jacksonEncoder = new JacksonEncoder(objectMapper);
31    }
32  
33    @Override
34    public void encode(Object object, Type bodyType, RequestTemplate template)
35        throws EncodeException {
36      if (bodyType == String.class) {
37        template.body(object.toString());
38      } else if (bodyType == byte[].class) {
39        template.body((byte[]) object, null);
40      } else {
41        jacksonEncoder.encode(object, bodyType, template);
42      }
43    }
44  }