configuration via sql (not finished yet)

This commit is contained in:
Sola
2015-11-11 19:10:32 +08:00
parent eeae874ded
commit 2dd27b3524
7 changed files with 76 additions and 38 deletions

View File

@@ -3,7 +3,7 @@ package love.sola.netsupport.api;
import com.google.gson.Gson;
import love.sola.netsupport.enums.ResponseCode;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.SQLQuery;
import love.sola.netsupport.sql.TableUser;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -46,7 +46,7 @@ public class GetUser extends HttpServlet {
out.println(gson.toJson(new Response(ResponseCode.PARAMETER_REQUIRED)));
} else if (id != null) {
try {
User u = SQLQuery.getUserById(Integer.parseInt(id));
User u = TableUser.getUserById(Integer.parseInt(id));
if (u == null)
out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND)));
else
@@ -55,7 +55,7 @@ public class GetUser extends HttpServlet {
out.println(gson.toJson(new Response(ResponseCode.ILLEGAL_PARAMETER)));
}
} else {
User u = SQLQuery.getUserByName(name);
User u = TableUser.getUserByName(name);
if (u == null)
out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND)));
else

View File

@@ -0,0 +1,32 @@
package love.sola.netsupport.sql;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* ***********************************************
* Created by Sola on 2014/8/20.
* Don't modify this source without my agreement
* ***********************************************
*/
public class SQLCore {
public static DataSource ds;
static {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/netsupport");
ds.setLoginTimeout(3);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void free(Connection conn) {
if (conn != null) try { conn.close(); } catch (SQLException e) { }
}
}

View File

@@ -0,0 +1,14 @@
package love.sola.netsupport.sql;
/**
* ***********************************************
* Created by Sola on 2015/11/10.
* Don't modify this source without my agreement
* ***********************************************
*/
public class TableConfig {
}

View File

@@ -1,10 +1,8 @@
package love.sola.netsupport.sql;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.enums.ISPType;
import love.sola.netsupport.pojo.User;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -12,11 +10,11 @@ import java.sql.SQLException;
/**
* ***********************************************
* Created by Sola on 2014/8/20.
* Created by Sola on 2015/11/10.
* Don't modify this source without my agreement
* ***********************************************
*/
public class SQLQuery {
public class TableUser extends SQLCore {
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
@@ -25,22 +23,6 @@ public class SQLQuery {
public static final String COLUMN_ISP = "isp";
public static final String COLUMN_WECHAT = "wechat";
public static DataSource ds;
static {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/netsupport");
ds.setLoginTimeout(3);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void free(Connection conn) {
if (conn != null) try { conn.close(); } catch (SQLException e) { }
}
public static User getUserByName(String name) {
Connection conn = null;
try {
@@ -57,9 +39,7 @@ public class SQLQuery {
rs.getString(COLUMN_WECHAT));
}
} catch (SQLException e) {
} finally {
free(conn);
}
} finally { free(conn); }
return null;
}
@@ -79,9 +59,7 @@ public class SQLQuery {
rs.getString(COLUMN_WECHAT));
}
} catch (SQLException e) {
} finally {
free(conn);
}
} finally { free(conn); }
return null;
}

View File

@@ -1,6 +1,5 @@
package love.sola.netsupport.wechat;
import love.sola.netsupport.wechat.handler.RegisterHandler;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
@@ -47,7 +46,6 @@ public class WxMpServlet extends HttpServlet {
wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(config);
wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
wxMpMessageRouter.rule().async(false).rContent("^").handler(new RegisterHandler()).end();
}
@Override

View File

@@ -3,6 +3,7 @@ package love.sola.netsupport.wechat.handler;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
@@ -17,6 +18,11 @@ import java.util.Map;
*/
public class RegisterHandler implements WxMpMessageHandler {
public RegisterHandler(WxMpMessageRouter router) {
router.rule().async(false).rContent("^").handler(this).end();
}
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
throws WxErrorException {

View File

@@ -1,7 +1,8 @@
package love.sola.netsupport.wechat.intercepter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageInterceptor;
@@ -17,18 +18,27 @@ import java.util.concurrent.TimeUnit;
* Don't modify this source without my agreement
* ***********************************************
*/
public class CheckSpamIntercepter implements WxMpMessageInterceptor {
public class CheckSpamInterceptor implements WxMpMessageInterceptor {
private static Cache<String, Long> cache = CacheBuilder.newBuilder()
private static class ValueLoader extends CacheLoader<String, Long> {
@Override
public Long load(String key) throws Exception {
return System.currentTimeMillis(); //TODO: CONFIGURATION
}
}
private static LoadingCache<String, Long> cache = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();
.maximumSize(4096)
.expireAfterWrite(5, TimeUnit.SECONDS)
.build(new ValueLoader());
@Override
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
if (cache.getIfPresent(wxMessage.getFromUserName()) != null) {
}
return false;
}