diff --git a/README.md b/README.md
index 2d1c072..2a1dee8 100644
--- a/README.md
+++ b/README.md
@@ -140,3 +140,17 @@ RAP.getMode();
```bash
RAP.setMode(1);
```
+
+接口文档进阶
+--------------------------------------
+
+### 接口文档请求链接语法
+
+http://www.taobao.com/getItem?[callback]=foo
+
+`[callback]` 表示参数`callback`会用作JSONP的回调key,若实际请求为getItem?callback=foo,则返回结果为:foo({...});
+
+http://www.taobao.com/getREST?{path}=delete
+
+`{path}` 表示参数`path`会在RAP文档和开发环境进行接口对接时,需要考虑path参数,一般REST API会使用比较多。因为REST不同请求是通过参数区分的,比如getRest?path=delete, getRest?path=update, 而{path}表示两者为不同的接口。
+
diff --git a/UPDATELOG.md b/UPDATELOG.md
index 26d49fd..6b386ff 100644
--- a/UPDATELOG.md
+++ b/UPDATELOG.md
@@ -1,4 +1,5 @@
### rap v0.8.1 ###
+* [功能] 请求链接添加新语法url?[callback]=foo,用以支持自定义的JSONP callback key。 2014-03-24
* [功能] 请求链接添加新语法url?{action}=update,用以支持REST_API的带参数查询接口的功能。 2014-03-20
* [BUG] 修复pageTester控制台请求链接带参数时的请求路径错误。 2014-03-20
* [BUG] 修复@mock空标签导致MOCK异常的问题。 2014-03-20
diff --git a/WebContent/demo/mock.plugin/index.htm b/WebContent/demo/mock.plugin/index.htm
index a651c7f..1d57c5d 100644
--- a/WebContent/demo/mock.plugin/index.htm
+++ b/WebContent/demo/mock.plugin/index.htm
@@ -12,7 +12,7 @@
+ src="http://rap.alibaba-inc.com/rap.plugin.js?projectId=79&mode=0">
diff --git a/src/com/taobao/rigel/rap/mock/service/MockMgr.java b/src/com/taobao/rigel/rap/mock/service/MockMgr.java
index 460b9a6..7f124e1 100644
--- a/src/com/taobao/rigel/rap/mock/service/MockMgr.java
+++ b/src/com/taobao/rigel/rap/mock/service/MockMgr.java
@@ -1,6 +1,7 @@
package com.taobao.rigel.rap.mock.service;
import java.io.UnsupportedEncodingException;
+import java.util.Map;
public interface MockMgr {
/**
@@ -8,24 +9,31 @@ public interface MockMgr {
*
* @param projectId
* @param pattern
+ * @param options
* @return JSON String
- * @throws UnsupportedEncodingException
+ * @throws UnsupportedEncodingException
*/
- public String generateData(int projectId, String pattern) throws UnsupportedEncodingException;
+ public String generateData(int projectId, String pattern,
+ Map options) throws UnsupportedEncodingException;
/**
* modify mock data of parameters based on mockData
- * @param actionId identifier of the action to be modified
- * @param mockData mock data string
- * example: response.param1.subParam=@format=x.xxxx_AND_response.
- * param2=@value=1_AND_response.param3.subParam.subSubParam=@length=7
+ *
+ * @param actionId
+ * identifier of the action to be modified
+ * @param mockData
+ * mock data string example:
+ * response.param1.subParam=@format=x.xxxx_AND_response.
+ * param2=@value=
+ * 1_AND_response.param3.subParam.subSubParam=@length=7
* @return number of rows affected
*
*/
public int modify(int actionId, String mockData);
/**
- * clear all mock data of object in specified projet
+ * clear all mock data of object in specified project
+ *
* @param projectId
* @return number of rows affected
*/
@@ -36,18 +44,22 @@ public interface MockMgr {
*
* @param id
* @param pattern
+ * @param options
* @return
- * @throws UnsupportedEncodingException
+ * @throws UnsupportedEncodingException
*/
- public String generateRule(int id, String pattern) throws UnsupportedEncodingException;
-
+ public String generateRule(int id, String pattern,
+ Map options) throws UnsupportedEncodingException;
+
/**
* generate mockjs data
*
* @param id
* @param pattern
+ * @param options
* @return
- * @throws UnsupportedEncodingException
+ * @throws UnsupportedEncodingException
*/
- public String generateRuleData(int id, String pattern) throws UnsupportedEncodingException;
+ public String generateRuleData(int id, String pattern,
+ Map options) throws UnsupportedEncodingException;
}
diff --git a/src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java b/src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
index 7befdb1..ec53500 100644
--- a/src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
+++ b/src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
@@ -54,7 +54,8 @@ public void setProjectDao(ProjectDao projectDao) {
}
@Override
- public String generateData(int projectId, String pattern) throws UnsupportedEncodingException {
+ public String generateData(int projectId, String pattern,
+ Map options) throws UnsupportedEncodingException {
_num = 1;
String originalPattern = pattern;
System.out.println("pattern before processed:" + pattern);
@@ -69,8 +70,8 @@ public String generateData(int projectId, String pattern) throws UnsupportedEnco
.getMatchedActionList(projectId, pattern);
if (aList.size() == 0)
return "{\"isOk\":false, \"errMsg\":\"no matched action\"}";
-
- Action action = actionPick(aList, originalPattern);
+
+ Action action = actionPick(aList, originalPattern, options);
String desc = action.getDescription();
Set pList = action.getResponseParameterList();
@@ -130,15 +131,15 @@ public String generateData(int projectId, String pattern) throws UnsupportedEnco
}
@Override
- public String generateRuleData(int projectId, String pattern)
- throws UnsupportedEncodingException {
- String result = generateRule(projectId, pattern);
+ public String generateRuleData(int projectId, String pattern,
+ Map options) throws UnsupportedEncodingException {
+ String result = generateRule(projectId, pattern, options);
return MockjsRunner.renderMockjsRule(result);
}
@Override
- public String generateRule(int projectId, String pattern)
- throws UnsupportedEncodingException {
+ public String generateRule(int projectId, String pattern,
+ Map options) throws UnsupportedEncodingException {
String originalPattern = pattern;
_num = 1;
System.out.println("pattern before processed:" + pattern);
@@ -155,7 +156,7 @@ public String generateRule(int projectId, String pattern)
return "{\"isOk\":false, \"errMsg\":\"no matched action\"}";
}
- Action action = actionPick(aList, originalPattern);
+ Action action = actionPick(aList, originalPattern, options);
String desc = action.getDescription();
Set pList = action.getResponseParameterList();
@@ -214,8 +215,8 @@ public String generateRule(int projectId, String pattern)
return resultFilter(result);
}
- private Action actionPick(List actionList, String pattern)
- throws UnsupportedEncodingException {
+ private Action actionPick(List actionList, String pattern,
+ Map options) throws UnsupportedEncodingException {
Action result = actionList.get(0);
Map> requestParams = getUrlParameters(pattern);
for (Action action : actionList) {
@@ -224,6 +225,15 @@ private Action actionPick(List actionList, String pattern)
boolean hasSchema = false;
boolean isPassed = true;
for (String docParamKey : docActionParams.keySet()) {
+ if (docParamKey.contains("[") && docParamKey.contains("]")) {
+ String docParamKeyProcessed = docParamKey.substring(1,
+ docParamKey.length() - 1);
+ List list = requestParams.get(docParamKeyProcessed);
+ if (list != null && list.size() > 0) {
+ options.put("callback", list.get(0));
+ }
+ }
+
if (docParamKey.contains("{") && docParamKey.contains("}")) {
hasSchema = true;
String docParamKeyProcessed = docParamKey.substring(1,
diff --git a/src/com/taobao/rigel/rap/mock/web/action/MockAction.java b/src/com/taobao/rigel/rap/mock/web/action/MockAction.java
index f176e2c..f01a670 100644
--- a/src/com/taobao/rigel/rap/mock/web/action/MockAction.java
+++ b/src/com/taobao/rigel/rap/mock/web/action/MockAction.java
@@ -2,7 +2,9 @@
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import com.taobao.rigel.rap.common.ActionBase;
import com.taobao.rigel.rap.mock.service.MockMgr;
@@ -59,7 +61,8 @@ public String get_c() {
}
public void set_c(String _c) {
- this._c = _c;;
+ this._c = _c;
+ ;
}
public String getCallback() {
@@ -99,7 +102,7 @@ public void setContent(String content) {
public String getPattern() {
return pattern;
}
-
+
private String callbackFilter(String cb) {
if (cb == null) {
return "callback";
@@ -107,14 +110,15 @@ private String callbackFilter(String cb) {
if (cb.contains("_c=")) {
int startIndex = cb.indexOf("_c=") + 3;
int endIndex = cb.indexOf("&", startIndex);
- if (endIndex == -1) endIndex = cb.length();
+ if (endIndex == -1)
+ endIndex = cb.length();
cb = cb.substring(startIndex, endIndex);
return cb;
}
if (cb.contains("&")) {
cb = cb.substring(0, cb.indexOf("&"));
}
-
+
return cb;
}
@@ -135,25 +139,38 @@ public void setPattern(String pattern) {
}
public String createData() throws UnsupportedEncodingException {
+ Map options = new HashMap();
String _c = get_c();
+ String result = mockMgr.generateData(id, pattern, options);
+ if (options.get("callback") != null) {
+ _c = (String) options.get("callback");
+ callback = (String) options.get("callback");
+ }
+
if (callback != null && !callback.isEmpty()) {
- setContent(callback + "(" + mockMgr.generateData(id, pattern) + ")");
+ setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
- setContent(_c + "(" + mockMgr.generateData(id, pattern) + ")");
+ setContent(_c + "(" + result + ")");
} else {
- setContent(mockMgr.generateData(id, pattern));
+ setContent(result);
}
return SUCCESS;
}
-
+
public String createRule() throws UnsupportedEncodingException {
+ Map options = new HashMap();
String _c = get_c();
+ String result = mockMgr.generateRule(id, pattern, options);
+ if (options.get("callback") != null) {
+ _c = (String) options.get("callback");
+ callback = (String) options.get("callback");
+ }
if (callback != null && !callback.isEmpty()) {
- setContent(callback + "(" + mockMgr.generateRule(id, pattern) + ")");
+ setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
- setContent(_c + "(" + mockMgr.generateRule(id, pattern) + ")");
+ setContent(_c + "(" + result + ")");
} else {
- setContent(mockMgr.generateRule(id, pattern));
+ setContent(result);
}
return SUCCESS;
}
@@ -167,7 +184,7 @@ public String reset() {
setNum(mockMgr.reset(projectId));
return SUCCESS;
}
-
+
public String createPluginScript() {
List list = new ArrayList();
Project p = projectMgr.getProject(projectId);
@@ -183,15 +200,21 @@ public String createPluginScript() {
urlList = list;
return SUCCESS;
}
-
+
public String createMockjsData() throws UnsupportedEncodingException {
String _c = get_c();
+ Map options = new HashMap();
+ String result = mockMgr.generateRuleData(id, pattern, options);
+ if (options.get("callback") != null) {
+ _c = (String) options.get("callback");
+ callback = (String) options.get("callback");
+ }
if (callback != null && !callback.isEmpty()) {
- setContent(callback + "(" + mockMgr.generateRuleData(id, pattern) + ")");
+ setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
- setContent(_c + "(" + mockMgr.generateRuleData(id, pattern) + ")");
+ setContent(_c + "(" + result + ")");
} else {
- setContent(mockMgr.generateRuleData(id, pattern));
+ setContent(result);
}
return SUCCESS;
}