4

我的客户群终于离开了 Coldfusion 8,所以现在我可以利用 Coldfusion 9 的Application.cfc -> onCFCRequest活动了。我有一个测试场景设置,我的结果不是我所期望的。我有一个方法,我调用它会产生一个XML像这样的有效响应......

Response Header: Content-Type:application/xml;charset=UTF-8
Response:
<?xml version="1.0" encoding="UTF-8"?>
<rows><row id="10000282742505"><cell/><cell> ...

现在,在我介绍了这个onCFCRequest事件之后,我得到了这个(这打破了我的网格)......

Response Header: Content-Type:application/xml;charset=UTF-8
Response:
<wddxPacket version='1.0'><header/><data><string>&lt;rows&gt;&lt;row id="10000282742505"&gt;&lt;cell&gt;&lt;/cell&gt;&lt;cell&gt; ...

这里是活动...

<cffunction name="onCFCRequest" access="public" returntype="Any" output="true">
    <cfargument type="string" name="cfc" required="true">
    <cfargument type="string" name="method" required="true">
    <cfargument type="struct" name="args" required="true">

    <cfscript>
        // OnCFCRequest security hole fix as detailed here: http://blog.adamcameron.me/2013/04/its-easy-to-create-security-hole-in.html
        var o = createObject(ARGUMENTS.cfc);
        var metadata = getMetadata(o[ARGUMENTS.method]);

        if (structKeyExists(metadata, "access") && metadata.access == "remote"){
            return invoke(o, ARGUMENTS.method, ARGUMENTS.args);
        }else{
            throw(type="InvalidMethodException", message="Invalid method called", detail="The method #method# does not exists or is inaccessible remotely");
        }
    </cfscript>
    <cfreturn />
</cffunction>

如何让 onCFCRequest 以与远程函数返回的相同格式传递响应?

我知道这篇文章:http ://www.bennadel.com/blog/1647-Learning-ColdFusion-9-Application-cfc-OnCFCRequest-Event-Handler-For-CFC-Requests.htm

我可能最终会尝试这样做,但首先我想弄清楚为什么我不能简单地通过相同格式的响应。

4

2 回答 2

1

看来我不能简单地通过相同格式的结果,因为这个......”默认情况下,ColdFusion 将除 XML 之外的所有返回类型(包括简单返回类型)序列化为 WDDX 格式,并将 XML 数据作为 XML 返回文本。 ”(http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f5c.html)。被调用的远程函数将 myxml作为字符串返回给onCFCRequestonCFCRequest然后将该简单返回类型(此时为字符串)转换为WDDX,因为这是默认行为。

所以...

经过大量测试后,我最终采用了 Ben Nadel 文章中的解决方案,但我想提一下一些调整。

  1. 第一个添加是添加我的问题中已经显示的代码,以解决 Adam Cameron 发现的错误。
  2. 第二个添加是在调用之后立即执行此操作:<cfset local.result = ToString(local.result)>。在 Ben 的评论中,它说“ ......所有返回的值都将是字符串...... ”但情况似乎并非如此。我们有一些只返回数字的远程函数。如果没有ToString()将响应转换为二进制的代码,则会失败。
  3. 在设置 mimeTypes 的部分中,我将 IF 语句更改为json. 在我们编写的每个远程函数中,我们创建一个 ColdFusion 结构,然后像这样返回它<cfreturn SerializeJSON(z.response) />:这似乎比手动拼凑一个 json 字符串,然后在onCFCRequest. 因此,在 json mimeType 的 onCFCRequest 中,我只是将其视为字符串,因为它已经序列化了;所以不需要第二次序列化它。
  4. 同样在 mimeType 部分,我为xml. 我们有许多远程函数可以输出xml网格,而不是wddx. 而且由于没有returnFormatofxml我在支票上方添加了returnType支票。xmlwddx
  5. 根据@Henry的评论将 responseMimeType 更改为和JSON更改。谢谢!XMLapplication/jsonapplication/xml

非常感谢 Ben 和 Adam 奠定了基础!

这是最终的结果...

