首页>代码>Java通过Spring MVC开发的微信公众平台开发框架>/wechat-develop-framework-master/src/wechat-framework/src/main/java/com/xgzhang/wechat/framework/MessageReceiver.java
package com.xgzhang.wechat.framework;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xgzhang.wechat.framework.domain.ReceiveEntity;
import com.xgzhang.wechat.framework.domain.response.ResponseMessage;
import com.xgzhang.wechat.framework.tools.DomainUtility;
import com.xgzhang.wechat.framework.tools.IOUtility;
import com.xgzhang.wechat.framework.tools.SecurityUtility;

public class MessageReceiver {
	private final String ACCESS_DENY = "access deny";
	
	private Logger _logger = LoggerFactory.getLogger(this.getClass());
	
	private String _token;
	private MessageProcesser _messageProcessor;
	
	public MessageReceiver(String token, MessageProcesser messageProcessor){
		this._token = token;
		this._messageProcessor = messageProcessor;
	}
	
	public void receiveGetMesseage(HttpServletRequest request, HttpServletResponse response){ 
		response.setContentType("text/plain");
		try{
			String requestSinature = request.getParameter("signature");
	        String echostr = request.getParameter("echostr");  //random string
	        String timestamp = request.getParameter("timestamp");
	        String nonce = request.getParameter("nonce");  //random number
	        
	        if(requestSinature == null || requestSinature.isEmpty()
	        	|| echostr == null || echostr.isEmpty()
	        	|| timestamp == null || timestamp.isEmpty()
	        	|| nonce == null || nonce.isEmpty())
	        {
	        	this._logger.info("Parameter is required!");
	        	response.getWriter().print(ACCESS_DENY);
	        	return;
	        }
	        
	        this._logger.trace(String.format("sinature=%s", requestSinature));
	        this._logger.trace(String.format("echostr=%s", echostr));
	        this._logger.trace(String.format("timestamp=%s", timestamp));
	        this._logger.trace(String.format("nonce=%s", nonce));

	        String[] parameters = { this._token, timestamp, nonce };
	        Arrays.sort(parameters);
	        String bigStr = parameters[0] + parameters[1] + parameters[2];
	        String tokenSinature = SecurityUtility.getDigestOfString(bigStr.getBytes()).toLowerCase();

	        if (tokenSinature.equals(requestSinature)) {
	            response.getWriter().print(echostr);
	        }else{
	        	this._logger.error("Check singature failed");
	        	response.getWriter().print(ACCESS_DENY);
	        }
		}catch(Exception e){
			this._logger.error(ACCESS_DENY, e);
		}
	}
	
	public void receivePostMessage(HttpServletRequest request, HttpServletResponse response){
		response.setContentType("text/plain");
		try{
			//Encoding
			request.setCharacterEncoding("UTF-8");
			response.setHeader("Content-type", "text/html;charset=UTF-8");  
			response.setCharacterEncoding("UTF-8");
			
			//Request
			BufferedReader bufferReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
			char[] body = new char[request.getContentLength()];
			bufferReader.read(body);
			String requestMessage = new String(body);
			this._logger.info(requestMessage);
			ReceiveEntity receiveEntity = DomainUtility.getReceiveEntity(requestMessage);
			
			this._logger.info(String.format("MessageType = %s", receiveEntity.getMsgType()));
			this._logger.info(String.format("TextContent = %s", receiveEntity.getContent()));
			this._logger.info(String.format("PicRUL = %s", receiveEntity.getPicUrl()));
			this._logger.info(String.format("EventType = %s", receiveEntity.getEventKey()));
			
			//Response
			ResponseMessage reponseMessage = null;
			if(receiveEntity.getMsgType().equals(ReceiveEntity.REQ_MESSAGE_TYPE_TEXT)){
				reponseMessage = this._messageProcessor.processText(receiveEntity.getContent());
			}else if(receiveEntity.getMsgType().equals(ReceiveEntity.REQ_MESSAGE_TYPE_IMAGE)){
				reponseMessage = this._messageProcessor.processImage(IOUtility.downloadImage(receiveEntity.getPicUrl()));
			}else if(receiveEntity.getMsgType().equals(ReceiveEntity.REQ_MESSAGE_TYPE_EVENT)){
				reponseMessage = this._messageProcessor.processEvent(receiveEntity.getEventKey());
			}
			reponseMessage.setFromUserName(receiveEntity.getToUserName());
			reponseMessage.setToUserName(receiveEntity.getFromUserName());
			
			//out
			PrintStream out  = new PrintStream(response.getOutputStream());
			this._logger.info(DomainUtility.messageToXml(reponseMessage));
			out.print(DomainUtility.messageToXml(reponseMessage));
			out.close();
		}catch(Exception e){
			this._logger.error("Process message error", e);
		}
	}
}
最近下载更多
我就我的 LV6前天
月亮星星星星
1763157114 LV153月29日
月亮月亮月亮星星星星星星
hellow1 LV31月31日
星星星星星星
皇朝灬焱轩 LV81月25日
月亮月亮
zt3631877 LV92020年12月10日
月亮月亮星星
HUANG888 LV62020年9月3日
月亮星星星星
aaa最代码 LV122020年8月21日
月亮月亮月亮
fzlzxc LV102020年8月12日
月亮月亮星星星星
郑志敏 LV72020年7月25日
月亮星星星星星星
kamiomisuzu LV92020年7月16日
月亮月亮星星
最近浏览更多
我就我的 LV6前天
月亮星星星星
暂无贡献等级
蹦迪的小熊 LV34月9日
星星星星星星
张泰然3月23日
暂无贡献等级
liuxie LV123月18日
月亮月亮月亮
dongren88 LV53月8日
月亮星星
Joker_ZST LV32月24日
星星星星星星
songjiahui LV182月20日
太阳星星星星
gong001002003 LV12月17日
星星
im-LiuM LV52月8日
月亮星星
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友