如何在Atlassian Confluence中获取用户宏中可用的Velocity对象和API的列表? [关闭]

问题描述 投票:1回答:1

我需要在Atlassian Confluence中编写一些用户宏。它使用Apache Velocity模板引擎。如何在该上下文中找到可用的API?

例如,一个community-provided macro使用像$spaceManager这样的对象。如何枚举所有可用的对象,并获取其方法的文档?

列出对象的page on the Atlassian site是不完整的:它不仅仅列出了可用的真实对象的一小部分,它也不是特定于用户宏上下文,甚至不是特定版本的Confluence。 (例如,用户宏被赋予与其他插件中使用的Velocity不同的Velocity上下文,并且具有不同的可用对象,Confluence 5.1中可用的对象与Confluence 5.6中可用的对象不同。)

Atlassian Answers网站上有similar questions,但没有一点指向完整的API和类型引用。

velocity confluence
1个回答
8
投票

official list of Velocity objects visible in Confluence列在Atlassian的网站上,但您可能已经注意到,它远未完成。

使用JIRA,there is a trick可以用来列出当前Velocity上下文中的所有对象:

#foreach($p in $ctx.keySet().toArray())                                
        $p.toString() - $ctx.get($p).getClass().getName().toString() 
#end

不幸的是,上面的JIRA技巧对Confluence不起作用,因为Confluence不公开ctx地图。

幸运的是,你提到你正在编写用户宏,所以不是所有的都丢失了!在默认上下文中使用access objects that are not normally available的另一个技巧,你可以通过一个洞进入为用户宏提供Velocity上下文的类,并给自己一个副本。结合这两种方法,我们得到以下Confluence:

#set($macroUtilClass=$action.class.forName('com.atlassian.confluence.renderer.radeox.macros.MacroUtils'))
#set($getContextMethod=$macroUtilClass.getDeclaredMethod('defaultVelocityContext',null))
#set($ctx=$getContextMethod.invoke(null))

#foreach($p in $ctx.keySet().toArray())
$p.toString() - $ctx.get($p).getClass().getName().toString()<br/>
#end

这将为您提供所有可用对象及其相应类型的列表,如下所示:

res - com.atlassian.confluence.web.filter.DebugFilter$LoggingResponseWrapper
bootstrap - com.atlassian.confluence.setup.DefaultBootstrapManager
settingsManager - com.atlassian.confluence.setup.settings.DefaultSettingsManager
userAccessor - com.sun.proxy.$Proxy54
seraph - com.atlassian.confluence.util.SeraphUtils
xsrfTokenGenerator - com.atlassian.xwork.SimpleXsrfTokenGenerator
...

要查找通过每个对象可用的方法,您需要按类名查找相应的JavaDoc。我发现Google通常是最快的完成路径,但您也可以直接转到JavaDocs(例如,SpaceManager),然后在其他软件包中查找或调整URL以匹配您想要的类。您还需要调整URL中的“最新”以对应您已安装的Confluence的实际版本。最后,一些类隐藏在代理之后(如上面的userAccessor),但变量名通常与类名匹配,因此这些通常很容易理解。

请注意,此示例专门针对用户宏而定制。其他Velocity上下文中可用的对象肯定会有所不同。

© www.soinside.com 2019 - 2024. All rights reserved.