All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weixin.popular.client.XmlResponseHandler Maven / Gradle / Ivy

Go to download

The weixin-popular is a JAVA SDK for weixin. Weixin web url is https://mp.weixin.qq.com.

There is a newer version: 2.8.43
Show newest version
package weixin.popular.client;

import java.io.IOException;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import weixin.popular.bean.DynamicField;
import weixin.popular.bean.paymch.MchBase;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.XMLConverUtil;

public class XmlResponseHandler{
	
	private static Logger logger = LoggerFactory.getLogger(XmlResponseHandler.class);

	public static  ResponseHandler createResponseHandler(Class clazz){
		return new XmlResponseHandlerImpl(null, clazz,null,null);
	}
	
	public static  ResponseHandler createResponseHandler(Class clazz,String sign_type,String key){
		return new XmlResponseHandlerImpl(null, clazz,sign_type,key);
	}

	public static class XmlResponseHandlerImpl extends LocalResponseHandler implements ResponseHandler {
		
		private Class clazz;
		
		private String sign_type;
		
		//签名校验key
		private String key;
		
		public XmlResponseHandlerImpl(String uriId, Class clazz,String sign_type,String key) {
			this.uriId = uriId;
			this.clazz = clazz;
			this.sign_type = sign_type;
			this.key = key;
		}

		@Override
		public T handleResponse(HttpResponse response)
				throws ClientProtocolException, IOException {
			int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                String str = EntityUtils.toString(entity);
                Header contentType = response.getEntity().getContentType();
                if(contentType!=null&&!contentType.toString().matches(".*[uU][tT][fF]-8$")){
                	str = new String(str.getBytes("iso-8859-1"),"utf-8");
                }
                logger.info("URI[{}] elapsed time:{} ms RESPONSE DATA:{}",super.uriId,System.currentTimeMillis()-super.startTime,str);
            	T t = XMLConverUtil.convertToObject(clazz,str);
            	if(t instanceof DynamicField ||(t instanceof MchBase && key != null)){
            		Map map = XMLConverUtil.convertToMap(str);
            		//转换动态属性 @since 2.8.5
            		if(t instanceof DynamicField){
	            		DynamicField dynamicField = (DynamicField)t;
	            		dynamicField.buildDynamicField(map);
            		}
            		
            		//返回数据验证签名 @since 2.8.5
            		if((t instanceof MchBase && key != null)){
            			MchBase mchBase = (MchBase)t;
            			mchBase.setSign_status(SignatureUtil.validateSign(map,sign_type,key));
            		}
            	}
            	return t;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }

		}
		
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy