entry : rawSet) {
624 | String key = entry == null ? null : entry.getKey();
625 | if (key == null) { // value 为 null 有效
626 | continue;
627 | }
628 |
629 | String[] pathKeys = key.split("\\.");
630 | //逐层到达child的直接容器JSONObject parent
631 | int last = pathKeys.length - 1;
632 | M parent = apijsonReq;
633 | for (int i = 0; i < last; i++) {//一步一步到达指定位置
634 | M p = getJSONObject(parent, pathKeys[i]);
635 | if (p == null) {
636 | p = JSON.createJSONObject();
637 | parent.put(key, p);
638 | }
639 | parent = p;
640 | }
641 |
642 | parent.put(pathKeys[last], entry.getValue());
643 | }
644 | }
645 |
646 | // 没必要,已经是预设好的实际参数了,如果要 tag 就在 apijson 字段配置 apijsonReq.put(JSONRequest.KEY_TAG, tag);
647 |
648 | return parser.setNeedVerifyContent(false).parse(apijsonReq);
649 | }
650 | catch (Exception e) {
651 | return JSON.toJSONString(newErrorResult(e));
652 | }
653 | }
654 |
655 | //通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
656 |
657 |
658 |
659 |
660 | /**重新加载配置
661 | * @return
662 | * @see
663 | *
664 | {
665 | "type": "ALL", //重载对象,ALL, FUNCTION, REQUEST, ACCESS,非必须
666 | "phone": "13000082001",
667 | "verify": "1234567" //验证码,对应类型为 Verify.TYPE_RELOAD
668 | }
669 | *
670 | */
671 | public M reload(String type) {
672 | M result = newSuccessResult();
673 |
674 | boolean reloadAll = StringUtil.isEmpty(type, true) || "ALL".equals(type);
675 |
676 | if (reloadAll || "ACCESS".equals(type)) {
677 | try {
678 | if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_ROLE == false) {
679 | throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_ROLE == false 时不支持校验角色权限!" +
680 | "如需支持则设置 AbstractVerifier.ENABLE_VERIFY_ROLE = true !");
681 | }
682 |
683 | if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
684 | result.put(ACCESS_, APIJSONVerifier.initAccess());
685 | }
686 | } catch (ServerException e) {
687 | e.printStackTrace();
688 | result.put(ACCESS_, newErrorResult(e));
689 | }
690 | }
691 |
692 | if (reloadAll || "FUNCTION".equals(type)) {
693 | try {
694 | if (reloadAll == false && APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION == false) {
695 | throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
696 | " == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
697 | }
698 |
699 | if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) {
700 | result.put(FUNCTION_, APIJSONFunctionParser.init());
701 | }
702 | } catch (ServerException e) {
703 | e.printStackTrace();
704 | result.put(FUNCTION_, newErrorResult(e));
705 | }
706 | }
707 |
708 | if (reloadAll || "REQUEST".equals(type)) {
709 | try {
710 | if (reloadAll == false && APIJSONVerifier.ENABLE_VERIFY_CONTENT == false) {
711 | throw new UnsupportedOperationException("AbstractVerifier.ENABLE_VERIFY_CONTENT == false 时不支持校验请求传参内容!" +
712 | "如需支持则设置 AbstractVerifier.ENABLE_VERIFY_CONTENT = true !");
713 | }
714 |
715 | if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) {
716 | result.put(REQUEST_, APIJSONVerifier.initRequest());
717 | }
718 | } catch (ServerException e) {
719 | e.printStackTrace();
720 | result.put(REQUEST_, newErrorResult(e));
721 | }
722 | }
723 |
724 | return result;
725 | }
726 |
727 |
728 | /**用户登录
729 | * @param session
730 | * @param visitor
731 | * @param version
732 | * @param format
733 | * @param defaults
734 | * @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
735 | */
736 | public Object login(@NotNull HttpSession session, @NotNull Visitor visitor, Integer version, Boolean format, M defaults) {
737 | //登录状态保存至session
738 | session.setAttribute(VISITOR_ID, visitor.getId()); //用户id
739 | session.setAttribute(VISITOR_, visitor); //用户
740 | session.setAttribute(VERSION, version); //全局默认版本号
741 | session.setAttribute(FORMAT, format); //全局默认格式化配置
742 | session.setAttribute(DEFAULTS, defaults); //给每个请求JSON最外层加的字段
743 | return null;
744 | }
745 |
746 | /**退出登录,清空session
747 | * @param session
748 | * @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
749 | */
750 | public Object logout(@NotNull HttpSession session) {
751 | Object userId = APIJSONVerifier.getVisitorId(session);//必须在session.invalidate();前!
752 | Log.d(TAG, "logout userId = " + userId + "; session.getId() = " + (session == null ? null : session.getId()));
753 | session.invalidate();
754 | return null;
755 | }
756 |
757 |
758 |
759 | // public JSONMap listMethod(String request) {
760 | // if (Log.DEBUG == false) {
761 | // return APIJSONParser.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!"));
762 | // }
763 | // return MethodUtil.listMethod(request);
764 | // }
765 | //
766 | // public void invokeMethod(String request, HttpServletRequest servletRequest) {
767 | // AsyncContext asyncContext = servletRequest.startAsync();
768 | //
769 | // final boolean[] called = new boolean[] { false };
770 | // MethodUtil.Listener listener = new MethodUtil.Listener() {
771 | //
772 | // @Override
773 | // public void complete(JSONMap data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
774 | //
775 | // ServletResponse servletResponse = called[0] ? null : asyncContext.getResponse();
776 | // if (servletResponse == null) { // || servletResponse.isCommitted()) { // isCommitted 在高并发时可能不准,导致写入多次
777 | // Log.w(TAG, "invokeMethod listener.complete servletResponse == null || servletResponse.isCommitted() >> return;");
778 | // return;
779 | // }
780 | // called[0] = true;
781 | //
782 | // servletResponse.setCharacterEncoding(servletRequest.getCharacterEncoding());
783 | // servletResponse.setContentType(servletRequest.getContentType());
784 | // servletResponse.getWriter().println(data);
785 | // asyncContext.complete();
786 | // }
787 | // };
788 | //
789 | // if (Log.DEBUG == false) {
790 | // try {
791 | // listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!")));
792 | // }
793 | // catch (Exception e1) {
794 | // e1.printStackTrace();
795 | // asyncContext.complete();
796 | // }
797 | //
798 | // return;
799 | // }
800 | //
801 | //
802 | // try {
803 | // MethodUtil.invokeMethod(request, null, listener);
804 | // }
805 | // catch (Exception e) {
806 | // Log.e(TAG, "invokeMethod try { JSONMap req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
807 | // try {
808 | // listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
809 | // }
810 | // catch (Exception e1) {
811 | // e1.printStackTrace();
812 | // asyncContext.complete();
813 | // }
814 | // }
815 | // }
816 |
817 | }
818 |
--------------------------------------------------------------------------------
/src/main/java/apijson/framework/APIJSONCreator.java:
--------------------------------------------------------------------------------
1 | /*Copyright ©2016 APIJSON(https://github.com/APIJSON)
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.*/
14 |
15 | package apijson.framework;
16 |
17 | import apijson.orm.ParserCreator;
18 | import apijson.orm.SQLCreator;
19 | import apijson.orm.VerifierCreator;
20 |
21 | import java.util.List;
22 | import java.util.Map;
23 |
24 |
25 | /**APIJSON相关创建器
26 | * @author Lemon
27 | */
28 | public class APIJSONCreator, L extends List