From 21f405bc1b3539b242861cea87f72b65237cc09a Mon Sep 17 00:00:00 2001 From: richie Date: Mon, 19 Aug 2019 17:37:36 +0800 Subject: [PATCH] =?UTF-8?q?url=E5=88=AB=E5=90=8D=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fanruan/api/net/URLAliasKit.java | 96 +++++++++++++++++++ .../com/fanruan/api/net/URLAliasKitTest.java | 75 +++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/main/java/com/fanruan/api/net/URLAliasKit.java create mode 100644 src/test/java/com/fanruan/api/net/URLAliasKitTest.java diff --git a/src/main/java/com/fanruan/api/net/URLAliasKit.java b/src/main/java/com/fanruan/api/net/URLAliasKit.java new file mode 100644 index 0000000..eb31b8f --- /dev/null +++ b/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); + } +} diff --git a/src/test/java/com/fanruan/api/net/URLAliasKitTest.java b/src/test/java/com/fanruan/api/net/URLAliasKitTest.java new file mode 100644 index 0000000..7867bda --- /dev/null +++ b/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()); + } + +} \ No newline at end of file