Browse Source

url别名工具类

pull/1/head
richie 5 years ago
parent
commit
21f405bc1b
  1. 96
      src/main/java/com/fanruan/api/net/URLAliasKit.java
  2. 75
      src/test/java/com/fanruan/api/net/URLAliasKitTest.java

96
src/main/java/com/fanruan/api/net/URLAliasKit.java

@ -0,0 +1,96 @@
package com.fanruan.api.net;
import com.fr.decision.webservice.url.alias.URLAlias;
import com.fr.decision.webservice.url.alias.URLAliasFactory;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-19
* 路由工具类
* 宽泛匹配/foo /aaa/bbb/foo 是匹配的
*/
public class URLAliasKit {
/**
* 创建路由的别名
*
* @param aliasPath 别名
* @param targetURL 原始路由
* @return 别名对象
*/
public static URLAlias createRawAlias(String aliasPath, String targetURL) {
return URLAliasFactory.createRawAlias(aliasPath, targetURL);
}
/**
* 创建路由的别名
*
* @param aliasPath 别名
* @param targetURL 原始路由
* @param wideRange 是否宽泛匹配
* @return 别名对象
*/
public static URLAlias createRawAlias(String aliasPath, String targetURL, boolean wideRange) {
return URLAliasFactory.createRawAlias(aliasPath, targetURL, wideRange);
}
/**
* 创建决策平台的路由别名
*
* @param aliasPath 别名
* @param decisionTargetURL 原始路由地址
* @return 加了决策平台servlet名字前缀的别名对象
*/
public static URLAlias createDecisionAlias(String aliasPath, String decisionTargetURL) {
return URLAliasFactory.createDecisionAlias(aliasPath, decisionTargetURL);
}
/**
* 创建决策平台的路由别名
*
* @param aliasPath 别名
* @param decisionTargetURL 原始路由地址
* @param wideRange 是否宽泛匹配
* @return 加了决策平台servlet名字前缀的别名对象
*/
public static URLAlias createDecisionAlias(String aliasPath, String decisionTargetURL, boolean wideRange) {
return URLAliasFactory.createDecisionAlias(aliasPath, decisionTargetURL, wideRange);
}
/**
* 创建插件的路由别名
*
* @param aliasPath 别名
* @param pluginPath 插件请求地址
* @return 别名对象
*/
public static URLAlias createPluginAlias(String aliasPath, String pluginPath) {
return URLAliasFactory.createPluginAlias(aliasPath, pluginPath);
}
/**
* 创建插件的路由别名
*
* @param aliasPath 别名
* @param pluginPath 插件请求地址
* @param isPublic 是否为公开可访问的地址
* @return 别名对象
*/
public static URLAlias createPluginAlias(String aliasPath, String pluginPath, boolean isPublic) {
return URLAliasFactory.createPluginAlias(aliasPath, pluginPath, isPublic);
}
/**
* 创建插件的路由别名
*
* @param aliasPath 别名
* @param pluginPath 插件请求地址
* @param isPublic 是否为公开可访问的地址
* @param wideRange 是否宽泛匹配
* @return 别名对象
*/
public static URLAlias createPluginAlias(String aliasPath, String pluginPath, boolean isPublic, boolean wideRange) {
return URLAliasFactory.createPluginAlias(aliasPath, pluginPath, isPublic, wideRange);
}
}

75
src/test/java/com/fanruan/api/net/URLAliasKitTest.java

@ -0,0 +1,75 @@
package com.fanruan.api.net;
import com.fanruan.api.Prepare;
import com.fr.decision.webservice.url.alias.URLAlias;
import com.fr.decision.webservice.url.alias.impl.PluginURLAlias;
import org.junit.Assert;
import org.junit.Test;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-19
*/
public class URLAliasKitTest extends Prepare {
@Test
public void raw() {
URLAlias alias = URLAliasKit.createRawAlias("/foo", "/aaa/bbb/foo");
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/aaa/bbb/foo");
Assert.assertFalse(alias.isWideRange());
}
@Test
public void raw2() {
URLAlias alias = URLAliasKit.createRawAlias("/foo", "/aaa/bbb/foo", true);
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/aaa/bbb/foo");
Assert.assertTrue(alias.isWideRange());
}
@Test
public void decision() {
URLAlias alias = URLAliasKit.createDecisionAlias("/foo", "/aaa/bbb/foo");
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/decision/aaa/bbb/foo");
Assert.assertFalse(alias.isWideRange());
}
@Test
public void decision2() {
URLAlias alias = URLAliasKit.createDecisionAlias("/foo", "/aaa/bbb/foo", true);
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/decision/aaa/bbb/foo");
Assert.assertTrue(alias.isWideRange());
}
@Test
public void plugin() {
URLAlias alias = URLAliasKit.createPluginAlias("/foo", "/aaa/bbb/foo");
((PluginURLAlias)alias).setPluginId("abcd");
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/decision/plugin/private/abcd/aaa/bbb/foo");
Assert.assertFalse(alias.isWideRange());
}
@Test
public void plugin2() {
URLAlias alias = URLAliasKit.createPluginAlias("/foo", "/aaa/bbb/foo", true);
((PluginURLAlias)alias).setPluginId("abcd");
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/decision/plugin/public/abcd/aaa/bbb/foo");
Assert.assertFalse(alias.isWideRange());
}
@Test
public void plugin3() {
URLAlias alias = URLAliasKit.createPluginAlias("/foo", "/aaa/bbb/foo", true, true);
((PluginURLAlias)alias).setPluginId("abcd");
Assert.assertEquals(alias.getShortPath(), "/foo");
Assert.assertEquals(alias.getTargetURL(), "/decision/plugin/public/abcd/aaa/bbb/foo");
Assert.assertTrue(alias.isWideRange());
}
}
Loading…
Cancel
Save