【问题标题】:Processing of HTTP status codesHTTP状态码的处理
【发布时间】:2022-06-21 00:16:13
【问题描述】:

我正在 Orbeon 2021.1.2 PE 中创建表单,但在处理错误调用时遇到问题。我正在通过表单生成器创建 HTTP 服务和操作。我希望能够解决它并能够留在Builder。 我称 API 女巫的工作方式与 Twitter 类似,因此它也会为业务错误返回错误 HTTP 状态代码(参见文档 https://developer.twitter.com/ja/docs/basics/response-codes 和 https://developer.twitter.com/en/docs/twitter-ads-api/response-codes)。 举个最简单的例子:如果我尝试在数据库中查找某个东西,如果它不在数据库中,我会得到代码 404,响应正文中有详细的错误。

我需要获取表单的状态代码 + 完整正文(标题也可以)。我不想使用模态窗口(Orbeon 中默认为 witch)来处理此调用。它打扰用户 + 通话经常会改变整个工作流程(可见的内容等)。有些调用甚至是异步的。

Orbeon 中这个问题的最佳解决方案是什么?我尝试了一些东西,并在几个地方找到了阻塞。

1] 通过属性oxf.fr.detail.process.action-service-error.*.*解决方案

这里是设置默认错误模式窗口。我删了,发现可以得到一些值,写在表格里,例如:

xf:setvalue(ref="//control-1", value="event('response-status-code')")

具体来说,这些是值:

event('error-type')
event('response-status-code')
event('resource-uri')
event('response-headers') - get specific via event('response-headers')[lower-case(name) = 'content-lenght']/value 
event('response-body') - This not working, it is still supported?

因为它是一个全局配置,所以我开始研究将这些值放在哪里。我尝试创建自己的实例,但最终得到了fr:insert() 函数。

我想为每个提交创建一个新实例,然后输入单独的值(event ('response-status-code'),...)。但是,fr:insert() 的进程不起作用,文档的示例很差 (https://doc.orbeon.com/form-runner/advanced/buttons-and-processes/actions-xforms#xf-insert)

这做我想要的,但在形式上:

<xf:insert context="xxf:instance('HTTP-ERROR-RESULT')" ref="responses" origin="xxf:instance('HTTP-ERROR-TEMPLATE')"/>

我试图将其转换为进程,但没有运气:

xf:insert(into="xxf:instance('HTTP-ERROR-RESULT')/reponses", origin="xxf:instance('HTTP-ERROR-TEMPLATE')")

如果这是正确的方法?如何修复xf:insert() 中的语义错误以及如何获取响应正文(event ('response-body') 不起作用...)?并且是某种获取提交名称或正在进行的操作的方法(我需要一些 id 来查找实例)。

2] 我尝试的另一种方法是通过xforms-submit-error 提交,但它也不起作用。 在构建器创建的结构下:

        <xf:action event="xforms-submit-done" ev:observer="echo-submission">
            <xf:action class="fr-set-control-value-action">
                <xf:var name="control-name" value="'control-2'"/>
                <xf:var name="control-value" value="/*"/>
            </xf:action>
        </xf:action>

我尝试插入类似的东西,但使用xforms-submit-error:

        <xf:action event="xforms-submit-error" ev:observer="echo-submission">
            <xf:action class="fr-set-control-value-action">
                <xf:var name="control-name" value="'control-2'"/>
                <xf:var name="control-value" value="/*"/>
            </xf:action>
        </xf:action>

我发现在提交时使用class="fr-service",我无法捕捉到xforms-submit-error。那么在构建器之外自己编写整个提交并使用xforms-submit-error 的正确解决方案是什么?

Orbeon 中是否有另一个优雅的解决方案?感谢您的回复!

