Server:新增指定字段;优化权限验证提示;新增请求方法;Client:新增指定字段示例
This commit is contained in:
parent
086158c905
commit
b70ee76673
@ -30,23 +30,25 @@ public final class R {
|
||||
public static final int select_activity=0x7f030001;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int access_error=0x7f050008;
|
||||
public static final int access_permitted=0x7f050009;
|
||||
public static final int access_error=0x7f050009;
|
||||
public static final int access_permitted=0x7f05000a;
|
||||
public static final int action_settings=0x7f050003;
|
||||
public static final int app_name=0x7f050000;
|
||||
public static final int app_version=0x7f050001;
|
||||
public static final int array=0x7f050006;
|
||||
public static final int complex=0x7f050007;
|
||||
public static final int demo_array=0x7f05000e;
|
||||
public static final int demo_complex=0x7f05000f;
|
||||
public static final int demo_rely=0x7f05000d;
|
||||
public static final int demo_single=0x7f05000c;
|
||||
public static final int demo_wallet=0x7f050010;
|
||||
public static final int demo_wallet_with_access=0x7f050011;
|
||||
public static final int array=0x7f050007;
|
||||
public static final int columns=0x7f050005;
|
||||
public static final int complex=0x7f050008;
|
||||
public static final int demo_array=0x7f050010;
|
||||
public static final int demo_columns=0x7f05000e;
|
||||
public static final int demo_complex=0x7f050011;
|
||||
public static final int demo_rely=0x7f05000f;
|
||||
public static final int demo_single=0x7f05000d;
|
||||
public static final int demo_wallet=0x7f050012;
|
||||
public static final int demo_wallet_with_access=0x7f050013;
|
||||
public static final int hello_world=0x7f050002;
|
||||
public static final int query=0x7f05000a;
|
||||
public static final int query_error=0x7f05000b;
|
||||
public static final int rely=0x7f050005;
|
||||
public static final int query=0x7f05000b;
|
||||
public static final int query_error=0x7f05000c;
|
||||
public static final int rely=0x7f050006;
|
||||
public static final int single=0x7f050004;
|
||||
}
|
||||
public static final class style {
|
||||
|
@ -17,6 +17,15 @@
|
||||
android:onClick="selectSingle"
|
||||
android:text="@string/demo_single" />
|
||||
|
||||
<TextView
|
||||
style="@style/select_name"
|
||||
android:text="@string/columns" />
|
||||
|
||||
<Button
|
||||
style="@style/select_json"
|
||||
android:onClick="selectColumns"
|
||||
android:text="@string/demo_columns" />
|
||||
|
||||
<TextView
|
||||
style="@style/select_name"
|
||||
android:text="@string/rely" />
|
||||
|
@ -6,7 +6,8 @@
|
||||
<string name="hello_world">Hello world!</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="single">单个Object</string>
|
||||
<string name="rely">Object之间的依赖</string>
|
||||
<string name="columns">指定字段</string>
|
||||
<string name="rely">向上依赖</string>
|
||||
<string name="array">Object数组</string>
|
||||
<string name="complex">复杂结构</string>
|
||||
<string name="access_error">没有访问权限</string>
|
||||
|
@ -6,6 +6,7 @@
|
||||
<string name="hello_world">Hello world!</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="single">Single</string>
|
||||
<string name="columns">Columns</string>
|
||||
<string name="rely">Rely</string>
|
||||
<string name="array">Array</string>
|
||||
<string name="complex">Complex</string>
|
||||
@ -14,6 +15,7 @@
|
||||
<string name="query">Query</string>
|
||||
<string name="query_error">There may be something wrong,you can follow by the steps:\n\n1.Check your net connection\n\n2.Check the url whether it\'s an available ipv4 address\n\n3.Long click the [Query] button to open the request by web browser\n\n4.Check logs outputed on the target server\n\n5.Try again</string>
|
||||
<string name="demo_single">{\n   \"User\":{\n      \"id\":38710\n   }\n}</string>
|
||||
<string name="demo_columns">{\n   \"User\":{\n      \"columns\":\"id,name,phone\",\n      \"id\":38710\n   }\n}</string>
|
||||
<string name="demo_rely">{\n   \"User\":{\n      \"id\":70793\n   },\n   \"Work\":{\n      \"userId\":\"User/id\"\n   }\n}</string>
|
||||
<string name="demo_array">{\n   \"User[]\":{\n      \"count\":5,\n      \"page\":1,\n      \"User\":{\n      }\n   }\n}</string>
|
||||
<string name="demo_complex">{\n   \"[]\":{\n      \"count\":2,\n      \"User\":{\n         \"sex\":0\n      },\n      \"Work\":{\n         \"userId\":\"/User/id\"\n      },\n      \"Comment[]\":{\n         \"count\":3,\n         \"Comment\":{\n            \"workId\":\"[]/Work/id\"\n          }\n      }\n   }\n}</string>
|
||||
|
@ -29,6 +29,12 @@ public class RequestUtil {
|
||||
public static JSONObject newSingleRequest() {
|
||||
return new JSONRequest(new User((long) 38710));
|
||||
}
|
||||
|
||||
public static JSONObject newColumnsRequest() {
|
||||
JSONObject object = new JSONObject(new User((long) 38710));
|
||||
object.setColumns("id,name,phone");//StringUtil.getString(new String[]{"id", "name", "phone"}));//
|
||||
return new JSONRequest(User.class.getSimpleName(), object);
|
||||
}
|
||||
|
||||
public static JSONObject newRelyRequest() {
|
||||
JSONRequest request = new JSONRequest();
|
||||
|
@ -73,11 +73,12 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
|
||||
|
||||
|
||||
public static final int TYPE_SINGLE = 0;
|
||||
public static final int TYPE_RELY = 1;
|
||||
public static final int TYPE_ARRAY = 2;
|
||||
public static final int TYPE_COMPLEX = 3;
|
||||
public static final int TYPE_ACCESS_ERROR = 4;
|
||||
public static final int TYPE_ACCESS_PERMITTED = 5;
|
||||
public static final int TYPE_COLUMNS = 1;
|
||||
public static final int TYPE_RELY = 2;
|
||||
public static final int TYPE_ARRAY = 3;
|
||||
public static final int TYPE_COMPLEX = 4;
|
||||
public static final int TYPE_ACCESS_ERROR = 5;
|
||||
public static final int TYPE_ACCESS_PERMITTED = 6;
|
||||
|
||||
|
||||
private int type = TYPE_SINGLE;
|
||||
@ -144,6 +145,9 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
|
||||
case TYPE_SINGLE:
|
||||
request = JSON.toJSONString(RequestUtil.newSingleRequest());
|
||||
break;
|
||||
case TYPE_COLUMNS:
|
||||
request = JSON.toJSONString(RequestUtil.newColumnsRequest());
|
||||
break;
|
||||
case TYPE_RELY:
|
||||
request = JSON.toJSONString(RequestUtil.newRelyRequest());
|
||||
break;
|
||||
|
@ -48,6 +48,10 @@ public class SelectActivity extends Activity {
|
||||
select(QueryActivity.TYPE_SINGLE);
|
||||
}
|
||||
|
||||
public void selectColumns(View v) {
|
||||
select(QueryActivity.TYPE_COLUMNS);
|
||||
}
|
||||
|
||||
public void selectRely(View v) {
|
||||
select(QueryActivity.TYPE_RELY);
|
||||
}
|
||||
|
@ -107,7 +107,31 @@ public class StringUtil {
|
||||
public static String getString(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
|
||||
/**获取string,为null则返回""
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
public static String getString(String[] array) {
|
||||
return getString(array, null);
|
||||
}
|
||||
/**获取string,为null则返回""
|
||||
* @param array
|
||||
* @param split
|
||||
* @return
|
||||
*/
|
||||
public static String getString(String[] array, String split) {
|
||||
String s = "";
|
||||
if (array != null) {
|
||||
if (split == null) {
|
||||
split = ",";
|
||||
}
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
s += ((i >= array.length - 1 ? "" : split) + array[i]);
|
||||
}
|
||||
}
|
||||
return getString(s);
|
||||
}
|
||||
|
||||
//获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
//获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||
|
@ -35,6 +35,15 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
|
||||
public JSONObject() {
|
||||
super(true);
|
||||
}
|
||||
/**transfer Object to JSONObject
|
||||
* @param object
|
||||
*/
|
||||
public JSONObject(Object object) {
|
||||
this(JSON.toJSONString(object));
|
||||
}
|
||||
/**parse JSONObject with JSON String
|
||||
* @param json
|
||||
*/
|
||||
public JSONObject(String json) {
|
||||
this(JSON.parseObject(json));
|
||||
}
|
||||
@ -48,6 +57,7 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @return if value is String, return URLDecoder.decode((String) value, UTF_8);
|
||||
@ -100,6 +110,21 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final String KEY_COLUMNS = "columns";
|
||||
/**set columns need to be returned
|
||||
* @param columns "column0,column1,column2..."
|
||||
* @return
|
||||
*/
|
||||
public JSONObject setColumns(String columns) {
|
||||
put(KEY_COLUMNS, columns);
|
||||
return this;
|
||||
}
|
||||
public String getColumns() {
|
||||
return getString(KEY_COLUMNS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//array object <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||
|
@ -93,6 +93,30 @@ public class StringUtil {
|
||||
public static String getString(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
/**获取string,为null则返回""
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
public static String getString(String[] array) {
|
||||
return getString(array, null);
|
||||
}
|
||||
/**获取string,为null则返回""
|
||||
* @param array
|
||||
* @param split
|
||||
* @return
|
||||
*/
|
||||
public static String getString(String[] array, String split) {
|
||||
String s = "";
|
||||
if (array != null) {
|
||||
if (split == null) {
|
||||
split = ",";
|
||||
}
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
s += ((i >= array.length - 1 ? "" : split) + array[i]);
|
||||
}
|
||||
}
|
||||
return getString(s);
|
||||
}
|
||||
|
||||
//获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
@ -251,7 +275,7 @@ public class StringUtil {
|
||||
|
||||
return p.matcher(email).matches();
|
||||
}
|
||||
|
||||
|
||||
/**判断是否全是数字
|
||||
* @param s
|
||||
* @return
|
||||
@ -508,7 +532,7 @@ public class StringUtil {
|
||||
public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3;
|
||||
public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4;
|
||||
public static final String[] PRICE_FORMATS = {
|
||||
"", "¥", "元", "¥ ", " 元"
|
||||
"", "¥", "元", "¥ ", " 元"
|
||||
};
|
||||
|
||||
/**获取价格,保留两位小数
|
||||
|
@ -14,6 +14,8 @@ limitations under the License.*/
|
||||
|
||||
package zuo.biao.apijson.server;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**mock test of client
|
||||
* @author Lemon
|
||||
*/
|
||||
@ -54,7 +56,7 @@ public class ClientTest {
|
||||
// "}");
|
||||
|
||||
//已经过bejson校验和传输校验
|
||||
new RequestParser().parse("{" +
|
||||
new RequestParser(RequestMethod.GET).parse("{" +
|
||||
"\"[]\": {" +
|
||||
"\"page\": 1, " +
|
||||
"\"count\": 10," +
|
||||
|
@ -14,8 +14,12 @@ limitations under the License.*/
|
||||
|
||||
package zuo.biao.apijson.server;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -29,21 +33,20 @@ import zuo.biao.apijson.JSON;
|
||||
@RequestMapping("")
|
||||
public class Controller {
|
||||
|
||||
@RequestMapping("get/{request}")
|
||||
public String get(@PathVariable String request) {
|
||||
System.out.println("request = " + request);
|
||||
JSONObject object = new RequestParser().parse(request);
|
||||
System.out.println("return " + JSON.toJSONString(object));
|
||||
return JSON.toJSONString(object);
|
||||
}
|
||||
@RequestMapping("get/{request}")
|
||||
public String get(@PathVariable String request) {
|
||||
System.out.println("get/request = " + request);
|
||||
JSONObject object = new RequestParser(RequestMethod.GET).parse(request);
|
||||
System.out.println("get/return " + JSON.toJSONString(object));
|
||||
return JSON.toJSONString(object);
|
||||
}
|
||||
|
||||
// @RequestMapping(value="/request",produces="application/json")
|
||||
// public String get2(@RequestBody JSONObject request){
|
||||
// return JSON.toJSONString(request);
|
||||
// }
|
||||
// @RequestMapping(method = RequestMethod.GET)
|
||||
// public String create(@RequestBody @Valid JSONObject request) {
|
||||
// return JSON.toJSONString(request);
|
||||
// }
|
||||
@RequestMapping(value="post/", method = RequestMethod.POST)
|
||||
public String post(@PathVariable String request) {
|
||||
System.out.println("post/request = " + request);
|
||||
JSONObject object = new RequestParser(RequestMethod.POST).parse(request);
|
||||
System.out.println("post/return " + JSON.toJSONString(object));
|
||||
return JSON.toJSONString(object);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,45 +18,61 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import zuo.biao.apijson.JSON;
|
||||
import zuo.biao.apijson.StringUtil;
|
||||
|
||||
/**config model for query
|
||||
* @author Lemon
|
||||
*/
|
||||
public class QueryConfig {
|
||||
|
||||
private RequestMethod method;
|
||||
private String table;
|
||||
private String[] columns;
|
||||
private String[][] values;
|
||||
private String columns;
|
||||
private String values;
|
||||
private Map<String, Object> where;
|
||||
private int limit;
|
||||
private int page;
|
||||
private int position;
|
||||
|
||||
public QueryConfig() {
|
||||
public QueryConfig(RequestMethod method) {
|
||||
setMethod(method);
|
||||
}
|
||||
public QueryConfig(String table) {
|
||||
this();
|
||||
public QueryConfig(RequestMethod method, String table) {
|
||||
this(method);
|
||||
setTable(table);
|
||||
}
|
||||
public QueryConfig(String table, Map<String, Object> where) {
|
||||
this(table);
|
||||
public QueryConfig(RequestMethod method, String table, Map<String, Object> where) {
|
||||
this(method, table);
|
||||
setWhere(where);
|
||||
}
|
||||
public QueryConfig(String table, String[] columns, String[][] values) {
|
||||
this(table);
|
||||
public QueryConfig(RequestMethod method, String table, String columns, String values) {
|
||||
this(method, table);
|
||||
setColumns(columns);
|
||||
setValues(values);
|
||||
}
|
||||
public QueryConfig(int limit, int page) {
|
||||
this();
|
||||
public QueryConfig(RequestMethod method, String table, String[] columns, String[][] values) {
|
||||
this(method, table);
|
||||
setColumns(columns);
|
||||
setValues(values);
|
||||
}
|
||||
public QueryConfig(RequestMethod method, int limit, int page) {
|
||||
this(method);
|
||||
setLimit(limit);
|
||||
setPage(page);
|
||||
}
|
||||
|
||||
|
||||
public RequestMethod getMethod() {
|
||||
return method;
|
||||
}
|
||||
public QueryConfig setMethod(RequestMethod method) {
|
||||
this.method = method;
|
||||
return this;
|
||||
}
|
||||
public String getTable() {
|
||||
return table;
|
||||
}
|
||||
@ -64,17 +80,39 @@ public class QueryConfig {
|
||||
this.table = table;
|
||||
return this;
|
||||
}
|
||||
public String[] getColumns() {
|
||||
public String getColumns() {
|
||||
return columns;
|
||||
}
|
||||
public QueryConfig setColumns(String[] columns) {
|
||||
this.columns = columns;
|
||||
return setColumns(StringUtil.getString(columns));
|
||||
}
|
||||
public QueryConfig setColumns(String columns) {
|
||||
columns = StringUtil.getTrimedString(columns);
|
||||
this.columns = columns.endsWith(",") ? columns.substring(0, columns.length() - 1) : columns;
|
||||
return this;
|
||||
}
|
||||
public String[][] getValues() {
|
||||
private String getColumnsString() {
|
||||
return StringUtil.isNotEmpty(columns, true) ? columns : "*";
|
||||
}
|
||||
|
||||
public String getValues() {
|
||||
return values;
|
||||
}
|
||||
public String getValuesString() {
|
||||
return values;
|
||||
}
|
||||
public QueryConfig setValues(String[][] values) {
|
||||
String s = "";
|
||||
if (values != null && values.length > 0) {
|
||||
String[] items = new String[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
items[i] = "(" + StringUtil.getString(values[i]) + ")";
|
||||
}
|
||||
s = StringUtil.getString(items);
|
||||
}
|
||||
return setValues(s);
|
||||
}
|
||||
public QueryConfig setValues(String values) {
|
||||
this.values = values;
|
||||
return this;
|
||||
}
|
||||
@ -158,7 +196,7 @@ public class QueryConfig {
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static synchronized QueryConfig getQueryConfig(String table, JSONObject request) {
|
||||
public static synchronized QueryConfig getQueryConfig(RequestMethod method, String table, JSONObject request) {
|
||||
Set<String> set = request == null ? null : request.keySet();
|
||||
Map<String, Object> transferredRequest = null;
|
||||
if (set != null) {
|
||||
@ -169,9 +207,9 @@ public class QueryConfig {
|
||||
}
|
||||
}
|
||||
}
|
||||
return new QueryConfig(table).setWhere(transferredRequest);
|
||||
return new QueryConfig(method, table).setWhere(transferredRequest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
@ -183,7 +221,24 @@ public class QueryConfig {
|
||||
* @return
|
||||
*/
|
||||
public static String getSQL(QueryConfig config) {
|
||||
return config == null ? null : "select * from " + config.getTable() + config.getWhereString() + config.getLimitString();
|
||||
if (config == null) {
|
||||
System.out.println("QueryConfig: getSQL config == null >> return null;");
|
||||
return null;
|
||||
}
|
||||
if (config.getMethod() == null) {
|
||||
config.setMethod(RequestMethod.GET);
|
||||
}
|
||||
switch (config.getMethod()) {
|
||||
case GET:
|
||||
return "select "+ config.getColumnsString() + " from " + config.getTable()
|
||||
+ config.getWhereString() + config.getLimitString();
|
||||
case POST:
|
||||
return "insert into " + config.getTable() + config.getWhereString() + " values " + config.getValuesString();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import zuo.biao.apijson.JSON;
|
||||
@ -36,14 +38,21 @@ public class RequestParser {
|
||||
private static final String TAG = "RequestParser: ";
|
||||
|
||||
public static final String SEPARATOR = StringUtil.SEPARATOR;
|
||||
public static final String KEY_COLUMNS = "columns";
|
||||
|
||||
private RequestMethod requestMethod;
|
||||
public RequestParser(RequestMethod requestMethod) {
|
||||
this.requestMethod = requestMethod;
|
||||
}
|
||||
|
||||
|
||||
private JSONObject requestObject;
|
||||
|
||||
private boolean parseRelation;
|
||||
private Map<String, String> relationMap;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param json
|
||||
*/
|
||||
@ -96,7 +105,7 @@ public class RequestParser {
|
||||
|
||||
QueryConfig config = StringUtil.isNumer(name) ? parentConfig : null;
|
||||
if (config == null) {
|
||||
config = new QueryConfig(name);
|
||||
config = new QueryConfig(requestMethod, name);
|
||||
}
|
||||
boolean nameIsNumber = StringUtil.isNumer(name);
|
||||
final int position = nameIsNumber ? Integer.valueOf(0 + StringUtil.getNumber(name)) : 0;
|
||||
@ -145,7 +154,8 @@ public class RequestParser {
|
||||
if (containRelation == false && isObjectKey(name)) {
|
||||
if (parseRelation == false || isInRelationMap(path)) {//避免覆盖原来已经获取的
|
||||
// relationMap.remove(path);
|
||||
QueryConfig config2 = getQueryConfig(name, transferredRequest);
|
||||
transferredRequest.remove(KEY_COLUMNS);
|
||||
QueryConfig config2 = getQueryConfig(name, transferredRequest).setColumns(request.getString(KEY_COLUMNS));
|
||||
if (parentConfig != null) {
|
||||
config2.setLimit(parentConfig.getLimit()).setPage(parentConfig.getPage())
|
||||
.setPosition(parentConfig.getPosition());//避免position > 0的object获取不到
|
||||
@ -207,7 +217,7 @@ public class RequestParser {
|
||||
// }
|
||||
System.out.println(TAG + "getArray page = " + page + "; count = " + count);
|
||||
|
||||
QueryConfig config = new QueryConfig(count, page);
|
||||
QueryConfig config = new QueryConfig(requestMethod, count, page);
|
||||
|
||||
Set<String> set = request.keySet();
|
||||
JSONObject transferredRequest = new JSONObject(true);
|
||||
@ -411,7 +421,7 @@ public class RequestParser {
|
||||
*/
|
||||
private synchronized JSONObject getSQLObject(QueryConfig config) throws AccessException {
|
||||
System.out.println("getSQLObject config = " + JSON.toJSONString(config));
|
||||
AccessVerifier.verify(requestObject, config == null ? null : config.getTable());
|
||||
AccessVerifier.verify(requestMethod, requestObject, config == null ? null : config.getTable());
|
||||
return QueryHelper.getInstance().select(config);//QueryHelper2.getInstance().select(config);//
|
||||
}
|
||||
|
||||
@ -421,7 +431,7 @@ public class RequestParser {
|
||||
* @return
|
||||
*/
|
||||
public QueryConfig getQueryConfig(String table, JSONObject request) {
|
||||
return QueryConfig.getQueryConfig(table, request);
|
||||
return QueryConfig.getQueryConfig(requestMethod, table, request);
|
||||
}
|
||||
/**获取查询配置
|
||||
* @param table
|
||||
@ -429,7 +439,7 @@ public class RequestParser {
|
||||
* @return
|
||||
*/
|
||||
public QueryConfig newQueryConfig(String table, JSONObject request) {
|
||||
return QueryConfig.getQueryConfig(table, request);
|
||||
return QueryConfig.getQueryConfig(requestMethod, table, request);
|
||||
}
|
||||
/**把parentConfig的array属性继承下来
|
||||
* @param config
|
||||
|
@ -2,6 +2,8 @@ package zuo.biao.apijson.server.sql;
|
||||
|
||||
import java.rmi.AccessException;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import zuo.biao.apijson.StringUtil;
|
||||
@ -23,34 +25,37 @@ public class AccessVerifier {
|
||||
public static final String[] PAY_ACCESS_TABLE_NAMES = {"Wallet"};
|
||||
|
||||
/**验证权限是否通过
|
||||
* @param method
|
||||
* @param request
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
public static boolean verify(JSONObject request, String tableName) throws AccessException {
|
||||
public static boolean verify(RequestMethod method, JSONObject request, String tableName) throws AccessException {
|
||||
try {
|
||||
verify(request, getAccessId(tableName));
|
||||
verify(method, request, getAccessId(tableName));
|
||||
} catch (AccessException e) {
|
||||
throw new AccessException(TAG + "verify tableName = " + tableName + ", error = " + e.getMessage());
|
||||
throw new AccessException(TAG + tableName + " : " + e.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**验证权限是否通过
|
||||
* @param method
|
||||
* @param request
|
||||
* @param accessId 可以直接在代码里写ACCESS_LOGIN等,或者建一个Access表,包括id和需要改权限的table的tableName列表
|
||||
* @return
|
||||
* @throws AccessException
|
||||
*/
|
||||
public static boolean verify(JSONObject request, int accessId) throws AccessException {
|
||||
public static boolean verify(RequestMethod method, JSONObject request, int accessId) throws AccessException {
|
||||
if (accessId < 0 || request == null) {
|
||||
return true;
|
||||
}
|
||||
long currentUserId = request.getLongValue(KEY_CURRENT_USER_ID);
|
||||
if (currentUserId <= 0) {
|
||||
throw new AccessException(TAG + "verify accessId = " + accessId
|
||||
System.out.println(TAG + "verify accessId = " + accessId
|
||||
+ " >> currentUserId <= 0, currentUserId = " + currentUserId);
|
||||
throw new AccessException(TAG + "请设置"+ KEY_CURRENT_USER_ID + "和对应的password!");
|
||||
}
|
||||
String password;
|
||||
|
||||
@ -58,16 +63,18 @@ public class AccessVerifier {
|
||||
case ACCESS_LOGIN:
|
||||
password = StringUtil.getString(request.getString(KEY_LOGIN_PASSWORD));
|
||||
if (password.equals(StringUtil.getString(getLoginPassword(currentUserId))) == false) {
|
||||
throw new AccessException(TAG + "verify accessId = " + accessId
|
||||
System.out.println(TAG + "verify accessId = " + accessId
|
||||
+ " >> currentUserId or loginPassword error"
|
||||
+ " currentUserId = " + currentUserId + ", loginPassword = " + password);
|
||||
throw new AccessException(TAG + KEY_CURRENT_USER_ID + "或" + KEY_LOGIN_PASSWORD + "错误!");
|
||||
}
|
||||
case ACCESS_PAY:
|
||||
password = StringUtil.getString(request.getString(KEY_PAY_PASSWORD));
|
||||
if (password.equals(StringUtil.getString(getPayPassword(currentUserId))) == false) {
|
||||
throw new AccessException(TAG + "verify accessId = " + accessId
|
||||
System.out.println(TAG + "verify accessId = " + accessId
|
||||
+ " >> currentUserId or payPassword error"
|
||||
+ " currentUserId = " + currentUserId + ", payPassword = " + password);
|
||||
throw new AccessException(TAG + KEY_CURRENT_USER_ID + "或" + KEY_PAY_PASSWORD + "错误!");
|
||||
}
|
||||
default:
|
||||
return true;
|
||||
|
@ -90,8 +90,8 @@ public class QueryHelper {
|
||||
metaData = connection.getMetaData();
|
||||
}
|
||||
|
||||
List<String> list = getColumnList(config.getTable());
|
||||
if (list == null || list.isEmpty()) {
|
||||
String[] columnArray = getColumnArray(config);
|
||||
if (columnArray == null || columnArray.length <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -108,8 +108,8 @@ public class QueryHelper {
|
||||
}
|
||||
object = new JSONObject(true);
|
||||
try {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
object.put(list.get(i), rs.getObject(rs.findColumn(list.get(i))));
|
||||
for (int i = 0; i < columnArray.length; i++) {
|
||||
object.put(columnArray[i], rs.getObject(rs.findColumn(columnArray[i])));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(TAG + "select while (rs.next()){ ... >> try { object.put(list.get(i), ..." +
|
||||
@ -134,8 +134,16 @@ public class QueryHelper {
|
||||
* @param table
|
||||
* @return
|
||||
*/
|
||||
public List<String> getColumnList(String table) {
|
||||
public String[] getColumnArray(QueryConfig config) {
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
String columns = config.getColumns();
|
||||
if (StringUtil.isNotEmpty(columns, true)) {
|
||||
return columns.contains(",") ? columns.split(",") : new String[]{columns};
|
||||
}
|
||||
List<String> list = new ArrayList<String>();
|
||||
String table = config.getTable();
|
||||
ResultSet rs;
|
||||
try {
|
||||
rs = metaData.getColumns(YOUR_MYSQL_SCHEMA, null, table, "%");
|
||||
@ -149,6 +157,6 @@ public class QueryHelper {
|
||||
"} catch (Exception e) {\n" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
return list.toArray(new String[]{});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user