Skip to content

Commit

Permalink
1. Modify security method; 2. Add debug log in CustomMenuLoadListener;
Browse files Browse the repository at this point in the history
  • Loading branch information
junior9919 committed Oct 9, 2015
1 parent 7b6c1fa commit 830aafb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
21 changes: 19 additions & 2 deletions src/com/halo/servlet/listeners/CustomMenuLoadListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,45 @@ 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.");
}

}
}

appLogger.getLogger().debug("End create menu.");
}

}
48 changes: 9 additions & 39 deletions src/com/halo/wechat/capabilities/SecurityCapability.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<String, String> args = new HashMap<String, String>();
args.put("from_user_name", fromUserName);
args.put("to_user_name", toUserName);
String resultStr = null;
try {
resultStr = this.getHttpTemplate().get(CHECK_SUBSCRIBER_URL, args);
Expand All @@ -42,35 +40,12 @@ private boolean isSubscriber(String fromUserName) {
}

try {
qrcodeResultBean = getJsonBean(new JSONUtils<QrcodeResultBean>(QrcodeResultBean.class), resultStr);
resultBean = getJsonBean(new JSONUtils<ResultBean>(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 {
Expand All @@ -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" + "<a href='"
+ "http://115.159.67.204/WeChat/images/" + imageFileName + "'>点击这里关注我们的微信号</a>";
+ "http://115.159.67.204/WeChat/images/subscribe_qrcode.jpg'>点击这里关注我们的微信号</a>";
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.");
Expand Down

0 comments on commit 830aafb

Please sign in to comment.