Skip to content

Commit

Permalink
Server: 解决 APIJSONFinal 缺 libs 目录及相关 jar 包;同步 APIJSONBoot 的代码到 APIJSO…
Browse files Browse the repository at this point in the history
…NFinal
  • Loading branch information
TommyLemon committed Aug 27, 2019
1 parent 0d42fef commit b68e4f4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
2 changes: 0 additions & 2 deletions APIJSON-Java-Server/APIJSONFinal/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ plan.txt

*.class

# Package Files #
*.jar


*.bak
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@
public class DemoFunction extends RemoteFunction {
private static final String TAG = "DemoFunction";

private final RequestMethod method;
private final HttpSession session;
public DemoFunction(RequestMethod method, HttpSession session) {
this.method = method;
public DemoFunction(RequestMethod method, String tag, int version, HttpSession session) {
super(method, tag, version);
this.session = session;
}

Expand Down Expand Up @@ -78,11 +77,11 @@ public static void test() throws Exception {
request.put("object", object);


Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null, null).invoke("plus(i0,i1)", request));
Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null, null).invoke("countArray(array)", request));
Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null, null).invoke("isContain(array,id)", request));
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null, null).invoke("getFromArray(array,@position)", request));
Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null, null).invoke("getFromObject(object,key)", request));
Log.i(TAG, "plus(1,-2) = " + new DemoFunction(null, null, 1, null).invoke("plus(i0,i1)", request));
Log.i(TAG, "count([1,2,4,10]) = " + new DemoFunction(null, null, 1, null).invoke("countArray(array)", request));
Log.i(TAG, "isContain([1,2,4,10], 10) = " + new DemoFunction(null, null, 1, null).invoke("isContain(array,id)", request));
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + new DemoFunction(null, null, 1, null).invoke("getFromArray(array,@position)", request));
Log.i(TAG, "getFromObject({key:true}, key) = " + new DemoFunction(null, null, 1, null).invoke("getFromObject(object,key)", request));

}

Expand Down Expand Up @@ -112,7 +111,7 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
request.putAll(functionItem.toArray(0, 0, FUNCTION_));
} //Function[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


JSONObject response = new DemoParser(RequestMethod.GET, true).parseResponse(request);
if (JSONResponse.isSuccess(response) == false) {
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
Expand All @@ -135,15 +134,28 @@ public static JSONObject init(boolean shutdownWhenServerError) throws ServerExce
if (demo == null) {
onServerError("字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!", shutdownWhenServerError);
}
String name = item.getString("name");
if (demo.containsKey("result()") == false) {
demo.put("result()", getFunctionCall(item.getString("name"), item.getString("arguments")));
demo.put("result()", getFunctionCall(name, item.getString("arguments")));
}
demo.put(JSONRequest.KEY_COLUMN, "id,name,arguments,demo");
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
// demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));

FUNCTION_MAP.put(name, item); //必须在测试 invoke 前!

String[] methods = StringUtil.split(item.getString("methods"));
JSONObject r = new DemoParser(
methods == null || methods.length <= 0 ? RequestMethod.GET : RequestMethod.valueOf(methods[0])
, true
)
.setTag(item.getString(JSONRequest.KEY_TAG))
.setVersion(item.getIntValue(JSONRequest.KEY_VERSION))
.parseResponse(demo);

JSONObject r = new DemoParser(RequestMethod.GET, true).parseResponse(demo);
if (JSONResponse.isSuccess(r) == false) {
onServerError(JSONResponse.getMsg(r), shutdownWhenServerError);
}

}