<cffunction name="onCFCRequest" access="public" returntype="void" output="true" hint="I process the user's CFC request.">
    <cfargument name="component" type="string" required="true" hint="I am the component requested by the user." />
    <cfargument name="methodName" type="string" required="true" hint="I am the method requested by the user." />
    <cfargument name="methodArguments" type="struct" required="true" hint="I am the argument collection sent by the user." />

    <!---
    Here we can setup any request level variables we want
    and they will be accessible to all remote cfc calls.
    --->
    <cfset request.jspath  = 'javascript'>
    <cfset request.imgpath = 'images'>
    <cfset request.csspath = 'css'>

    <!---
    Check to see if the target CFC exists in our cache.
    If it doesn't then, create it and cached it.
    --->
    <cfif !structKeyExists( application.apiCache, arguments.component )>

        <!---
        Create the CFC and cache it via its path in the
        application cache. This way, it will exist for
        the life of the application.
        --->
        <cfset application.apiCache[ arguments.component ] = createObject( "component", arguments.component ) />
    </cfif>

    <!---
    ASSERT: At this point, we know that the target
    component has been created and cached in the
    application.
    --->

    <!--- Get the target component out of the cache. --->
    <cfset local.cfc = application.apiCache[ arguments.component ] />

    <!--- Get the cfcs metaData --->
    <cfset var metadata = getMetaData( local.cfc[ arguments.methodName ] )>

    <!--- OnCFCRequest security hole fix as detailed here: http://cfmlblog.adamcameron.me/2013/04/its-easy-to-create-security-hole-in.html --->
    <cfif structKeyExists(metadata, "access") and metadata.access eq "remote">
        <!--- Good to go! --->
    <cfelse>
        <cfthrow type="InvalidMethodException" message="Invalid method called" detail="The method #arguments.methodName# does not exists or is inaccessible remotely">
    </cfif>

    <!---
    Execute the remote method call and store the response
    (note that if the response is void, it will destroy
    the return variable).
    --->
    <cfinvoke returnvariable="local.result" component="#local.cfc#" method="#arguments.methodName#" argumentcollection="#arguments.methodArguments#" />

    <!---
    We have some functions that return only a number (ex: lpitems.cfc->get_lpno_onhandqty).
    For those we must convert the number to a string, otherwise, when we try to
    convert the response to binary down at the bottom of this function it will bomb.
    --->
    <cfset local.result = ToString(local.result)>

    <!---
    Create a default response data variable and mime-type.
    While all the values returned will be string, the
    string might represent different data structures.
    --->
    <cfset local.responseData = "" />
    <cfset local.responseMimeType = "text/plain" />

    <!---
    Check to see if the method call above resulted in any
    return value. If it didn't, then we can just use the
    default response value and mime type.
    --->
    <cfif structKeyExists( local, "result" )>

        <!---
        Check to see what kind of return format we need to
        use in our transformation. Keep in mind that the
        URL-based return format takes precedence. As such,
        we're actually going to PARAM the URL-based format
        with the default in the function. This will make
        our logic much easier to follow.

        NOTE: This expects the returnFormat to be defined
        on your CFC - a "best practice" with remote
        method definitions.
        --->
        <cfparam name="url.returnFormat" type="string" default="#metadata.returnFormat#" />
        <cfparam name="url.returnType" type="string" default="#metadata.returnType#" /> <!--- Added this line so we can check for returnType of xml --->

        <!---
        Now that we know the URL scope will have the
        correct format, we can check that exclusively.
        --->
        <cfif (url.returnFormat eq "json")>
            <!--- Convert the result to json. --->
            <!---
            We already serializeJSON in the function being called by the user, this would cause double encoding, so just treat as text
            <cfset local.responseData = serializeJSON( local.result ) />
            --->
            <cfset local.responseData = local.result />
            <!--- Set the appropriate mime type. --->
            <cfset local.responseMimeType = "application/json" />
        <!---
        There is no returnFormat of xml so we will check returnType instead.
        This leaves the door open for us to use wddx in future if we decide to.
        --->
        <cfelseif (url.returnType eq "xml")>
            <!--- Convert the result to string. --->
            <cfset local.responseData = local.result />
            <!--- Set the appropriate mime type. --->
            <cfset local.responseMimeType = "application/xml" />
        <cfelseif (url.returnFormat eq "wddx")>
            <!--- Convert the result to XML. --->
            <cfwddx action="cfml2wddx" input="#local.result#" output="local.responseData" />
            <!--- Set the appropriate mime type. --->
            <cfset local.responseMimeType = "application/xml" />
        <cfelse>
            <!--- Convert the result to string. --->
            <cfset local.responseData = local.result />
            <!--- Set the appropriate mime type. --->
            <cfset local.responseMimeType = "text/plain" />
        </cfif>

    </cfif>

    <!---
    Now that we have our response data and mime type
    variables defined, we can stream the response back
    to the client.
    --->

    <!--- Convert the response to binary. --->
    <cfset local.binaryResponse = toBinary( toBase64( local.responseData ) ) />

    <!---
    Set the content length (to help the client know how
    much data is coming back).
    --->
    <cfheader name="content-length" value="#arrayLen( local.binaryResponse )#" />

    <!--- Stream the content. --->
    <cfcontent type="#local.responseMimeType#" variable="#local.binaryResponse#" />

    <!--- Return out. --->
    <cfreturn />
</cffunction>
于 2014-04-03T15:58:51.693 回答
1

我从来没有用过onCfcRequest,但你是对的,这有点愚蠢。

似乎onCfcRequest也会“吞下” returnFormat,因此您必须实现自己的returnFormat检测并序列化为正确的格式。

OnCFCRequest() 方法的 ReturnType 应该是 VOID ,就像它的 OnRequest() 对应部分一样。从这个方法返回一个值似乎对页面响应中实际返回的内容没有任何影响。要返回一个值,您要么必须在方法体中输出它,要么通过 CFContent 将其流回

引自:http ://www.bennadel.com/blog/1647-Learning-ColdFusion-9-Application-cfc-OnCFCRequest-Event-Handler-For-CFC-Requests.htm

例如

...
var result = invoke(o, ARGUMENTS.method, ARGUMENTS.args);
...
<!--- after your </cfscript> --->

<!--- TODO: do some checking to determine the preferred return type --->

<!--- if detected as xml, serve as xml (safer option) --->
<cfcontent type="application/xml" 
           variable="#toBinay(toBase64(local.result))#">

<!--- *OR* (cleaner version) watch out for white spaces --->
<cfcontent type="application/xml">
<cfoutput>#result#</cfoutput>

于 2014-04-02T07:34:58.140 回答