From 830aafb38d4f8062af0acbfac74fb50a53dcaaf1 Mon Sep 17 00:00:00 2001 From: junior9919 <18186131274@189.cn> Date: Fri, 9 Oct 2015 21:37:31 +0800 Subject: [PATCH] 1. Modify security method; 2. Add debug log in CustomMenuLoadListener; --- .../listeners/CustomMenuLoadListener.java | 21 +++++++- .../capabilities/SecurityCapability.java | 48 ++++--------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/com/halo/servlet/listeners/CustomMenuLoadListener.java b/src/com/halo/servlet/listeners/CustomMenuLoadListener.java index eda3e7c..2302f6c 100644 --- a/src/com/halo/servlet/listeners/CustomMenuLoadListener.java +++ b/src/com/halo/servlet/listeners/CustomMenuLoadListener.java @@ -147,21 +147,36 @@ public void contextDestroyed(ServletContextEvent arg0) { public void contextInitialized(ServletContextEvent arg0) { appLogger = (AppLogger) SpringUtils.getBean("appLogger"); + appLogger.getLogger().debug("Starting create menu..."); + String menuFileName = getMenuFileName(arg0.getServletContext()); + + appLogger.getLogger().debug("Menu file is " + menuFileName); + File menuFile = getMenuFile(menuFileName); if (null != menuFile) { + + appLogger.getLogger().debug("Got a menu file."); + CustomMenuAbility customMenuAbility = (CustomMenuAbility) SpringUtils.getBean("customMenuAbility"); if (null == customMenuAbility) { appLogger.getLogger().error("Get customMenuAbility bean error, may be applicationContext-wechat.xml is damaged."); return; } if (menuFileName.endsWith("deleteMenu.json")) { - deleteMenu(customMenuAbility); + + appLogger.getLogger().debug("Ready to delete menu."); + + if (!deleteMenu(customMenuAbility)) { + appLogger.getLogger().debug("Delete menu request has no response."); + } } else { String jsonStr = getJsonStr(menuFile); if (null != jsonStr && !jsonStr.isEmpty()) { if (deleteMenu(customMenuAbility)) { - createMenu(jsonStr, customMenuAbility); + if (!createMenu(jsonStr, customMenuAbility)) { + appLogger.getLogger().debug("Create menu wrong."); + } } } else { appLogger.getLogger().error("File read error, or file is empty."); @@ -169,6 +184,8 @@ public void contextInitialized(ServletContextEvent arg0) { } } + + appLogger.getLogger().debug("End create menu."); } } diff --git a/src/com/halo/wechat/capabilities/SecurityCapability.java b/src/com/halo/wechat/capabilities/SecurityCapability.java index 1734711..c6c0b0e 100644 --- a/src/com/halo/wechat/capabilities/SecurityCapability.java +++ b/src/com/halo/wechat/capabilities/SecurityCapability.java @@ -1,6 +1,5 @@ package com.halo.wechat.capabilities; -import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Random; @@ -11,10 +10,8 @@ import com.halo.http.utils.HttpUtilsException; import com.halo.json.utils.JSONUtils; -import com.halo.spring.utils.SpringUtils; -import com.halo.wechat.capabilities.abilities.QrcodeAbility; import com.halo.wechat.capabilities.abilities.SecurityAbility; -import com.halo.wechat.capabilities.beans.QrcodeResultBean; +import com.halo.wechat.capabilities.beans.ResultBean; import com.halo.wechat.messages.Message; import com.halo.wechat.messages.MsgType; import com.halo.wechat.messages.TextMessage; @@ -29,11 +26,12 @@ public class SecurityCapability extends AbstractCapability implements SecurityAb private final String CHECK_SUBSCRIBER_URL = "http://115.159.67.204/WeChat/isSubscriber.do"; - private QrcodeResultBean qrcodeResultBean; + private ResultBean resultBean; - private boolean isSubscriber(String fromUserName) { + private boolean isSubscriber(String fromUserName, String toUserName) { Map args = new HashMap(); args.put("from_user_name", fromUserName); + args.put("to_user_name", toUserName); String resultStr = null; try { resultStr = this.getHttpTemplate().get(CHECK_SUBSCRIBER_URL, args); @@ -42,35 +40,12 @@ private boolean isSubscriber(String fromUserName) { } try { - qrcodeResultBean = getJsonBean(new JSONUtils(QrcodeResultBean.class), resultStr); + resultBean = getJsonBean(new JSONUtils(ResultBean.class), resultStr); } catch (CapabilityException e) { return false; } - if (null != qrcodeResultBean) { - if (1 == qrcodeResultBean.getErrcode()) { - return true; - } - } - return false; - } - - private String getQrcodeImage(String ticket) throws CapabilityException { - QrcodeAbility qrcodeAbility = (QrcodeAbility) SpringUtils.getBean("qrcodeAbility"); - if (null == qrcodeAbility) { - throw new CapabilityException("Get bean qrcodeAbility failed, applicationContext-wechat.xml may be damaged."); - } - File downloadFile = qrcodeAbility.getQrcodeImage(ticket); - - String fileName = ticket.replace('\\', 'x').replace('/', 'x').replace(':', 'x').replace('*', 'x').replace('?', 'x').replace('\"', 'x') - .replace('<', 'x').replace('>', 'x').replace('|', 'x').replace('\"', 'x') - + ".jpg"; - String saveAsFileName = SpringUtils.getWebApplicationContext().getServletContext().getRealPath("/images") + File.separator + fileName; - - File saveAsFile = new File(saveAsFileName); - downloadFile.renameTo(saveAsFile); - - return fileName; + return null != resultBean ? 1 == resultBean.getErrcode() : false; } public SecurityCapability() throws CapabilityException { @@ -82,23 +57,18 @@ public void messageSecurityCheck(JoinPoint jp) throws CapabilityException { Object[] args = jp.getArgs(); if (args.length == 2 && null != args[1] && args[1] instanceof Message) { Message message = (Message) args[1]; - if (!isSubscriber(message.getFromUserName())) { - String imageFileName = getQrcodeImage(qrcodeResultBean.getTicket()); + if (!isSubscriber(message.getFromUserName(), message.getToUserName())) { if (jp.getTarget() instanceof MessageCapability) { MessageCapability messageCapability = (MessageCapability) jp.getTarget(); if (args[0] instanceof HttpServletResponse) { HttpServletResponse response = (HttpServletResponse) args[0]; String content = "时间就是金钱,我的朋友!\r\n" + "别怪我啰嗦,开发这款地精科技产品我们可是花了不少时间!\r\n" + "关注我们的微信号吧,保证你再也听不到这些唠叨。\r\n\r\n" + "点击这里关注我们的微信号"; + + "http://115.159.67.204/WeChat/images/subscribe_qrcode.jpg'>点击这里关注我们的微信号"; Random rand = new Random(); long msgId = rand.nextLong(); TextMessage responseMessage = new TextMessage(message.getToUserName(), message.getFromUserName(), System.currentTimeMillis(), MsgType.TEXT, content, msgId); - try { - messageCapability.responseMessage(response, responseMessage); - } catch (CapabilityException e) { - - } + messageCapability.responseMessage(response, responseMessage); } } throw new CapabilityException("User response message canceled.");