【问题讨论】:

    标签: orbeon


    【解决方案1】:

    编写自己的 XForms 将为您提供最大的灵活性。您可以使用 Form Builder 中的 Edit Source 将 XForms 直接放在表单定义中,也可以放在自定义模型中,这是服务器磁盘上的一个文件,我建议您这样做,请参阅doc。

    您似乎已经找到了所有部分(您在那里做了一些很好的研究!),但这里有一个摘要。如果你有一个服务my-service,你可以监听xforms-submit-error,并将事件属性的值存储在你自己的实例中。如果使用自定义模型,该文件将具有以下内容,您也可以通过 Form Builder 中的 Edit Source 将下面的&lt;xf:model&gt; 的内容直接放在表单定义的&lt;xf:model&gt; 中。这里的逻辑只是将状态码保存在实例my-error-instance 中,然后使用&lt;xf:message&gt; 显示是否进行调试。

    <xf:model xmlns:xf="http://www.w3.org/2002/xforms">
    
        <xf:instance id="my-error-instance">
            <_>
                <status-code/>
            </_>
        </xf:instance>
    
        <xf:action observer="my-service-submission" event="xforms-submit-error">
            <xf:setvalue
                ref="instance('my-error-instance')/status-code"
                value="event('response-status-code')"/>
            <xf:message value="instance('my-error-instance')/status-code"/>
        </xf:action>
    
    </xf:model>
    

    您还需要通过设置以下属性来禁用默认的错误对话框。

    <property as="xs:string" name="oxf.fr.detail.process.action-service-error.*.*"/>
    

    最后,我在下面放了一个表单定义的完整源代码,您可以用它来测试它,使用my-service HTTP 服务,向http://httpbin.org/status/404 发出请求,并调用我用来测试的表单加载以上自定义模型逻辑。

    <xh:html xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:ev="http://www.w3.org/2001/xml-events"
             xmlns:xi="http://www.w3.org/2001/XInclude"
             xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
             xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
             xmlns:map="http://www.w3.org/2005/xpath-functions/map"
             xmlns:array="http://www.w3.org/2005/xpath-functions/array"
             xmlns:math="http://www.w3.org/2005/xpath-functions/math"
             xmlns:exf="http://www.exforms.org/exf/1-0"
             xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
             xmlns:saxon="http://saxon.sf.net/"
             xmlns:sql="http://orbeon.org/oxf/xml/sql"
             xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
             xmlns:fb="http://orbeon.org/oxf/xml/form-builder">
        <xh:head>
            <xh:title>Calling service returning a 404 on form load</xh:title>
            <xf:model id="fr-form-model" xxf:expose-xpath-types="true" xxf:analysis.calculate="true">
    
                <!-- Main instance -->
                <xf:instance id="fr-form-instance" xxf:exclude-result-prefixes="#all" xxf:index="id">
                    <form>
                        <section-1>
                            <grid-1>
                                <control-1/>
                            </grid-1>
                            
                        </section-1>
                    </form>
                </xf:instance>
    
                <!-- Bindings -->
                <xf:bind id="fr-form-binds" ref="instance('fr-form-instance')">
                    <xf:bind id="section-1-bind" name="section-1" ref="section-1">
                        <xf:bind id="grid-1-bind" ref="grid-1" name="grid-1">
                            <xf:bind id="control-1-bind" name="control-1" ref="control-1" xxf:whitespace="trim"/>
                        </xf:bind>
                        
                    </xf:bind>
                </xf:bind>
    
                <!-- Metadata -->
                <xf:instance id="fr-form-metadata" xxf:readonly="true" xxf:exclude-result-prefixes="#all">
                    <metadata>
                        <application-name>a</application-name>
                        <form-name>a</form-name>
                        <title xml:lang="en">Calling service returning a 404 on form load</title>
                        <description xml:lang="en"/>
                        <created-with-version>2021.1-SNAPSHOT PE</created-with-version>
                        <library-versions>
                            <app>4</app>
                        </library-versions>
                    </metadata>
                </xf:instance>
    
                <!-- Attachments -->
                <xf:instance id="fr-form-attachments" xxf:exclude-result-prefixes="#all">
                    <attachments/>
                </xf:instance>
    
                <!-- All form resources -->
                <xf:instance xxf:readonly="true" id="fr-form-resources" xxf:exclude-result-prefixes="#all">
                    <resources>
                        <resource xml:lang="en">
                            <section-1>
                                <label>Untitled Section</label>
                            </section-1>
                            <control-1>
                                <label/>
                                <hint/>
                                <alert/>
                            </control-1>
                        </resource>
                    </resources>
                </xf:instance>
    
                <xf:instance id="my-service-instance" class="fr-service" xxf:exclude-result-prefixes="#all">
                            <body xmlns:xxbl="http://orbeon.org/oxf/xml/xbl"
                          xmlns:fbf="java:org.orbeon.oxf.fb.FormBuilderXPathApi">&lt;params/&gt;</body>
                        </xf:instance>
                <xf:submission id="my-service-submission" class="fr-service"
                               resource="http://httpbin.org/status/404"
                               method="get"
                               serialization="none"
                               mediatype=""/>
                <xf:action id="my-action-binding">
                    <xf:action event="fr-run-form-load-action-after-controls" ev:observer="fr-form-model"
                               if="true()">
                        <xf:send submission="my-service-submission"/>
                    </xf:action>
                    <xf:action event="xforms-submit" ev:observer="my-service-submission">
                        <xf:var name="request-instance-name" value="'my-service-instance'"/>
                        <xf:action/>
                    </xf:action>
                    <xf:action event="xforms-submit-done" ev:observer="my-service-submission"/>
                </xf:action>
            </xf:model>
        </xh:head>
        <xh:body>
            <fr:view>
                <fr:body xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xbl="http://www.w3.org/ns/xbl"
                         xmlns:oxf="http://www.orbeon.com/oxf/processors">
                    <fr:section id="section-1-section" bind="section-1-bind">
                        <xf:label ref="$form-resources/section-1/label"/>
                        <fr:grid id="grid-1-grid" bind="grid-1-bind">
                            <fr:c y="1" x="1" w="6">
                                    <xf:input id="control-1-control" bind="control-1-bind">
                                        <xf:label ref="$form-resources/control-1/label"/>
                                        <xf:hint ref="$form-resources/control-1/hint"/>
                                        <xf:alert ref="$fr-resources/detail/labels/alert"/>
                                    </xf:input>
                                </fr:c>
                            <fr:c y="1" x="7" w="6"/>
                        </fr:grid>
                    </fr:section>
                </fr:body>
            </fr:view>
        </xh:body>
    </xh:html>
    

    【讨论】:

    • 非常感谢!所有这一切我工作得很好。现在,当我尝试使其更通用时,我正在苦苦挣扎。我想制作一个自定义模型来处理所有提交。我在我的自定义模型中执行操作:&lt;xf:action observer="my-main-model" event="xforms-submit-error"&gt; &lt;xf:setvalue ref="xxf:instance('my-error-instance')//submission[1]/name" value=???/&gt; &lt;xf:setvalue ref="xxf:instance('my-error-instance')//submission[1]/status-code" value="event('response-status-code')"/&gt; &lt;/xf:action&gt; 但不知道如何获取提交的名称或类似的名称到这个通用事件。有可能吗?
    • 完美@DavidDejmal,我很高兴这有帮助。开箱即用,主模型名为fr-form-model,不建议改成my-main-model。所以你的action应该有observer="fr-form-model",然后它可以使用event('target')来获取导致错误的提交的id。你会告诉我这是否适合你! -亚历克斯
    • 是的,event('target') 工作正常!非常感谢。现在我拥有了我需要的一切。
    【解决方案2】:

    根据@avernet 的回答,我为xforms-submit-error 制作了通用处理程序。首先,如上所述,我通过设置此属性来禁用默认对话框。

    <property as="xs:string" name="oxf.fr.detail.process.action-service-error.*.*"/>
    

    然后我把我自己的自定义模型放到WEB-INF/resources/forms/resources。整体是这样的:

    <xf:model  xmlns:xf="http://www.w3.org/2002/xforms" id="my-model">
        <xf:instance id="my-error-instance">
            <reponseList>
            </reponseList>
        </xf:instance>
    
        <xf:instance id="my-error-template-instance">
            <submission name="">
                <status-code/>
                <uri/>
                <date/>
            </submission>
        </xf:instance>
    
        <xf:action observer="fr-form-model" event="xforms-submit-error">
            <xf:insert if="not(exists(xxf:instance('my-error-instance')//submission[@name=event('target')]))" context="xxf:instance('my-error-instance')" ref="reponseList" origin="xxf:instance('my-error-template-instance')"/>
            <xf:setvalue if="not(exists(xxf:instance('my-error-instance')//submission[@name=event('target')]))" ref="xxf:instance('my-error-instance')//submission[1]/@name" value="event('target')"/>
            <xf:setvalue ref="xxf:instance('my-error-instance')//submission[@name=event('target')]/status-code" value="event('response-status-code')"/>
            <xf:setvalue ref="xxf:instance('my-error-instance')//submission[@name=event('target')]/uri" value="event('resource-uri')"/>
            <xf:setvalue ref="instance('my-error-instance')//submission[@name=event('target')]/date" value="event('response-headers')[lower-case(name) = 'date']/value"/>
        </xf:action>
    </xf:model>
    

    注意两个条件(插入和第一个设置值)。因为他们模型只保存一个提交的最后一个错误。如果您出于某种原因需要历史记录,可以将其删除。

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2019-09-18
      • 1970-01-01
      • 2018-02-16
      • 2019-08-26
      • 2012-09-25
      • 2015-08-24
      • 2011-09-01
      • 2020-04-30
      相关资源
      最近更新 更多