|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/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.demo; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +import javax.servlet.http.HttpSession; |
| 22 | + |
| 23 | +import com.alibaba.fastjson.JSONArray; |
| 24 | +import com.alibaba.fastjson.JSONObject; |
| 25 | + |
| 26 | +import apijson.JSONResponse; |
| 27 | +import apijson.NotNull; |
| 28 | +import apijson.RequestMethod; |
| 29 | +import apijson.RequestRole; |
| 30 | +import apijson.StringUtil; |
| 31 | +import apijson.framework.APIJSONFunctionParser; |
| 32 | +import apijson.orm.JSONRequest; |
| 33 | + |
| 34 | + |
| 35 | +/**可远程调用的函数类 |
| 36 | + * @author Lemon |
| 37 | + */ |
| 38 | +public class DemoFunctionParser extends APIJSONFunctionParser { |
| 39 | + public static final String TAG = "DemoFunctionParser"; |
| 40 | + |
| 41 | + public DemoFunctionParser() { |
| 42 | + this(null, null, 0, null, null); |
| 43 | + } |
| 44 | + public DemoFunctionParser(RequestMethod method, String tag, int version, JSONObject request, HttpSession session) { |
| 45 | + super(method, tag, version, request, session); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param current |
| 50 | + * @return |
| 51 | + * @throws Exception |
| 52 | + */ |
| 53 | + public Object verifyIdList(@NotNull JSONObject current, @NotNull String idList) throws Exception { |
| 54 | + Object obj = current.get(idList); |
| 55 | + if (obj instanceof Collection == false) { |
| 56 | + throw new IllegalArgumentException(idList + " 不符合 Array 类型! 结构必须是 [] !"); |
| 57 | + } |
| 58 | + JSONArray array = (JSONArray) obj; |
| 59 | + if (array != null) { |
| 60 | + for (int i = 0; i < array.size(); i++) { |
| 61 | + if (array.get(i) instanceof Long == false && array.get(i) instanceof Integer == false) { |
| 62 | + throw new IllegalArgumentException(idList + " 内字符 " + array.getString(i) + " 不符合 Long 类型!"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * @param request |
| 72 | + * @return |
| 73 | + * @throws Exception |
| 74 | + */ |
| 75 | + public Object verifyURLList(@NotNull JSONObject current, @NotNull String urlList) throws Exception { |
| 76 | + Object obj = current.get(urlList); |
| 77 | + if (obj instanceof Collection == false) { |
| 78 | + throw new IllegalArgumentException(urlList + " 不符合 Array 类型! 结构必须是 [] !"); |
| 79 | + } |
| 80 | + JSONArray array = (JSONArray) obj; |
| 81 | + if (array != null) { |
| 82 | + for (int i = 0; i < array.size(); i++) { |
| 83 | + if (StringUtil.isUrl(array.getString(i)) == false) { |
| 84 | + throw new IllegalArgumentException(urlList + " 内字符 " + array.getString(i) + " 不符合 URL 格式!"); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @param rq |
| 94 | + * @param momentId |
| 95 | + * @return |
| 96 | + * @throws Exception |
| 97 | + */ |
| 98 | + public int deleteCommentOfMoment(@NotNull JSONObject current, @NotNull String momentId) throws Exception { |
| 99 | + long mid = current.getLongValue(momentId); |
| 100 | + if (mid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + JSONRequest request = new JSONRequest(); |
| 105 | + |
| 106 | + //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 107 | + JSONRequest comment = new JSONRequest(); |
| 108 | + comment.put("momentId", mid); |
| 109 | + |
| 110 | + request.put("Comment", comment); |
| 111 | + //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 112 | + |
| 113 | + JSONObject rp = new DemoParser(RequestMethod.DELETE).setNeedVerify(false).parseResponse(request); |
| 114 | + |
| 115 | + JSONObject c = rp.getJSONObject("Comment"); |
| 116 | + return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /**删除评论的子评论 |
| 121 | + * @param rq |
| 122 | + * @param toId |
| 123 | + * @return |
| 124 | + */ |
| 125 | + public int deleteChildComment(@NotNull JSONObject current, @NotNull String toId) throws Exception { |
| 126 | + long tid = current.getLongValue(toId); |
| 127 | + if (tid <= 0 || current.getIntValue(JSONResponse.KEY_COUNT) <= 0) { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + //递归获取到全部子评论id |
| 132 | + |
| 133 | + JSONRequest request = new JSONRequest(); |
| 134 | + |
| 135 | + //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 136 | + JSONRequest comment = new JSONRequest(); |
| 137 | + comment.put("id{}", getChildCommentIdList(tid)); |
| 138 | + |
| 139 | + request.put("Comment", comment); |
| 140 | + //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 141 | + |
| 142 | + JSONObject rp = new DemoParser(RequestMethod.DELETE).setNeedVerify(false).parseResponse(request); |
| 143 | + |
| 144 | + JSONObject c = rp.getJSONObject("Comment"); |
| 145 | + return c == null ? 0 : c.getIntValue(JSONResponse.KEY_COUNT); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + private JSONArray getChildCommentIdList(long tid) { |
| 150 | + |
| 151 | + JSONArray arr = new JSONArray(); |
| 152 | + |
| 153 | + JSONRequest request = new JSONRequest(); |
| 154 | + |
| 155 | + //Comment-id[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 156 | + JSONRequest idItem = new JSONRequest(); |
| 157 | + |
| 158 | + //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 159 | + JSONRequest comment = new JSONRequest(); |
| 160 | + comment.put("toId", tid); |
| 161 | + comment.setColumn("id"); |
| 162 | + idItem.put("Comment", comment); |
| 163 | + //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 164 | + |
| 165 | + request.putAll(idItem.toArray(0, 0, "Comment-id")); |
| 166 | + //Comment-id[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 167 | + |
| 168 | + JSONObject rp = new DemoParser().setNeedVerify(false).parseResponse(request); |
| 169 | + |
| 170 | + JSONArray a = rp.getJSONArray("Comment-id[]"); |
| 171 | + if (a != null) { |
| 172 | + arr.addAll(a); |
| 173 | + |
| 174 | + JSONArray a2; |
| 175 | + for (int i = 0; i < a.size(); i++) { |
| 176 | + |
| 177 | + a2 = getChildCommentIdList(a.getLongValue(i)); |
| 178 | + if (a2 != null) { |
| 179 | + arr.addAll(a2); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return arr; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + /**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()" |
| 189 | + * @param request |
| 190 | + * @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃 |
| 191 | + * @throws Exception |
| 192 | + */ |
| 193 | + public JSONArray getIdList(@NotNull JSONObject current) { |
| 194 | + return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710))); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + /**TODO 仅用来测试 "key-()":"verifyAccess()" |
| 199 | + * @param request |
| 200 | + * @return |
| 201 | + * @throws Exception |
| 202 | + */ |
| 203 | + public Object verifyAccess(@NotNull JSONObject current) throws Exception { |
| 204 | + long userId = current.getLongValue(JSONRequest.KEY_USER_ID); |
| 205 | + RequestRole role = RequestRole.get(current.getString(JSONRequest.KEY_ROLE)); |
| 206 | + if (role == RequestRole.OWNER && userId != DemoVerifier.getVisitorId(getSession())) { |
| 207 | + throw new IllegalAccessException("登录用户与角色OWNER不匹配!"); |
| 208 | + } |
| 209 | + return null; |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +} |