From ac58649a40e0c9ca45ae458645572d3ccee30c73 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 19 Sep 2019 20:06:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9B=B8=E5=85=B3=E7=9A=84AP?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 1 + .../fanruan/api/decision/AuthorityKit.java | 11 +++ .../api/decision/middle/ReadOnlyUser.java | 45 ------------ .../fanruan/api/decision/user/OpenUser.java | 73 +++++++++++++++++++ .../api/decision/{ => user}/UserKit.java | 35 ++++++++- .../java/com/fanruan/api/i18n/I18nKit.java | 13 ++++ .../java/com/fanruan/api/net/NetworkKit.java | 37 +++++++--- .../java/com/fanruan/api/web/FlushKit.java | 53 ++++++++++++++ .../java/com/fanruan/api/web/ResponseKit.java | 64 ++++++++++++++++ .../com/fanruan/api/web/TextGeneratorKit.java | 21 ++++++ 10 files changed, 294 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/fanruan/api/decision/AuthorityKit.java delete mode 100644 src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java create mode 100644 src/main/java/com/fanruan/api/decision/user/OpenUser.java rename src/main/java/com/fanruan/api/decision/{ => user}/UserKit.java (50%) create mode 100644 src/main/java/com/fanruan/api/web/FlushKit.java create mode 100644 src/main/java/com/fanruan/api/web/ResponseKit.java create mode 100644 src/main/java/com/fanruan/api/web/TextGeneratorKit.java diff --git a/readme.md b/readme.md index d3c4811..7d2ac95 100644 --- a/readme.md +++ b/readme.md @@ -54,6 +54,7 @@ SessionKit#getSession(@NotNull String sessionID); |网页框插件|https://git.fanruan.com/fanruan/demo-widget-iframe| |时钟控件|https://git.fanruan.com/fanruan/demo-show-clock| |ldaps认证插件|https://git.fanruan.com/fanruan/demo-ldaps-passport| +|个人国际化设置插件|https://git.fanruan.com/fanruan/decision-user-language| ## 如何判断插件中调用的API需要增加到FineKit中 diff --git a/src/main/java/com/fanruan/api/decision/AuthorityKit.java b/src/main/java/com/fanruan/api/decision/AuthorityKit.java new file mode 100644 index 0000000..54ba641 --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/AuthorityKit.java @@ -0,0 +1,11 @@ +package com.fanruan.api.decision; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 权限相关的工具类 + */ +public class AuthorityKit { + +} diff --git a/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java b/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java deleted file mode 100644 index 0c1321d..0000000 --- a/src/main/java/com/fanruan/api/decision/middle/ReadOnlyUser.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fanruan.api.decision.middle; - -/** - * @author richie - * @version 10.0 - * Created by richie on 2019-09-19 - */ -public class ReadOnlyUser { - - /** - * 非空项,无默认值 - */ - private String userName = null; - /** - * 非空项,无默认值 - */ - private String password = null; - - /** - * 姓名 - */ - private String realName = null; - - public static ReadOnlyUser build(String userName, String password, String realName) { - return new ReadOnlyUser(userName, password, realName); - } - - private ReadOnlyUser(String userName, String password, String realName) { - this.userName = userName; - this.password = password; - this.realName = realName; - } - - public String getUserName() { - return userName; - } - - public String getPassword() { - return password; - } - - public String getRealName() { - return realName; - } -} diff --git a/src/main/java/com/fanruan/api/decision/user/OpenUser.java b/src/main/java/com/fanruan/api/decision/user/OpenUser.java new file mode 100644 index 0000000..52ff51e --- /dev/null +++ b/src/main/java/com/fanruan/api/decision/user/OpenUser.java @@ -0,0 +1,73 @@ +package com.fanruan.api.decision.user; + +import com.fr.decision.authority.data.User; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + */ +public class OpenUser { + + private User user; + + static OpenUser wrap(User user) { + return new OpenUser(user); + } + + public OpenUser create(String id) { + return new OpenUser(new User().id(id)); + } + + private OpenUser(User user) { + this.user = user; + } + + User select() { + return user; + } + + public String getUserName() { + return user.getUserName(); + } + + public String getPassword() { + return user.getPassword(); + } + + public void setPassword(String password) { + this.user.setPassword(password); + } + + public String getRealName() { + return user.getRealName(); + } + + public void setRealName(String realName) { + this.user.setRealName(realName); + } + + public String getLanguage() { + return user.getLanguage(); + } + + public void setLanguage(String language) { + this.user.setLanguage(language); + } + + public String getEmail() { + return user.getEmail(); + } + + public void setEmail(String email) { + this.user.setEmail(email); + } + + public String getMobile() { + return user.getMobile(); + } + + public void setMobile(String mobile) { + this.user.setMobile(mobile); + } +} diff --git a/src/main/java/com/fanruan/api/decision/UserKit.java b/src/main/java/com/fanruan/api/decision/user/UserKit.java similarity index 50% rename from src/main/java/com/fanruan/api/decision/UserKit.java rename to src/main/java/com/fanruan/api/decision/user/UserKit.java index 323d6b4..eeb1429 100644 --- a/src/main/java/com/fanruan/api/decision/UserKit.java +++ b/src/main/java/com/fanruan/api/decision/user/UserKit.java @@ -1,11 +1,13 @@ -package com.fanruan.api.decision; +package com.fanruan.api.decision.user; -import com.fanruan.api.decision.middle.ReadOnlyUser; +import com.fr.decision.authority.AuthorityContext; import com.fr.decision.authority.data.User; import com.fr.decision.webservice.utils.UserSourceFactory; import com.fr.decision.webservice.utils.user.source.UserSource; import com.fr.decision.webservice.v10.user.UserService; +import javax.servlet.http.HttpServletRequest; + /** * @author richie * @version 10.0 @@ -25,6 +27,11 @@ public class UserKit { return UserSourceFactory.getInstance().getUserSource(user); } + public static OpenUser getUserByRequest(HttpServletRequest req) throws Exception { + User user = UserService.getInstance().getUserByRequest(req); + return OpenUser.wrap(user); + } + /** * 根据用户名获取用户信息 * @@ -32,8 +39,28 @@ public class UserKit { * @return 用户信息 * @throws Exception 如果获取用户出错,则抛出此异常 */ - public static ReadOnlyUser getReadOnlyUser(String username) throws Exception { + public static OpenUser getUser(String username) throws Exception { User user = UserService.getInstance().getUserByUserName(username); - return ReadOnlyUser.build(user.getUserName(), user.getPassword(), user.getRealName()); + return OpenUser.wrap(user); + } + + /** + * 更新一个用户信息 + * + * @param user 用户 + * @throws Exception 更新用户信息失败则抛出此异常 + */ + public static void update(OpenUser user) throws Exception { + AuthorityContext.getInstance().getUserController().update(user.select()); + } + + /** + * 添加一个用户 + * + * @param user 用户 + * @throws Exception 添加用户失败则抛出此异常 + */ + public static void add(OpenUser user) throws Exception { + AuthorityContext.getInstance().getUserController().update(user.select()); } } diff --git a/src/main/java/com/fanruan/api/i18n/I18nKit.java b/src/main/java/com/fanruan/api/i18n/I18nKit.java index 74a95c9..a72f000 100644 --- a/src/main/java/com/fanruan/api/i18n/I18nKit.java +++ b/src/main/java/com/fanruan/api/i18n/I18nKit.java @@ -2,6 +2,9 @@ package com.fanruan.api.i18n; import com.fr.locale.InterProviderFactory; +import java.util.Locale; +import java.util.Map; + public class I18nKit { /** * 获取国际化文本 @@ -23,4 +26,14 @@ public class I18nKit { public static String getLocText(String string, String... args) { return InterProviderFactory.getProvider().getLocText(string, args); } + + /** + * 获取某种语言下所有的国际化键值对 + * + * @param locale 国际化 + * @return 国际化键值对集合 + */ + public static Map getEntireKV(Locale locale) { + return InterProviderFactory.getClientProvider().getEntireKV(locale); + } } diff --git a/src/main/java/com/fanruan/api/net/NetworkKit.java b/src/main/java/com/fanruan/api/net/NetworkKit.java index ebc52ef..e697b37 100644 --- a/src/main/java/com/fanruan/api/net/NetworkKit.java +++ b/src/main/java/com/fanruan/api/net/NetworkKit.java @@ -2,12 +2,14 @@ package com.fanruan.api.net; import com.fr.base.ServerConfig; import com.fr.data.NetworkHelper; +import com.fr.decision.webservice.utils.WebServiceUtils; import org.jetbrains.annotations.Nullable; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +import java.util.Locale; import java.util.Map; /** @@ -21,7 +23,7 @@ public class NetworkKit { /** * 生成一个打印输出器 * - * @param res HTTP响应 + * @param res http响应 * @return 打印输出器 * @throws java.io.IOException 如果无法创建输出器则抛出此异常 */ @@ -32,7 +34,7 @@ public class NetworkKit { /** * 生成一个打印输出器 * - * @param res HTTP响应 + * @param res http响应 * @param charsetName 编码 * @return 打印输出器 * @throws java.io.IOException 如果无法创建输出器则抛出此异常 @@ -45,7 +47,7 @@ public class NetworkKit { * 写出指定的模板 * * @param resource 模板路径 - * @param response HTTP响应 + * @param response http响应 * @param map 用于替换模板中参数的的参数集 * @throws java.io.IOException 如果无法创建输出器则抛出此异常 */ @@ -56,7 +58,7 @@ public class NetworkKit { /** * 把HTTP请求中指定名字的参数值转化为整数,参数为空或不是整数则返回-1 * - * @param req HTTP请求 + * @param req http请求 * @param paraName 参数名 * @return 整型参数值 */ @@ -67,7 +69,7 @@ public class NetworkKit { /** * 把HTTP请求中指定名字的参数值转化为整数 * - * @param req HTTP请求 + * @param req http请求 * @param paraName 参数名 * @param defaultValue 默认值 * @return 返回req中参数的整数值。参数为空或不是整数则返回defaultValue @@ -79,7 +81,7 @@ public class NetworkKit { /** * 从http请求中获取sessionID * - * @param req HTTP请求 + * @param req http请求 * @return session编号 */ public static @Nullable String getHTTPRequestSessionIDParameter(HttpServletRequest req) { @@ -89,7 +91,7 @@ public class NetworkKit { /** * 把HTTP请求中指定名字的参数值转化为布尔值 * - * @param req HTTP请求 + * @param req http请求 * @param paraName 参数名 * @return 布尔类型的参数值 */ @@ -100,7 +102,7 @@ public class NetworkKit { /** * 获取HTTP请求中指定名字的参数值 * - * @param req HTTP请求 + * @param req http请求 * @param paraName 参数名 * @return 字符型参数值 */ @@ -111,7 +113,7 @@ public class NetworkKit { /** * 获取第一个不为空的参数 * - * @param req HTTP请求 + * @param req http请求 * @param paraNames 参数列表 * @return 字符型参数值 */ @@ -119,8 +121,23 @@ public class NetworkKit { return NetworkHelper.getHTTPRequestParameter(req, paraNames); } - + /** + * 生成服务器地址 + * + * @param req http请求 + * @return 服务器地址 + */ public static String createServletURL(HttpServletRequest req) { return NetworkHelper.createServletURL(req, ServerConfig.getInstance().getServletName()); } + + /** + * 从http请求中获取用户的国际化信息 + * + * @param req http请求 + * @return 国际化对象 + */ + public static Locale getLocale(HttpServletRequest req) { + return WebServiceUtils.getLocale(req); + } } diff --git a/src/main/java/com/fanruan/api/web/FlushKit.java b/src/main/java/com/fanruan/api/web/FlushKit.java new file mode 100644 index 0000000..70de44a --- /dev/null +++ b/src/main/java/com/fanruan/api/web/FlushKit.java @@ -0,0 +1,53 @@ +package com.fanruan.api.web; + +import com.fanruan.api.net.NetworkKit; +import com.fr.json.JSONArray; +import com.fr.json.JSONObject; + +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 将内容输入到输出流中 + */ +public class FlushKit { + + /** + * 输出JSON类型的字符串 + * + * @param res HTTP响应 + * @param jo JSON对象 + * @throws Exception 输出出现错误则抛出此异常 + */ + public static void printAsJSON(HttpServletResponse res, JSONObject jo) throws Exception { + printAsString(res, jo.toString()); + } + + /** + * 输出JSON类型的字符串 + * + * @param res HTTP响应 + * @param ja JSON数组对象 + * @throws Exception 输出出现错误则抛出此异常 + */ + public static void printAsJSON(HttpServletResponse res, JSONArray ja) throws Exception { + printAsString(res, ja.toString()); + } + + /** + * 输出字符串,一般来说是JSON格式 + * + * @param res HTTP响应 + * @param jo JSON样式的字符串 + * @throws Exception 输出出现错误则抛出此异常 + */ + public static void printAsString(HttpServletResponse res, String jo) throws Exception { + PrintWriter pw = NetworkKit.createPrintWriter(res); + pw.print(jo); + pw.flush(); + pw.close(); + } +} diff --git a/src/main/java/com/fanruan/api/web/ResponseKit.java b/src/main/java/com/fanruan/api/web/ResponseKit.java new file mode 100644 index 0000000..28663c7 --- /dev/null +++ b/src/main/java/com/fanruan/api/web/ResponseKit.java @@ -0,0 +1,64 @@ +package com.fanruan.api.web; + +import com.fr.decision.webservice.Response; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + * 响应工具类 + */ +public class ResponseKit { + + /** + * 成功的响应,关注结果 + * + * @param obj 响应结果 + * @return 响应 + */ + public static Object ok(Object obj) { + return Response.ok(obj); + } + + /** + * 成功的响应,不关注结果 + * + * @return 响应 + */ + public static Object success() { + return Response.success(); + } + + /** + * 成功的响应,关注操作成功条目数 + * + * @param successNum 操作成功的条目数 + * @return 响应 + */ + public static Object success(int successNum) { + return Response.success(successNum); + } + + /** + * 失败的响应 + * + * @param errorCode 错误码 + * @param errorMsg 错误信息 + * @return 响应 + */ + public static Object error(String errorCode, String errorMsg) { + return Response.error(errorCode, errorMsg); + } + + /** + * 失败的响应 + * + * @param status 状态码 + * @param errorCode 错误码 + * @param errorMsg 错误信息 + * @return 响应 + */ + public static Object error(int status, String errorCode, String errorMsg) { + return Response.error(status, errorCode, errorMsg); + } +} diff --git a/src/main/java/com/fanruan/api/web/TextGeneratorKit.java b/src/main/java/com/fanruan/api/web/TextGeneratorKit.java new file mode 100644 index 0000000..b1a279c --- /dev/null +++ b/src/main/java/com/fanruan/api/web/TextGeneratorKit.java @@ -0,0 +1,21 @@ +package com.fanruan.api.web; + +import com.fr.decision.web.i18n.I18nTextGenerator; +import com.fr.gen.TextGenerator; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-19 + */ +public class TextGeneratorKit { + + /** + * 创建一个用于国际化的文本输出器 + * + * @return 文本输出器 + */ + public static TextGenerator newI18nTextGenerator() { + return new I18nTextGenerator(); + } +}