return response;
Expand Down Expand Up @@ -223,10 +235,6 @@ public Object verifyURLList(@NotNull JSONObject request, @NotNull String urlList
* @throws Exception
*/
public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentId) throws Exception {
if (method != RequestMethod.DELETE) {
throw new UnsupportedOperationException("远程函数 deleteCommentOfMoment 只支持 DELETE 方法!");
}

long mid = rq.getLongValue(momentId);
if (mid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
return 0;
Expand Down Expand Up @@ -254,10 +262,6 @@ public int deleteCommentOfMoment(@NotNull JSONObject rq, @NotNull String momentI
* @return
*/
public int deleteChildComment(@NotNull JSONObject rq, @NotNull String toId) throws Exception {
if (method != RequestMethod.DELETE) { //TODO 如果这样的判断太多,可以把 DemoFunction 分成对应不同 RequestMethod 的 GetFunciton 等,创建时根据 method 判断用哪个
throw new UnsupportedOperationException("远程函数 deleteChildComment 只支持 DELETE 方法!");
}

long tid = rq.getLongValue(toId);
if (tid <= 0 || rq.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
return 0;
Expand Down Expand Up @@ -324,10 +328,10 @@ private JSONArray getChildCommentIdList(long tid) {
* @return
* @throws ServerException
*/
public JSONObject getFunctionDemo(@NotNull JSONObject request) throws ServerException {
public JSONObject getFunctionDemo(@NotNull JSONObject request) {
JSONObject demo = JSON.parseObject(request.getString("demo"));
if (demo == null) {
throw new ServerException("服务器内部错误,字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!");
demo = new JSONObject();
}
if (demo.containsKey("result()") == false) {
demo.put("result()", getFunctionCall(request.getString("name"), request.getString("arguments")));
Expand All @@ -349,15 +353,15 @@ public String getFunctionDetail(@NotNull JSONObject request) {
* @return
*/
private static String getFunctionCall(String name, String arguments) {
return name + "(" + arguments + ")";
return name + "(" + StringUtil.getTrimedString(arguments) + ")";
}

/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
* @param request
* @return JSONArray 只能用JSONArray,用long[]会在SQLConfig解析崩溃
* @throws Exception
*/
public JSONArray getIdList(@NotNull JSONObject request) throws Exception {
public JSONArray getIdList(@NotNull JSONObject request) {
return new JSONArray(new ArrayList<Object>(Arrays.asList(12, 15, 301, 82001, 82002, 38710)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package apijson.demo.server;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
Expand All @@ -22,14 +24,15 @@
import zuo.biao.apijson.RequestMethod;
import zuo.biao.apijson.StringUtil;
import zuo.biao.apijson.server.AbstractObjectParser;
import zuo.biao.apijson.server.Join;
import zuo.biao.apijson.server.Parser;
import zuo.biao.apijson.server.SQLConfig;


/**简化Parser,getObject和getArray(getArrayConfig)都能用
* @author Lemon
*/
public abstract class DemoObjectParser extends AbstractObjectParser {
public class DemoObjectParser extends AbstractObjectParser {

static {
COMPILE_MAP.put("phone", StringUtil.PATTERN_PHONE);
Expand All @@ -38,7 +41,7 @@ public abstract class DemoObjectParser extends AbstractObjectParser {
}



/**for single object
* @param parentPath
* @param request
Expand All @@ -48,7 +51,7 @@ public abstract class DemoObjectParser extends AbstractObjectParser {
public DemoObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, String name, SQLConfig arrayConfig, boolean isSubquery) throws Exception {
super(request, parentPath, name, arrayConfig, isSubquery);
}

@Override
public DemoObjectParser setMethod(RequestMethod method) {
super.setMethod(method);
Expand All @@ -61,9 +64,10 @@ public DemoObjectParser setParser(Parser<?> parser) {
return this;
}


@Override
public SQLConfig newSQLConfig(boolean isProcedure) throws Exception {
return DemoSQLConfig.newSQLConfig(method, table, alias, sqlRequest, joinList, isProcedure);
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
return DemoSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public JSONObject parseResponse(JSONObject request) {
@Override
public Object onFunctionParse(JSONObject json, String fun) throws Exception {
if (function == null) {
function = new DemoFunction(requestMethod, session);
function = new DemoFunction(requestMethod, tag, version, session);
}
return function.invoke(fun, json);
}
Expand All @@ -106,15 +106,7 @@ public DemoObjectParser createObjectParser(JSONObject request, String parentPath
return new DemoObjectParser(session, request, parentPath, name, arrayConfig, isSubquery) {

//TODO 删除,onPUTArrayParse改用MySQL函数JSON_ADD, JSON_REMOVE等
@Override
public JSONObject parseResponse(JSONRequest request) throws Exception {
DemoParser demoParser = new DemoParser(RequestMethod.GET);
demoParser.setSession(session);
// parser.setNoVerifyRequest(noVerifyRequest)
demoParser.setNoVerifyLogin(noVerifyLogin);
demoParser.setNoVerifyRole(noVerifyRole);
return demoParser.parseResponse(request);
}



// @Override
Expand Down

0 comments on commit b68e4f4

Please sign in to comment.