View Javadoc
1   package com.foxinmy.weixin4j.http.weixin;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import com.alibaba.fastjson.JSONObject;
9   import com.alibaba.fastjson.TypeReference;
10  import com.foxinmy.weixin4j.http.HttpHeaders;
11  import com.foxinmy.weixin4j.http.HttpResponse;
12  import com.foxinmy.weixin4j.http.HttpStatus;
13  import com.foxinmy.weixin4j.http.HttpVersion;
14  import com.foxinmy.weixin4j.http.message.JsonMessageConverter;
15  import com.foxinmy.weixin4j.http.message.MessageConverter;
16  import com.foxinmy.weixin4j.http.message.XmlMessageConverter;
17  import com.foxinmy.weixin4j.util.StringUtil;
18  
19  /**
20   * 调用微信接口响应
21   * 
22   * @className WeixinResponse
23   * @author jinyu
24   * @date Jul 21, 2016
25   * @since JDK 1.6
26   */
27  public class WeixinResponse implements HttpResponse {
28  
29  	private volatile String text;
30  	private final HttpResponse response;
31  	private static List<MessageConverter> messageConverters = new ArrayList<MessageConverter>();
32  	private final TypeReference<ApiResult> APIRESULT_CLAZZ = new TypeReference<ApiResult>() {
33  	};
34  	private final TypeReference<XmlResult> XMLRESULT_CLAZZ = new TypeReference<XmlResult>() {
35  	};
36  	private final TypeReference<JSONObject> JSONOBJECT_CLAZZ = new TypeReference<JSONObject>() {
37  	};
38  
39  	static {
40  		messageConverters.add(new JsonMessageConverter());
41  		messageConverters.add(new XmlMessageConverter());
42  	}
43  
44  	public WeixinResponse(HttpResponse response) {
45  		this.response = response;
46  	}
47  
48  	public String getAsString() {
49  		if (text == null) {
50  			text = StringUtil.newStringUtf8(getContent());
51  		}
52  		return text;
53  	}
54  
55  	public ApiResult getAsResult() {
56  		return getAsObject(APIRESULT_CLAZZ);
57  	}
58  
59  	public JSONObject getAsJson() {
60  		return getAsObject(JSONOBJECT_CLAZZ);
61  	}
62  
63  	public XmlResult getAsXml() {
64  		return getAsObject(XMLRESULT_CLAZZ);
65  	}
66  
67  	@SuppressWarnings("unchecked")
68  	public <T> T getAsObject(TypeReference<T> typeReference) {
69  		Class<T> clazz = (Class<T>) typeReference.getType();
70  		for (MessageConverter messageConverter : messageConverters) {
71  			if (messageConverter.canConvert(clazz, response)) {
72  				try {
73  					return messageConverter.convert(clazz, response);
74  				} catch (IOException e) {
75  					throw new RuntimeException("IO error on convert to "
76  							+ clazz, e);
77  				}
78  			}
79  		}
80  		if (clazz.isAssignableFrom(ApiResult.class)) {
81  			return (T) new ApiResult();
82  		}
83  		throw new RuntimeException("cannot convert to " + clazz);
84  	}
85  
86  	@Override
87  	public HttpHeaders getHeaders() {
88  		return response.getHeaders();
89  	}
90  
91  	@Override
92  	public HttpStatus getStatus() {
93  		return response.getStatus();
94  	}
95  
96  	@Override
97  	public byte[] getContent() {
98  		return response.getContent();
99  	}
100 
101 	@Override
102 	public InputStream getBody() {
103 		return response.getBody();
104 	}
105 
106 	@Override
107 	public HttpVersion getProtocol() {
108 		return response.getProtocol();
109 	}
110 
111 	@Override
112 	public void close() {
113 		response.close();
114 	}
115 }