Jenkins使用对象作为参数进行表单验证

时间:2015-09-30 07:03:06

标签: jenkins

我正在为Jenkins构建一个插件,我正在尝试验证我的表单(连接测试方法)。当所有@QueryParameter都是字符串时,这很好。

现在我正在尝试将表单验证方法发送为这样的对象:

        public FormValidation doTestConnection(
                @QueryParameter("url") final String url,
                @QueryParameter("timeout") final String timeout,
                @QueryParameter("bypassProxy") final boolean bypassProxy,
                @QueryParameter("deployerCredentialsConfig") final CredentialsConfig deployerCredentialsConfig,
                @QueryParameter("resolverCredentialsConfig") final CredentialsConfig resolverCredentialsConfig
        ) throws ServletException {

在我的global.jelly文件中,我有这个:

<f:validateButton
        title="${%Test Connection}" progress="${%Testing...}"
                        method="testConnection"
                        with="url,timeout,bypassProxy,deployerCredentialsConfig,resolverCredentialsConfig"/>

我的CredentialConfig类实现了Serializable,但我想这还不够,因为我在点击“测试连接”按钮时得到了这个:

java.lang.IllegalArgumentException: Failed to invoke public hudson.util.FormValidation 

org.jfrog.hudson.MyBuilder $ DescriptorImpl.doTestConnection(java.lang.String,java.lang.String,boolean,org.jfrog.hudson.CredentialsConfig,org.jfrog.hudson.CredentialsConfig)抛出javax.servlet。的ServletException

Jenkins没有关于在FormValidation调用中使用对象的好文档。

2 个答案:

答案 0 :(得分:0)

查看Jenkins文档和&lt; f:validateButton /&gt;背后的代码,我认为不可能让对象在验证逻辑中绑定。

文档说(https://wiki.jenkins-ci.org/display/JENKINS/Jelly+form+controls):

  

&#39;&#39; attribute指定发送到服务器的输入字段   验证。它们与字段属性或   其他输入控件的name属性。最近的值   上面的输入字段被发送到服务器,所以   这意味着按钮必须在输入字段之后。多   可以使用&#39;,&#39;。

指定字段

代码只是按名称获取字段 -​​ 没有&#34;对象汇编&#34; (我相信它只在实际配置提交期间完成)。

https://github.com/jenkinsci/jenkins/blob/96ec7a267e0efba2ec99590c871db0940e35920f/war/src/main/webapp/scripts/hudson-behavior.js#L2856

答案 1 :(得分:0)

我遇到了类似的问题。查看代码,似乎装订器会尝试将参数转换为doCheck函数声明中提供的类型。

    class HandlerImpl extends AnnotationHandler<QueryParameter> {
    public Object parse(StaplerRequest request, QueryParameter a, Class type, String parameterName) throws ServletException {
        String name = a.value();
        if(name.length()==0)    name=parameterName;
        if(name==null)
            throw new IllegalArgumentException("Parameter name unavailable neither in the code nor in annotation");

        String value = request.getParameter(name);
        if(a.required() && value==null)
            throw new ServletException("Required Query parameter "+name+" is missing");
        if(a.fixEmpty() && value!=null && value.length()==0)
            value = null;
        return convert(type,value); // <--- HERE
    }
}

作为一种解决方法,我将类型更改为布尔值,如下所示:

    public FormValidation doTestConnection(
            @QueryParameter("url") final String url,
            @QueryParameter("timeout") final String timeout,
            @QueryParameter("bypassProxy") final boolean bypassProxy,
            @QueryParameter("deployerCredentialsConfig") final boolean deployerCredentialsConfig,
            @QueryParameter("resolverCredentialsConfig") final boolean resolverCredentialsConfig
    ) throws ServletException {

这使我至少可以检查是否设置了变量。但是,对于您的用例而言,可能还不够。