Skip to content

Commit

Permalink
reverse match for RESTful API support, add new url pattern: reg:/path…
Browse files Browse the repository at this point in the history
…/biz[0-9]{4}/abc to match url like : /path/biz1234/abc, add quick url: /path/:id/cba to match url like:/path/1/cba
  • Loading branch information
Bosn committed May 22, 2014
1 parent 6a57adf commit 0c7711f
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Rigel Automation Platform
===

@version v0.8.1
@author Bosn(霍雍), wangjeaf(思竹)
@weibo http://weibo.com/bosn, http://github.com/wangjeaf
@mail [email protected], [email protected]
@version v0.8.1
@author RAP: [Bosn(霍雍)](http://weibo.com/bosn), [wangjeaf(思竹)](http://github.com/wangjeaf)
@dependency [MockJS](http://mockjs.com): 墨智, IF-TESTS: 蒋壮
@mail [email protected], [email protected]

什么是RAP?
--------------------------------------
Expand Down
16 changes: 9 additions & 7 deletions WebContent/tcom/template.rap.vm
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@
#if ($curUser)
<div class="pull-right">
<ul class="nav navbar-nav">
<li>
<!--
<li>
<a href="#" class="dropdown-toggle messages-trigger">消息<span class="label label-success">4</span></a>
<ul class="dropdown-menu dropdown-menu-right messages-dropdown">
<li>
<div class="messages-container">
<div class="msgs"></div>
<div class="btns">
</div>
</div>
<div class="messages-container">
<div class="msgs"></div>
<div class="btns">

</div>
</div>
</li>
</ul>
</li>
<li>
-->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">$curUser.name <b class="caret"></b></a>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="$newUrl.home">我的主页</a></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

Expand Down Expand Up @@ -187,7 +186,7 @@ private void addUserSetting(long userId, String key, String value) {
query.executeUpdate();
}

@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<Notification> getNotificationList(long userId) {
String sql = "SELECT * FROM tb_notification WHERE user_id = :userId";
Expand Down
22 changes: 11 additions & 11 deletions src/com/taobao/rigel/rap/account/web/action/AccountAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public String test() throws AddressException, InterruptedException {



// List<Notification> list = mgr.getNotificationList(getCurUserId());
// setJson(list.toString());
// mgr.updateUserSetting(1, "test", "123 567");
// String r1 = mgr.getUserSetting(1, "test");
// mgr.updateUserSetting(1, "test2", "abc");
// String r2 = "";
// Map<String, String> settings = mgr.getUserSettings(1);
// for (String s : settings.keySet()) {
// r2 += s + "|" + settings.get(s) + ",";
// }
// setJson(r1 + r2);
List<Notification> list = mgr.getNotificationList(getCurUserId());
setJson(list.toString());
mgr.updateUserSetting(1, "test", "123 567");
String r1 = mgr.getUserSetting(1, "test");
mgr.updateUserSetting(1, "test2", "abc");
String r2 = "";
Map<String, String> settings = mgr.getUserSettings(1);
for (String s : settings.keySet()) {
r2 += s + "|" + settings.get(s) + ",";
}
setJson(r1 + r2);
/**
String[] list = new String[2];
int i = 1;
Expand Down
23 changes: 22 additions & 1 deletion src/com/taobao/rigel/rap/common/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 为FE提供各类过滤字符串的接口
Expand Down Expand Up @@ -445,9 +447,28 @@ public static boolean isChinese(char c) {
}
return false;
}

public static String removeParamsInUrl(String url) {
return url.replaceAll("/:[^/]*", "");
}

public static String removeRealParamsInUrl(String url) {
return url.replaceAll("/[0-9]*/", "/");
}

/**
* regular expression matcher helper
*
* @param pattern
* regular expression
* @param str
* string to be matched
* @return
*/
public static boolean regMatch(String pattern, String str) {
Pattern p = Pattern.compile(pattern);
Matcher matcher = p.matcher(str);
return matcher.matches();
}

}
4 changes: 4 additions & 0 deletions src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ public String generateData(int projectId, String pattern,
if (pattern.contains("?")) {
pattern = pattern.substring(0, pattern.indexOf("?"));
}
/**
if (pattern.charAt(0) == '/') {
pattern = pattern.substring(1);
}
*/
// System.out.println("pattern processed:" + pattern);
List<Action> aList = projectMgr
.getMatchedActionList(projectId, pattern);
Expand Down Expand Up @@ -150,9 +152,11 @@ public String generateRule(int projectId, String pattern,
if (pattern.contains("?")) {
pattern = pattern.substring(0, pattern.indexOf("?"));
}
/**
if (pattern.charAt(0) == '/') {
pattern = pattern.substring(1);
}
*/
// System.out.println("pattern processed:" + pattern);
if (pattern.isEmpty()) {
return "{\"isOk\":false, \"errMsg\":\"pattern is empty. 路径为空,请检查RAP文档中的请求链接是否正确填写。\"}";
Expand Down
18 changes: 18 additions & 0 deletions src/com/taobao/rigel/rap/project/bo/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.taobao.rigel.rap.account.bo.Notification;
import com.taobao.rigel.rap.common.StringUtils;

public class Action implements java.io.Serializable {
Expand Down Expand Up @@ -262,5 +265,20 @@ public String getRequestUrlRel() {
}
return url;
}

public static List<Action> loadList(List<Map<String, Object>> result) {
List<Action> list = new ArrayList<Action>();
for (Map<String, Object> row : result) {
Action obj = new Action();
obj.setDescription((String)row.get("description"));
obj.setId((Integer)row.get("id"));
obj.setName((String)row.get("name"));
obj.setRemarks((String)row.get("remarks"));
obj.setRequestType((String)row.get("request_type"));
obj.setRequestUrl((String)row.get("request_url"));
list.add(obj);
}
return list;
}

}
58 changes: 56 additions & 2 deletions src/com/taobao/rigel/rap/project/dao/impl/ProjectDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,37 @@ public long getProjectListNum(User user) {
List<Long> list = query.list();
return list.get(0);
}



@SuppressWarnings("unchecked")
@Override
public List<Action> getMatchedActionList(int projectId, String pattern) {
List<Action> list = getActionListOfProject(projectId);
List<Action> result = new ArrayList<Action>();
for (Action action : list) {
String url = action.getRequestUrl();
if (url.startsWith("reg:")) { // regular pattern
if (StringUtils.regMatch(url.substring(4), pattern)) {
result.add(action);
}
} else if (url.contains(":")) {
String urlParamRemoved = StringUtils.removeParamsInUrl(url);
String realUrlParamRemoved = StringUtils.removeRealParamsInUrl(pattern);
if (urlParamRemoved.contains(realUrlParamRemoved)) {
result.add(action);
}
} else { // normal pattern
if (url.contains(pattern)) {
result.add(action);
}
}
}


return result;



// process /:id/ cases
// boolean urlParalized = false;
// String patternOrignial = pattern;
Expand All @@ -306,6 +333,7 @@ public List<Action> getMatchedActionList(int projectId, String pattern) {
// pattern = pattern.substring(0, pattern.indexOf(":"));
// }

/**
StringBuilder sb = new StringBuilder();
sb.append("SELECT a.id FROM tb_action a ")
.append("JOIN tb_action_and_page ap ON ap.action_id = a.id ")
Expand All @@ -322,6 +350,7 @@ public List<Action> getMatchedActionList(int projectId, String pattern) {
for (int id : list) {
actionList.add(getAction(id));
}
*/

// URL parameters filter
/**
Expand All @@ -343,7 +372,7 @@ public List<Action> getMatchedActionList(int projectId, String pattern) {
actionList = filteredActionList;
}
*/
return actionList;
// return actionList;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -415,4 +444,29 @@ public List<Project> search(String key) {
query.setString("key", "%" + key + "%");
return query.list();
}

@SuppressWarnings({ "rawtypes" })
private List<Action> getActionListOfProject(int projectId) {
List<Action> list = new ArrayList<Action>();
StringBuilder sql = new StringBuilder();
sql.append("SELECT a.id ");
sql.append("FROM tb_project p ");
sql.append("JOIN tb_module m ON m.project_id = p.id ");
sql.append("JOIN tb_page ON tb_page.module_id = m.id ");
sql.append("JOIN tb_action_and_page anp ON anp.page_id = tb_page.id ");
sql.append("JOIN tb_action a ON a.id = anp.action_id ");
sql.append("WHERE p.id = :projectId ");
Query query = getSession().createSQLQuery(sql.toString());
query.setInteger("projectId", projectId);

List result = query.list();
List<Integer> ids = new ArrayList<Integer>();
for (Object r : result) {
ids.add((Integer)r);
}
for (Integer id : ids) {
list.add(this.getAction(id));
}
return list;
}
}

0 comments on commit 0c7711f

Please sign in to comment.