From 884cc930c8da6d6b8066bd90f8d4203fc32c718b Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Thu, 25 Aug 2016 09:00:20 +0800 Subject: [PATCH] --- test/org/redkale/test/rest/HelloBean.java | 22 +++ test/org/redkale/test/rest/HelloEntity.java | 63 +++++++ test/org/redkale/test/rest/HelloService.java | 69 ++++++++ test/org/redkale/test/rest/HelloService2.java | 61 +++++++ test/org/redkale/test/rest/RetCodes.java | 161 ++++++++++++++++++ .../redkale/test/rest/SimpleRestServlet.java | 41 +++++ test/org/redkale/test/rest/UserInfo.java | 42 +++++ test/org/redkale/test/rest/UserService.java | 21 +++ .../test/rest/_DynHelloRestServlet.java | 83 +++++++++ .../test/rest/_DynHelloRestServlet2.java | 66 +++++++ 10 files changed, 629 insertions(+) create mode 100644 test/org/redkale/test/rest/HelloBean.java create mode 100644 test/org/redkale/test/rest/HelloEntity.java create mode 100644 test/org/redkale/test/rest/HelloService.java create mode 100644 test/org/redkale/test/rest/HelloService2.java create mode 100644 test/org/redkale/test/rest/RetCodes.java create mode 100644 test/org/redkale/test/rest/SimpleRestServlet.java create mode 100644 test/org/redkale/test/rest/UserInfo.java create mode 100644 test/org/redkale/test/rest/UserService.java create mode 100644 test/org/redkale/test/rest/_DynHelloRestServlet.java create mode 100644 test/org/redkale/test/rest/_DynHelloRestServlet2.java diff --git a/test/org/redkale/test/rest/HelloBean.java b/test/org/redkale/test/rest/HelloBean.java new file mode 100644 index 000000000..f43a180c0 --- /dev/null +++ b/test/org/redkale/test/rest/HelloBean.java @@ -0,0 +1,22 @@ +package org.redkale.test.rest; + +import org.redkale.convert.json.JsonFactory; +import org.redkale.source.FilterBean; + +public class HelloBean implements FilterBean { + + private int helloid; + + public int getHelloid() { + return helloid; + } + + public void setHelloid(int helloid) { + this.helloid = helloid; + } + + @Override + public String toString() { + return JsonFactory.root().getConvert().convertTo(this); + } +} diff --git a/test/org/redkale/test/rest/HelloEntity.java b/test/org/redkale/test/rest/HelloEntity.java new file mode 100644 index 000000000..1297f1bfe --- /dev/null +++ b/test/org/redkale/test/rest/HelloEntity.java @@ -0,0 +1,63 @@ +package org.redkale.test.rest; + +import javax.persistence.Id; +import org.redkale.convert.json.JsonFactory; + +public class HelloEntity { + + @Id + private int helloid; + + private String helloname; + + private int creator; + + private long updatetime; + + private long createtime; + + public int getHelloid() { + return helloid; + } + + public void setHelloid(int helloid) { + this.helloid = helloid; + } + + public String getHelloname() { + return helloname; + } + + public void setHelloname(String helloname) { + this.helloname = helloname; + } + + public long getUpdatetime() { + return updatetime; + } + + public void setUpdatetime(long updatetime) { + this.updatetime = updatetime; + } + + public long getCreatetime() { + return createtime; + } + + public void setCreatetime(long createtime) { + this.createtime = createtime; + } + + public int getCreator() { + return creator; + } + + public void setCreator(int creator) { + this.creator = creator; + } + + @Override + public String toString() { + return JsonFactory.root().getConvert().convertTo(this); + } +} diff --git a/test/org/redkale/test/rest/HelloService.java b/test/org/redkale/test/rest/HelloService.java new file mode 100644 index 000000000..3a1b504f1 --- /dev/null +++ b/test/org/redkale/test/rest/HelloService.java @@ -0,0 +1,69 @@ +package org.redkale.test.rest; + +import java.util.List; +import javax.annotation.Resource; +import org.redkale.net.http.*; + +import org.redkale.service.*; +import org.redkale.source.DataSource; +import org.redkale.source.Flipper; +import org.redkale.util.Sheet; + + +/** + * 类说明: + * Flipper : Source组件中的翻页对象 + * UserInfo :当前用户类 + * HelloEntity: Hello模块的实体类 + * HelloBean: Hellow模块实现FilterBean的过滤Bean类 + * + */ +public class HelloService implements Service { + + @Resource + private DataSource source; + + //增加记录 + public RetResult createHello(UserInfo info, HelloEntity entity) { + entity.setCreator(info == null ? 0 : info.getUserid()); //设置当前用户ID + entity.setCreatetime(System.currentTimeMillis()); + source.insert(entity); + return new RetResult<>(entity); + } + + //删除记录 + public void deleteHello(int id) { //通过 /hello/delete/1234 删除对象 + source.delete(HelloEntity.class, id); + } + + //修改记录 + public void updateHello(HelloEntity entity) { //通过 /hello/update?bean={...} 修改对象 + entity.setUpdatetime(System.currentTimeMillis()); + source.update(entity); + } + + //修改记录 + @RestMapping(name = "partupdate") + public void updateHello(HelloEntity entity, @RestParam("cols") String[] columns) { //通过 /hello/partupdate?bean={...} 修改对象 + entity.setUpdatetime(System.currentTimeMillis()); + source.updateColumns(entity, columns); + } + + //查询Sheet列表 + public Sheet queryHello(HelloBean bean, Flipper flipper) { //通过 /hello/query/offset:0/limit:20?bean={...} 查询Sheet列表 + return source.querySheet(HelloEntity.class, flipper, bean); + } + + //查询List列表 + @RestMapping(name = "list") + public List queryHello(HelloBean bean) { //通过 /hello/list?bean={...} 查询List列表 + return source.queryList(HelloEntity.class, bean); + } + + //查询单个 + @RestMapping(name = "find") + @RestMapping(name = "jsfind", jsvar = "varhello") + public HelloEntity findHello(@RestParam("#") int id) { //通过 /hello/find/1234 查询对象 + return source.find(HelloEntity.class, id); + } +} \ No newline at end of file diff --git a/test/org/redkale/test/rest/HelloService2.java b/test/org/redkale/test/rest/HelloService2.java new file mode 100644 index 000000000..931244c35 --- /dev/null +++ b/test/org/redkale/test/rest/HelloService2.java @@ -0,0 +1,61 @@ +/* + * 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 org.redkale.test.rest; + +import javax.annotation.Resource; +import org.redkale.net.http.*; +import org.redkale.service.*; +import org.redkale.source.*; +import org.redkale.util.Sheet; + +/** + * 类说明: + * Flipper : Source组件中的翻页对象 + * UserInfo :当前用户类 + * HelloEntity: Hello模块的实体类 + * HelloBean: Hellow模块实现FilterBean的过滤Bean类 + * + */ +@RestService(value = "hello", module = 0, repair = true, ignore = false) +public class HelloService2 implements Service { + + @Resource + private DataSource source; + + //增加记录 + @RestMapping(name = "create", authignore = true) + public RetResult createHello(UserInfo info, @RestParam("bean") HelloEntity entity) { + entity.setCreator(info == null ? 0 : info.getUserid()); //设置当前用户ID + entity.setCreatetime(System.currentTimeMillis()); + source.insert(entity); + return new RetResult<>(entity); + } + + //删除记录 + @RestMapping(name = "delete", authignore = true) + public void deleteHello(@RestParam("#") int id) { //通过 /hello/delete/1234 删除对象 + source.delete(HelloEntity.class, id); + } + + //修改记录 + @RestMapping(name = "update", authignore = true) + public void updateHello(@RestParam("bean") HelloEntity entity) { //通过 /hello/update?bean={...} 修改对象 + entity.setUpdatetime(System.currentTimeMillis()); + source.update(entity); + } + + //查询列表 + @RestMapping(name = "query", authignore = true) + public Sheet queryHello(@RestParam("bean") HelloBean bean, Flipper flipper) { //通过 /hello/query/offset:0/limit:20?bean={...} 查询列表 + return source.querySheet(HelloEntity.class, flipper, bean); + } + + //查询单个 + @RestMapping(name = "find", authignore = true) + public HelloEntity findHello(@RestParam("#") int id) { //通过 /hello/find/1234 查询对象 + return source.find(HelloEntity.class, id); + } +} diff --git a/test/org/redkale/test/rest/RetCodes.java b/test/org/redkale/test/rest/RetCodes.java new file mode 100644 index 000000000..1501597c4 --- /dev/null +++ b/test/org/redkale/test/rest/RetCodes.java @@ -0,0 +1,161 @@ +/* + * 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 org.redkale.test.rest; + +import java.lang.reflect.*; +import java.util.*; +import org.redkale.service.*; + +/** + * + * @author zhangjx + */ +public abstract class RetCodes { + + static { + load(RetCodes.class); + } + + protected RetCodes() { + } + //----------------------------------------------------------------------------------------------------------- + + protected static final Map rets = new HashMap<>(); + + protected static void load(Class clazz) { + for (Field field : clazz.getFields()) { + if (!Modifier.isStatic(field.getModifiers())) continue; + if (field.getType() != int.class) continue; + RetLabel info = field.getAnnotation(RetLabel.class); + if (info == null) continue; + int value; + try { + value = field.getInt(null); + } catch (Exception ex) { + ex.printStackTrace(); + continue; + } + rets.put(value, info.value()); + } + } + + public static RetResult retResult(int retcode) { + if (retcode == 0) return RetResult.success(); + return new RetResult(retcode, retInfo(retcode)); + } + + public static String retInfo(int retcode) { + if (retcode == 0) return "成功"; + return rets.getOrDefault(retcode, "未知错误"); + } + + //2000_0001 - 2999_9999 预留给 Redkale的扩展包redkalex使用 + //3000_0001 - 7999_9999 为平台系统使用 + //8000_0001 - 9999_9999 为OSS系统使用 + //------------------------------------- 通用模块 ----------------------------------------- + @RetLabel("参数无效") + public static final int RET_PARAMS_ILLEGAL = 30010001; + + @RetLabel("无上传文件") + public static final int RET_UPLOAD_NOFILE = 30010002; + + @RetLabel("上传文件过大") + public static final int RET_UPLOAD_FILETOOBIG = 30010003; + + @RetLabel("上传文件不是图片") + public static final int RET_UPLOAD_NOTIMAGE = 30010004; + + //------------------------------------- 用户模块 ----------------------------------------- + @RetLabel("未登陆") + public static final int RET_USER_UNLOGIN = 30020001; + + @RetLabel("用户登录失败") + public static final int RET_USER_LOGIN_FAIL = 30020002; + + @RetLabel("用户或密码错误") + public static final int RET_USER_ACCOUNT_PWD_ILLEGAL = 30020003; + + @RetLabel("密码设置无效") + public static final int RET_USER_PASSWORD_ILLEGAL = 30020004; + + @RetLabel("用户被禁用") + public static final int RET_USER_FREEZED = 30020005; + + @RetLabel("用户权限不够") + public static final int RET_USER_AUTH_ILLEGAL = 30020006; + + @RetLabel("用户不存在") + public static final int RET_USER_NOTEXISTS = 30020007; + + @RetLabel("用户状态异常") + public static final int RET_USER_STATUS_ILLEGAL = 30020008; + + @RetLabel("用户注册参数无效") + public static final int RET_USER_SIGNUP_ILLEGAL = 30020009; + + @RetLabel("用户性别参数无效") + public static final int RET_USER_GENDER_ILLEGAL = 30020010; + + @RetLabel("用户名无效") + public static final int RET_USER_USERNAME_ILLEGAL = 30020011; + + @RetLabel("用户账号无效") + public static final int RET_USER_ACCOUNT_ILLEGAL = 30020012; + + @RetLabel("用户账号已存在") + public static final int RET_USER_ACCOUNT_EXISTS = 30020013; + + @RetLabel("手机号码无效") + public static final int RET_USER_MOBILE_ILLEGAL = 30020014; + + @RetLabel("手机号码已存在") + public static final int RET_USER_MOBILE_EXISTS = 30020015; + + @RetLabel("手机验证码发送过于频繁") + public static final int RET_USER_MOBILE_SMSFREQUENT = 30020016; + + @RetLabel("邮箱地址无效") + public static final int RET_USER_EMAIL_ILLEGAL = 30020017; + + @RetLabel("邮箱地址已存在") + public static final int RET_USER_EMAIL_EXISTS = 30020018; + + @RetLabel("微信绑定号无效") + public static final int RET_USER_WXID_ILLEGAL = 30020019; + + @RetLabel("微信绑定号已存在") + public static final int RET_USER_WXID_EXISTS = 30020020; + + @RetLabel("绑定微信号失败") + public static final int RET_USER_WXID_BIND_FAIL = 30020021; + + @RetLabel("QQ绑定号无效") + public static final int RET_USER_QQID_ILLEGAL = 30020022; + + @RetLabel("QQ绑定号已存在") + public static final int RET_USER_QQID_EXISTS = 30020023; + + @RetLabel("绑定QQ号失败") + public static final int RET_USER_QQID_BIND_FAIL = 30020024; + + @RetLabel("获取绑定QQ信息失败") + public static final int RET_USER_QQID_INFO_FAIL = 30020025; + + @RetLabel("验证码无效") + public static final int RET_USER_RANDCODE_ILLEGAL = 30020026; //邮件或者短信验证码 + + @RetLabel("验证码已过期") + public static final int RET_USER_RANDCODE_EXPIRED = 30020027; //邮件或者短信验证码 + + @RetLabel("验证码错误或失效") + public static final int RET_USER_CAPTCHA_ILLEGAL = 30020028; //图片验证码 + + @RetLabel("用户类型无效") + public static final int RET_USER_TYPE_ILLEGAL = 30020029; + + @RetLabel("用户设备ID无效") + public static final int RET_USER_APPTOKEN_ILLEGAL = 30020030; +} diff --git a/test/org/redkale/test/rest/SimpleRestServlet.java b/test/org/redkale/test/rest/SimpleRestServlet.java new file mode 100644 index 000000000..4bf7c735e --- /dev/null +++ b/test/org/redkale/test/rest/SimpleRestServlet.java @@ -0,0 +1,41 @@ +package org.redkale.test.rest; + +import java.io.IOException; +import javax.annotation.Resource; + +import org.redkale.net.http.*; +import org.redkale.service.RetResult; + + +public class SimpleRestServlet extends RestHttpServlet { + + protected static final RetResult RET_UNLOGIN = RetCodes.retResult(RetCodes.RET_USER_UNLOGIN); + + protected static final RetResult RET_AUTHILLEGAL = RetCodes.retResult(RetCodes.RET_USER_AUTH_ILLEGAL); + + @Resource + private UserService userService; + + //获取当前用户信息 + @Override + protected UserInfo currentUser(HttpRequest req) throws IOException { + String sessionid = req.getSessionid(false); + if (sessionid == null || sessionid.isEmpty()) return null; + return userService.current(sessionid); + } + + //普通鉴权 + @Override + public boolean authenticate(int module, int actionid, HttpRequest request, HttpResponse response) throws IOException { + UserInfo info = currentUser(request); + if (info == null) { + response.finishJson(RET_UNLOGIN); + return false; + } else if (!info.checkAuth(module, actionid)) { + response.finishJson(RET_AUTHILLEGAL); + return false; + } + return true; + } + +} diff --git a/test/org/redkale/test/rest/UserInfo.java b/test/org/redkale/test/rest/UserInfo.java new file mode 100644 index 000000000..70c1167c7 --- /dev/null +++ b/test/org/redkale/test/rest/UserInfo.java @@ -0,0 +1,42 @@ +package org.redkale.test.rest; + +import org.redkale.convert.json.JsonFactory; + +/** + * 当前用户对象 + * + * @author zhangjx + */ +public class UserInfo { + + private int userid; + + private String username = ""; + + public int getUserid() { + return userid; + } + + public boolean checkAuth(int moduleid, int actionid) { + if (moduleid == 0 || actionid == 0) return true; + //权限判断 + return true; + } + + public void setUserid(int userid) { + this.userid = userid; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public String toString() { + return JsonFactory.root().getConvert().convertTo(this); + } +} diff --git a/test/org/redkale/test/rest/UserService.java b/test/org/redkale/test/rest/UserService.java new file mode 100644 index 000000000..00839bd18 --- /dev/null +++ b/test/org/redkale/test/rest/UserService.java @@ -0,0 +1,21 @@ +/* + * 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 org.redkale.test.rest; + +import org.redkale.service.Service; + +/** + * 简单的定义UserService接口 + * + * @author zhangjx + */ +public class UserService implements Service { + + //根据登录态获取当前用户信息 + public UserInfo current(String sessionid) { + return null; + } +} diff --git a/test/org/redkale/test/rest/_DynHelloRestServlet.java b/test/org/redkale/test/rest/_DynHelloRestServlet.java new file mode 100644 index 000000000..5649eb235 --- /dev/null +++ b/test/org/redkale/test/rest/_DynHelloRestServlet.java @@ -0,0 +1,83 @@ +package org.redkale.test.rest; + +import java.io.IOException; +import java.util.List; +import javax.annotation.Resource; +import org.redkale.net.http.*; +import org.redkale.service.RetResult; +import org.redkale.source.Flipper; +import org.redkale.util.*; + +@WebServlet(value = {"/hello/*"}, repair = true) +public class _DynHelloRestServlet extends SimpleRestServlet { + + @Resource + private HelloService _service; + + @AuthIgnore + @WebAction(url = "/hello/create") + public void create(HttpRequest req, HttpResponse resp) throws IOException { + HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); + UserInfo user = currentUser(req); + RetResult result = _service.createHello(user, bean); + resp.finishJson(result); + } + + @AuthIgnore + @WebAction(url = "/hello/delete/") + public void delete(HttpRequest req, HttpResponse resp) throws IOException { + int id = Integer.parseInt(req.getRequstURILastPath()); + _service.deleteHello(id); + resp.finishJson(RetResult.success()); + } + + @AuthIgnore + @WebAction(url = "/hello/update") + public void update(HttpRequest req, HttpResponse resp) throws IOException { + HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); + _service.updateHello(bean); + resp.finishJson(RetResult.success()); + } + + @AuthIgnore + @WebAction(url = "/hello/partupdate") + public void partupdate(HttpRequest req, HttpResponse resp) throws IOException { + HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); + String[] cols = req.getJsonParameter(String[].class, "cols"); + _service.updateHello(bean, cols); + resp.finishJson(RetResult.success()); + } + + @AuthIgnore + @WebAction(url = "/hello/query") + public void query(HttpRequest req, HttpResponse resp) throws IOException { + HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); + Flipper flipper = req.getFlipper(); + Sheet result = _service.queryHello(bean, flipper); + resp.finishJson(result); + } + + @AuthIgnore + @WebAction(url = "/hello/list") + public void list(HttpRequest req, HttpResponse resp) throws IOException { + HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); + List result = _service.queryHello(bean); + resp.finishJson(result); + } + + @AuthIgnore + @WebAction(url = "/hello/find/") + public void find(HttpRequest req, HttpResponse resp) throws IOException { + int id = Integer.parseInt(req.getRequstURILastPath()); + HelloEntity bean = _service.findHello(id); + resp.finishJson(bean); + } + + @AuthIgnore + @WebAction(url = "/hello/jsfind/") + public void jsfind(HttpRequest req, HttpResponse resp) throws IOException { + int id = Integer.parseInt(req.getRequstURILastPath()); + HelloEntity bean = _service.findHello(id); + resp.finishJsResult("varhello", bean); + } +} diff --git a/test/org/redkale/test/rest/_DynHelloRestServlet2.java b/test/org/redkale/test/rest/_DynHelloRestServlet2.java new file mode 100644 index 000000000..85e87e664 --- /dev/null +++ b/test/org/redkale/test/rest/_DynHelloRestServlet2.java @@ -0,0 +1,66 @@ +/* + * 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 org.redkale.test.rest; + +import java.io.IOException; +import javax.annotation.Resource; +import org.redkale.net.http.*; +import org.redkale.service.RetResult; +import org.redkale.source.Flipper; +import org.redkale.util.Sheet; + +/** + * + * @author zhangjx + */ +@WebServlet(value = {"/hello/*"}, repair = true) +public class _DynHelloRestServlet2 extends SimpleRestServlet { + + @Resource + private HelloService2 _service; + + @AuthIgnore + @WebAction(url = "/hello/create") + public void create(HttpRequest req, HttpResponse resp) throws IOException { + HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); + UserInfo user = currentUser(req); + RetResult result = _service.createHello(user, bean); + resp.finishJson(result); + } + + @AuthIgnore + @WebAction(url = "/hello/delete/") + public void delete(HttpRequest req, HttpResponse resp) throws IOException { + int id = Integer.parseInt(req.getRequstURILastPath()); + _service.deleteHello(id); + resp.finishJson(RetResult.success()); + } + + @AuthIgnore + @WebAction(url = "/hello/update") + public void update(HttpRequest req, HttpResponse resp) throws IOException { + HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); + _service.updateHello(bean); + resp.finishJson(RetResult.success()); + } + + @AuthIgnore + @WebAction(url = "/hello/query") + public void query(HttpRequest req, HttpResponse resp) throws IOException { + HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); + Flipper flipper = req.getFlipper(); + Sheet result = _service.queryHello(bean, flipper); + resp.finishJson(result); + } + + @AuthIgnore + @WebAction(url = "/hello/find/") + public void find(HttpRequest req, HttpResponse resp) throws IOException { + int id = Integer.parseInt(req.getRequstURILastPath()); + HelloEntity bean = _service.findHello(id); + resp.finishJson(bean); + } +}