11
zhouwei
2019-07-12 143f7be25ff19896e70ffc486999a64a3bc3b76f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.safeluck.aaej.app.utils.feign;
 
 
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.safeluck.aaej.app.utils.PayJsonResult;
import com.safeluck.aaej.app.utils.PayMessageEncryptor;
import com.safeluck.aaej.base.utils.DefaultGson;
import feign.Response;
import feign.Util;
import feign.codec.Decoder;
import ikidou.reflect.TypeBuilder;
import org.springframework.util.StreamUtils;
 
import java.io.IOException;
import java.lang.reflect.Type;
 
public class FeignGsonDecoder implements Decoder {
    private Gson gson = DefaultGson.get();
 
    PayMessageEncryptor payMessageEncryptor = new PayMessageEncryptor();
 
    public Object decode(Response response, Type type) throws IOException {
        if (response.status() == 404) {
            return Util.emptyValueOf(type);
        } else if (response.body() == null) {
            return null;
        } else {
//            Reader reader = response.body().asReader();
 
//            PayJsonResult var4;
            try {
                byte[] bytes = StreamUtils.copyToByteArray(response.body().asInputStream());
                String b64_string = payMessageEncryptor.decrypt(bytes);
 
//                String b64_string = new String(encrypt_bytes,"utf-8");
                if(type==String.class)
                {
                    return b64_string;
                }
                Type newtype = TypeBuilder.newInstance(PayJsonResult.class).addTypeParam(type).build();
 
                PayJsonResult var4 = this.gson.fromJson(b64_string, newtype);
                if(var4.getErrorFlag())
                {
                    throw new RuntimeException(var4.getErrorMessage());
                }
                return var4.getData();
//                var4 = this.gson.fromJson(reader, type);
            } catch (JsonIOException var8) {
                if (var8.getCause() != null && var8.getCause() instanceof IOException) {
                    throw (IOException)IOException.class.cast(var8.getCause());
                }
 
                throw var8;
            } finally {
//                Util.ensureClosed(reader);
            }
 
 
        }
    }
}