Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cn.com.sunjiesh.wechat.service; import cn.com.sunjiesh.wechat.common.WeChatProperties; import cn.com.sunjiesh.wechat.common.WechatReceiveMessageEventEnum; import cn.com.sunjiesh.wechat.common.WechatReceiveMessageMsgTypeEnum; import cn.com.sunjiesh.wechat.entity.WechatMessage; import cn.com.sunjiesh.wechat.entity.message.event.WechatReceiveEventPicCommonMessage; import cn.com.sunjiesh.wechat.entity.message.event.WechatReceiveEventWeixinMessage; import cn.com.sunjiesh.wechat.helper.WechatMessageConvertDocumentHelper; import cn.com.sunjiesh.wechat.model.request.WechatMessageBaseRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventClickMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventLocationMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventLocationSelectMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventPicSysphotoMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventScanMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventScancodeCommonMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventSubscribeMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventUnSubscribeMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventViewMessageRequest; import cn.com.sunjiesh.wechat.model.request.event.WechatEventPicPhotoOrAlbumMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalImageMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalLinkMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalLocationMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalShortvideoMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalTextMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalVideoMessageRequest; import cn.com.sunjiesh.wechat.model.request.message.WechatNormalVoiceMessageRequest; import cn.com.sunjiesh.wechat.model.response.message.WechatReceiveReplayTextMessageResponse; import cn.com.sunjiesh.xcutils.common.base.ServiceException; import com.qq.weixin.mp.aes.AesException; import com.qq.weixin.mp.aes.WXBizMsgCrypt; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ??Abstract * @author tom */ public abstract class AbstractWechatMessageReceiveService { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractWechatMessageReceiveService.class); /** * ?? * * @param message ? * @param queryParams querystring? * @return * @throws ServiceException ServiceException * @throws AesException AesException */ public String messageReceive(String message, Map<String, String> queryParams) throws ServiceException, AesException { try { // ??XML Document doc4j = DocumentHelper.parseText(message); Node encryptEle = doc4j.selectSingleNode("/xml/Encrypt"); if (encryptEle != null) { LOGGER.debug("???"); String token = WeChatProperties.getProperty(WeChatProperties.TOKEN); String encodingAesKey = WeChatProperties.getProperty(WeChatProperties.ENCODING_AES_KEY); String appId = WeChatProperties.getProperty(WeChatProperties.APPID); String timeStamp = queryParams.get("timestamp"); String nonce = queryParams.get("nonce"); String msgSignature = queryParams.get("msg_signature"); WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId); String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>"; String fromXML = String.format(format, encryptEle.getText()); String decryptStr = pc.decryptMsg(msgSignature, timeStamp, nonce, fromXML); LOGGER.debug("" + decryptStr); Document decryptDoc = DocumentHelper.parseText(decryptStr); Document resultDoc = messageReceive(decryptDoc); String resultStr = resultDoc.asXML(); String encryptResultStr = pc.encryptMsg(resultStr, timeStamp, nonce); return encryptResultStr; } Document resultDoc = messageReceive(doc4j); if (resultDoc == null) { //???Success return "success"; } return resultDoc.asXML(); } catch (DocumentException ex) { throw new ServiceException("?XML", ex); } } /** * ?? * * @param doc4j ??Dom * @return XML * @throws ServiceException ServiceException */ protected Document messageReceive(Document doc4j) throws ServiceException { // ?? Node toUserNameNode = doc4j.selectSingleNode("/xml/ToUserName"); Node fromUserNameNode = doc4j.selectSingleNode("/xml/FromUserName"); Node createTimeNode = doc4j.selectSingleNode("/xml/CreateTime"); Node msgTypeNode = doc4j.selectSingleNode("/xml/MsgType"); String toUserName = toUserNameNode == null ? "" : toUserNameNode.getText(); String fromUserName = fromUserNameNode == null ? "" : fromUserNameNode.getText(); String createTime = createTimeNode == null ? "" : createTimeNode.getText(); String msgType = msgTypeNode == null ? "" : msgTypeNode.getText(); Calendar createTimeCal = Calendar.getInstance(); createTimeCal.setTimeInMillis(Long.valueOf(createTime)); Date createTimeDate = createTimeCal.getTime(); WechatMessage wechatMessage = new WechatMessage(); wechatMessage.setToUserName(toUserName); wechatMessage.setFromUserName(fromUserName); wechatMessage.setCreateTime(createTimeDate); wechatMessage.setMsgType(msgType); WechatMessageBaseRequest wechatMessageBaseRequest = new WechatMessageBaseRequest(); wechatMessageBaseRequest.setToUserName(toUserName); wechatMessageBaseRequest.setFromUserName(fromUserName); wechatMessageBaseRequest.setCreateTime(createTimeDate); wechatMessageBaseRequest.setMsgType(msgType); Document respDoc4j = null; // MsgType???WechatMessage WechatReceiveMessageMsgTypeEnum msgTypeEnum = WechatReceiveMessageMsgTypeEnum .valueOf(msgType.toUpperCase().replace("_", "")); switch (msgTypeEnum) { case TEXT: respDoc4j = textMessageReceive(doc4j, wechatMessageBaseRequest); break; case IMAGE: respDoc4j = imageMessageReceive(doc4j, wechatMessage); break; case VOICE: respDoc4j = voiceMessageReceive(doc4j, wechatMessage); break; case VIDEO: respDoc4j = videoMessageReceive(doc4j, wechatMessage); break; case SHORTVIDEO: respDoc4j = shortvideoMessageReceive(doc4j, wechatMessage); break; case LOCATION: respDoc4j = locationMessageReceive(doc4j, wechatMessage); break; case LINK: respDoc4j = linkMessageReceive(doc4j, wechatMessage); break; case EVENT: respDoc4j = eventReceive(doc4j, wechatMessage); break; default: break; } return respDoc4j; } /** * ?? * * @param doc4j ??Dom * @param wechatMessage * @return ? * @throws ServiceException ServiceException */ protected Document eventReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { // ?? // ?? Node eventNode = doc4j.selectSingleNode("/xml/Event"); Node eventKeyNode = doc4j.selectSingleNode("/xml/EventKey"); String event = eventNode == null ? "" : eventNode.getText(); String eventKey = eventKeyNode == null ? "" : eventKeyNode.getText(); String toUserName = wechatMessage.getToUserName(); String fromUserName = wechatMessage.getFromUserName(); Document respDoc4j = null; WechatReceiveMessageEventEnum eventEnum = WechatReceiveMessageEventEnum.valueOf(event.toUpperCase()); switch (eventEnum) { case CLICK: respDoc4j = clickEventReceive(toUserName, fromUserName, eventKey); break; case LOCATION: respDoc4j = locationEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case SCAN: respDoc4j = scanEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case SUBSCRIBE: respDoc4j = subscribeEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case UNSUBSCRIBE: respDoc4j = unSubscribeEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case VIEW: respDoc4j = viewEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case SCANCODE_PUSH: respDoc4j = scanCodeEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case SCANCODE_WAITMSG: respDoc4j = scanCodeEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case PIC_SYSPHOTO: respDoc4j = picSysphotoEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case PIC_PHOTO_OR_ALBUM: respDoc4j = picPhotoOrAlbumEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case PIC_WEIXIN: respDoc4j = picWeixinEventReceive(doc4j, toUserName, fromUserName, eventKey); break; case LOCATION_SELECT: respDoc4j = locationSelectEventReceive(doc4j, toUserName, fromUserName, eventKey); break; default: break; } return respDoc4j; } /** * ??? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document locationSelectEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Element sendLocationInfoEle = (Element) doc4j.selectSingleNode("/xml/SendLocationInfo"); Element locationXEle = sendLocationInfoEle.element("Location_X"); Element locationYEle = sendLocationInfoEle.element("Location_Y"); Element scaleEle = sendLocationInfoEle.element("Scale"); Element labelEle = sendLocationInfoEle.element("Label"); Element poinameEle = sendLocationInfoEle.element("Poiname"); BigDecimal locationX = new BigDecimal( StringUtils.isEmpty(locationXEle.getTextTrim()) ? "0" : locationXEle.getTextTrim()); BigDecimal locationY = new BigDecimal( StringUtils.isEmpty(locationYEle.getTextTrim()) ? "0" : locationYEle.getTextTrim()); BigDecimal scale = new BigDecimal( StringUtils.isEmpty(scaleEle.getTextTrim()) ? "0" : scaleEle.getTextTrim()); String label = labelEle.getTextTrim(); String poiname = poinameEle.getTextTrim(); WechatEventLocationSelectMessageRequest wechatMessage = new WechatEventLocationSelectMessageRequest( toUserName, fromUserName, WechatReceiveMessageEventEnum.LOCATION_SELECT.toString().toLowerCase(), eventKey); WechatEventLocationSelectMessageRequest.SendLocationInfo sendLocationInfo = wechatMessage.new SendLocationInfo(); sendLocationInfo.setLocationX(locationX); sendLocationInfo.setLocationY(locationY); sendLocationInfo.setScale(scale); sendLocationInfo.setLabel(label); sendLocationInfo.setPoiname(poiname); Document respDoc4j = messageReceive(wechatMessage); return respDoc4j; } /** * ??? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return * @throws ServiceException ServiceException */ protected Document picSysphotoEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { String event = WechatReceiveMessageEventEnum.PIC_SYSPHOTO.toString().toLowerCase(); WechatReceiveEventPicCommonMessage wechatMessage = getWechatReceiveEventPicCommonMessage(doc4j, toUserName, fromUserName, eventKey, event); WechatEventPicSysphotoMessageRequest picSysphotoMessage = new WechatEventPicSysphotoMessageRequest( toUserName, fromUserName, event, eventKey); try { BeanUtils.copyProperties(picSysphotoMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } //TODO return messageReceive(picSysphotoMessage); return null; } /** * ??? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document picPhotoOrAlbumEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { String event = WechatReceiveMessageEventEnum.PIC_PHOTO_OR_ALBUM.toString().toLowerCase(); WechatReceiveEventPicCommonMessage wechatMessage = getWechatReceiveEventPicCommonMessage(doc4j, toUserName, fromUserName, eventKey, event); WechatEventPicPhotoOrAlbumMessageRequest picPhotoOrAlbumEventMessage = new WechatEventPicPhotoOrAlbumMessageRequest( toUserName, fromUserName, event, eventKey); try { BeanUtils.copyProperties(picPhotoOrAlbumEventMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TODO return messageReceive(picPhotoOrAlbumEventMessage); return null; } /** * ?? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document picWeixinEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { String event = WechatReceiveMessageEventEnum.PIC_WEIXIN.toString().toLowerCase(); WechatReceiveEventPicCommonMessage wechatMessage = getWechatReceiveEventPicCommonMessage(doc4j, toUserName, fromUserName, eventKey, event); WechatReceiveEventWeixinMessage picPhotoOrAlbumEventMessage = new WechatReceiveEventWeixinMessage( toUserName, fromUserName, event, eventKey); try { BeanUtils.copyProperties(picPhotoOrAlbumEventMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TODO return messageReceive(picPhotoOrAlbumEventMessage); return null; } /** * ?? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @param event event * @return ? */ protected WechatReceiveEventPicCommonMessage getWechatReceiveEventPicCommonMessage(Document doc4j, String toUserName, String fromUserName, String eventKey, String event) { Element sendPicsInfoEle = (Element) doc4j.selectSingleNode("/xml/SendPicsInfo"); Element countNode = sendPicsInfoEle.element("Count"); List<Element> picListNodeList = sendPicsInfoEle.elements("PicList"); Integer count = Integer.parseInt(countNode.getText()); WechatReceiveEventPicCommonMessage wechatMessage = new WechatReceiveEventPicCommonMessage(toUserName, fromUserName, event, eventKey); wechatMessage.setCount(count); picListNodeList.forEach(node -> { Element itemEle = node.element("item"); Element picMd5SumEle = itemEle.element("PicMd5Sum"); String picMd5Sum = picMd5SumEle.getTextTrim(); wechatMessage.getPicsInfoList().add(wechatMessage.new SendPicsInfo(picMd5Sum)); }); return wechatMessage; } /** * ?? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document scanCodeEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Document respDoc4j; Node eventNode = doc4j.selectSingleNode("/xml/Event"); Element scanCodeInfoNode = (Element) doc4j.selectSingleNode("/xml/ScanCodeInfo"); Element scanTypeNode = scanCodeInfoNode.element("ScanType"); Element scanResultNode = scanCodeInfoNode.element("ScanResult"); String event = eventNode == null ? "" : eventNode.getText(); String scanType = scanTypeNode.getText(); String scanResult = scanResultNode.getText(); WechatEventScancodeCommonMessageRequest scanCodePushMessage = new WechatEventScancodeCommonMessageRequest( toUserName, fromUserName, event, eventKey); scanCodePushMessage.new ScanCodeInfo(scanType, scanResult); //TODO return messageReceive(scanCodePushMessage); return null; } /** * ??? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document viewEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { LOGGER.debug("doc4j=" + doc4j); String event = WechatReceiveMessageEventEnum.VIEW.toString().toLowerCase(); WechatEventViewMessageRequest viewMessage = new WechatEventViewMessageRequest(toUserName, fromUserName, event, eventKey); return messageRecive(viewMessage); } /** * ? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document unSubscribeEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Node ticketNode = doc4j.selectSingleNode("/xml/Ticket"); String ticket = ticketNode == null ? "" : ticketNode.getText(); WechatEventUnSubscribeMessageRequest unSubscribeMessage = new WechatEventUnSubscribeMessageRequest( toUserName, fromUserName, "unsubscribe", eventKey); unSubscribeMessage.setTicket(ticket); return messageRecive(unSubscribeMessage); } /** * * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID d * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document subscribeEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Node ticketNode = doc4j.selectSingleNode("/xml/Ticket"); String ticket = ticketNode == null ? "" : ticketNode.getText(); WechatEventSubscribeMessageRequest subscribeMessage = new WechatEventSubscribeMessageRequest(toUserName, fromUserName, "subscribe", eventKey); subscribeMessage.setTicket(ticket); return messageRecive(subscribeMessage); } /** * ???? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document scanEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Node ticketNode = doc4j.selectSingleNode("/xml/Ticket"); String ticket = ticketNode == null ? "" : ticketNode.getText(); WechatEventScanMessageRequest scanMessage = new WechatEventScanMessageRequest(toUserName, fromUserName, "scan", eventKey); scanMessage.setTicket(ticket); return messageRecive(scanMessage); } /** * ?? * * @param doc4j ??Dom * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document locationEventReceive(Document doc4j, String toUserName, String fromUserName, String eventKey) throws ServiceException { Node latitudeNode = doc4j.selectSingleNode("/xml/Latitude"); Node longitudeNode = doc4j.selectSingleNode("/xml/Longitude"); Node precisionNode = doc4j.selectSingleNode("/xml/Precision"); String latitude = latitudeNode == null ? "" : latitudeNode.getText(); String longitude = longitudeNode == null ? "" : longitudeNode.getText(); String precision = precisionNode == null ? "" : precisionNode.getText(); WechatEventLocationMessageRequest locationMessage = new WechatEventLocationMessageRequest(toUserName, fromUserName, "location", eventKey); locationMessage.setLatitude(latitude); locationMessage.setLongitude(longitude); locationMessage.setPrecision(precision); return messageRecive(locationMessage); } /** * ?? * * @param toUserName ?? * @param fromUserName ????OpenID * @param eventKey KEY??? * @return ? * @throws ServiceException ServiceException */ protected Document clickEventReceive(String toUserName, String fromUserName, String eventKey) throws ServiceException { String event = WechatReceiveMessageEventEnum.CLICK.toString().toLowerCase(); WechatEventClickMessageRequest clickMessage = new WechatEventClickMessageRequest(toUserName, fromUserName, event, eventKey); return messageRecive(clickMessage); } protected Document linkMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node titleNode = doc4j.selectSingleNode("/xml/Title"); Node descriptionNode = doc4j.selectSingleNode("/xml/Description"); Node urlNode = doc4j.selectSingleNode("/xml/Url"); String title = titleNode == null ? "" : titleNode.getText(); String description = descriptionNode == null ? "" : descriptionNode.getText(); String url = urlNode == null ? "" : urlNode.getText(); WechatNormalLinkMessageRequest linkMessage = new WechatNormalLinkMessageRequest(); try { BeanUtils.copyProperties(linkMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } linkMessage.setTitle(title); linkMessage.setDescription(description); linkMessage.setUrl(url); return messageRecive(linkMessage); } protected Document locationMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node locationXNode = doc4j.selectSingleNode("/xml/Location_X"); Node locationYNode = doc4j.selectSingleNode("/xml/Location_Y"); Node scaleNode = doc4j.selectSingleNode("/xml/Scale"); Node labelNode = doc4j.selectSingleNode("/xml/Label"); BigDecimal locationX = locationXNode == null ? new BigDecimal(0) : new BigDecimal(locationXNode.getText()); BigDecimal locationY = locationYNode == null ? new BigDecimal(0) : new BigDecimal(locationYNode.getText()); BigDecimal scale = scaleNode == null ? new BigDecimal(0) : new BigDecimal(scaleNode.getText()); String label = labelNode == null ? "" : labelNode.getText(); WechatNormalLocationMessageRequest locationMessage = new WechatNormalLocationMessageRequest(); try { BeanUtils.copyProperties(locationMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } locationMessage.setLocationX(locationX); locationMessage.setLocationY(locationY); locationMessage.setScale(scale); locationMessage.setLabel(label); return messageRecive(locationMessage); } protected Document shortvideoMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node msgIdNode = doc4j.selectSingleNode("/xml/MsgId"); Node mediaIdNode = doc4j.selectSingleNode("/xml/MediaId"); Node thumbMediaIdNode = doc4j.selectSingleNode("/xml/ThumbMediaId"); String msgId = msgIdNode == null ? "" : msgIdNode.getText(); String mediaId = mediaIdNode == null ? "" : mediaIdNode.getText(); String thumbMediaId = thumbMediaIdNode == null ? "" : thumbMediaIdNode.getText(); WechatNormalShortvideoMessageRequest shortVodeoMessage = new WechatNormalShortvideoMessageRequest(); try { BeanUtils.copyProperties(shortVodeoMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } shortVodeoMessage.setMsgId(msgId); shortVodeoMessage.setMediaId(mediaId); shortVodeoMessage.setThumbMediaId(thumbMediaId); Document respDoc4j = messageRecive(shortVodeoMessage); return respDoc4j; } protected Document videoMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node msgIdNode = doc4j.selectSingleNode("/xml/MsgId"); Node mediaIdNode = doc4j.selectSingleNode("/xml/MediaId"); Node thumbMediaIdNode = doc4j.selectSingleNode("/xml/ThumbMediaId"); String msgId = msgIdNode == null ? "" : msgIdNode.getText(); String mediaId = mediaIdNode == null ? "" : mediaIdNode.getText(); String thumbMediaId = thumbMediaIdNode == null ? "" : thumbMediaIdNode.getText(); WechatNormalVideoMessageRequest videoMessage = new WechatNormalVideoMessageRequest(); try { BeanUtils.copyProperties(videoMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } videoMessage.setMsgId(msgId); videoMessage.setMediaId(mediaId); videoMessage.setThumbMediaId(thumbMediaId); Document respDoc4j = messageRecive(videoMessage); return respDoc4j; } protected Document voiceMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node msgIdNode = doc4j.selectSingleNode("/xml/MsgId"); Node mediaIdNode = doc4j.selectSingleNode("/xml/MediaId"); Node formatNode = doc4j.selectSingleNode("/xml/Format"); String msgId = msgIdNode == null ? "" : msgIdNode.getText(); String mediaId = mediaIdNode == null ? "" : mediaIdNode.getText(); String format = formatNode == null ? "" : formatNode.getText(); WechatNormalVoiceMessageRequest voiceMessage = new WechatNormalVoiceMessageRequest(); try { BeanUtils.copyProperties(voiceMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } voiceMessage.setMsgId(msgId); voiceMessage.setMediaId(mediaId); voiceMessage.setFormat(format); Document respDoc4j = messageRecive(voiceMessage); return respDoc4j; } protected Document imageMessageReceive(Document doc4j, WechatMessage wechatMessage) throws ServiceException { Node msgIdNode = doc4j.selectSingleNode("/xml/MsgId"); Node picUrlNode = doc4j.selectSingleNode("/xml/PicUrl"); Node mediaIdNode = doc4j.selectSingleNode("/xml/MediaId"); String msgId = msgIdNode == null ? "" : msgIdNode.getText(); String picUrl = picUrlNode == null ? "" : picUrlNode.getText(); String mediaId = mediaIdNode == null ? "" : mediaIdNode.getText(); Document respDoc4j; WechatNormalImageMessageRequest imageMessage = new WechatNormalImageMessageRequest(); try { BeanUtils.copyProperties(imageMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } imageMessage.setMsgId(msgId); imageMessage.setPicUrl(picUrl); imageMessage.setMediaId(mediaId); respDoc4j = messageRecive(imageMessage); return respDoc4j; } protected Document textMessageReceive(Document doc4j, WechatMessageBaseRequest wechatMessage) throws ServiceException { Node msgIdNode = doc4j.selectSingleNode("/xml/MsgId"); Node contentNode = doc4j.selectSingleNode("/xml/Content"); String msgId = msgIdNode == null ? "" : msgIdNode.getText(); String content = contentNode == null ? "" : contentNode.getText(); WechatNormalTextMessageRequest textMessage = new WechatNormalTextMessageRequest(); try { BeanUtils.copyProperties(textMessage, wechatMessage); } catch (IllegalAccessException | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } textMessage.setMsgId(msgId); textMessage.setContent(content); Document respDoc4j = messageRecive(textMessage); return respDoc4j; } protected Document replayTextMessage(String responseToUserName, String responseFromUserName, String content) { WechatReceiveReplayTextMessageResponse textMessageResponse = new WechatReceiveReplayTextMessageResponse( responseToUserName, responseFromUserName); textMessageResponse.setContent(content); return WechatMessageConvertDocumentHelper.textMessageResponseToDocument(textMessageResponse); } protected abstract Document messageReceive(WechatEventLocationSelectMessageRequest wechatMessage); protected abstract Document messageRecive(WechatNormalTextMessageRequest textMessage) throws ServiceException; protected abstract Document messageRecive(WechatNormalImageMessageRequest imageMessage) throws ServiceException; protected abstract Document messageRecive(WechatNormalVoiceMessageRequest voiceMessage); protected abstract Document messageRecive(WechatNormalVideoMessageRequest videoMessage); protected abstract Document messageRecive(WechatNormalShortvideoMessageRequest shortVodeoMessage); protected abstract Document messageRecive(WechatNormalLocationMessageRequest locationMessage); protected abstract Document messageRecive(WechatNormalLinkMessageRequest linkMessage); protected abstract Document messageRecive(WechatEventClickMessageRequest clickMessage) throws ServiceException; protected abstract Document messageRecive(WechatEventLocationMessageRequest locationMessage); protected abstract Document messageRecive(WechatEventScanMessageRequest scanMessage); protected abstract Document messageRecive(WechatEventSubscribeMessageRequest subscribeMessage) throws ServiceException; protected abstract Document messageRecive(WechatEventUnSubscribeMessageRequest unSubscribeMessage) throws ServiceException; protected abstract Document messageRecive(WechatEventViewMessageRequest viewMessage); protected abstract Document messageRecive(WechatEventScancodeCommonMessageRequest scanCodePushMessage); protected abstract Document messageRecive(WechatEventPicSysphotoMessageRequest picSysphotoMessage); protected abstract Document messageRecive(WechatEventPicPhotoOrAlbumMessageRequest picPhotoOrAlbumEventMessage); }