Skip to content

Commit

Permalink
Merge pull request #357 from xiang2026/mingmingz
Browse files Browse the repository at this point in the history
fix URI has "null" substring bug and remove excrescent path in appid
  • Loading branch information
zxy0728 authored Aug 3, 2018
2 parents a929845 + 71a800c commit 7a4024c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ else if (rc == -1) {

if (clientURL.startsWith("http")) {
String rs = (String) context.get(ProfileConstants.PC_ARG_CLIENT_RS);
if(StringHelper.isNaturalNumber(rs)) {
if (StringHelper.isNaturalNumber(rs)) {
urlAttrs.put(MonitorServerUtil.getActionTag(rs), curTime);
}
}
Expand All @@ -147,8 +147,29 @@ else if (rc == -1) {
*/
private ProfileElementInstance getTargetURIInst(URI clientTargetURI, ProfileElement elem) {

String uri = clientTargetURI.getScheme() + "://";
String host = clientTargetURI.getHost();
int port = clientTargetURI.getPort();

// According the analysis function in Java.Net.URI, char '_' is treated as Illegal character in host name.
// It makes a URISyntaxException when parse URI string.
if (host == null){
String authority = clientTargetURI.getAuthority();
// if the authority part contains user info
int index = authority.indexOf("@");
if (index >= 0){
uri += authority.substring(index + 1);
}else{
uri += authority;
}
}
else{
uri += host;
int port = clientTargetURI.getPort();
if (port > 0){
uri += ":" + port;
}
}

// String dnsName = null;
//
// if (!NetworkHelper.isIPV4(host)) {
Expand All @@ -158,12 +179,6 @@ private ProfileElementInstance getTargetURIInst(URI clientTargetURI, ProfileElem
// host = tmp;
// }
// }

String uri = clientTargetURI.getScheme() + "://" + host;
if (port > 0) {
uri += ":" + port;
}

// http://xxxxx/yyyyy@client type
ProfileElementInstance pei = elem.getInstance(uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,28 +624,23 @@ private static String getDefaultApplicationId(String contextroot, String basePat
if ("".equals(contextroot)) {
/*
* NOTE: springboot's basePath is a random temp directory,so we use main(usually the jar name) as the appid
*
*/
if (UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR)
.equals(UAVServer.ServerVendor.SPRINGBOOT)) {
String javaCommand = System.getProperty("sun.java.command");
appid = javaCommand.split(" ")[0];
}else {
appid = basePath;
}
else {
String tmp = basePath.replace("\\", "/");
int index = tmp.lastIndexOf("/");

/**
* "/app/xxxxx/" remove the last "/" to get the appid
*/
if (index == tmp.length() - 1) {
tmp = tmp.substring(0, tmp.length() - 1);
index = tmp.lastIndexOf("/");
}
appid = tmp.substring(index + 1);
}
appid = getReducedPath(appid);
}
else {
appid = contextroot;
if (UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR)
.equals(UAVServer.ServerVendor.SPRINGBOOT)) {
appid = getReducedPath(appid);
}
}

// 去除最开始的"/"
Expand All @@ -661,4 +656,21 @@ private static String getDefaultApplicationId(String contextroot, String basePat

return appid;
}

/*
* 获取给定路径的最后以层目录
例如:给定目录 abc/def/
输出:def
*/
private static String getReducedPath(String path) {
if (path == null || path.equals(""))
return "";
String reducedPath = path.replace("\\", "/");
int index = reducedPath.lastIndexOf("/");
if (index == reducedPath.length() - 1) {
reducedPath = reducedPath.substring(0, reducedPath.length() - 1);
index = reducedPath.lastIndexOf("/");
}
return reducedPath.substring(index + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void startServer(String port, String contextPath, String appName, Object
// start Monitor Server when server starts
UAVServer.instance().start(new Object[] { UAVServer.ServerVendor.SPRINGBOOT });
// set appid
setAppid(contextPath);
//setAppid(contextPath);
// set the connector port
UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT,
DataConvertHelper.toInt(port, 8080));
Expand All @@ -73,10 +73,19 @@ public void startServer(String port, String contextPath, String appName, Object
*
* @param contextPath
*/
@Deprecated
public void setAppid(String contextPath) {

if (contextPath == null || "/".equals(contextPath)) {
contextPath = "";
}else{
contextPath = contextPath.replace("\\", "/");
int index = contextPath.lastIndexOf("/");
if (index == contextPath.length() - 1){
contextPath = contextPath.substring(0, contextPath.length() - 1);
index = contextPath.lastIndexOf("/");
}
contextPath = contextPath.substring(index + 1);
}

System.setProperty("com.creditease.uav.appid", MonitorServerUtil.getApplicationId(contextPath, ""));
Expand Down

0 comments on commit 7a4024c

Please sign in to comment.