Coldfusion onCFCRequest将XML的返回类型更改为WDDX

时间:2014-04-02 00:50:05

标签: xml coldfusion wddx

我的客户群终于关闭了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

我可能最终会尝试这一点,但首先我想弄清楚为什么我不能简单地以相同的格式传递响应。

2 个答案:

答案 0 :(得分:1)

我从未使用onCfcRequest,但你是对的,这有点愚蠢。

似乎onCfcRequest也会&#34;吞下&#34; 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

E.g。

...
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>

答案 1 :(得分:1)

看来我不能简单地以相同的格式传递结果...&#34; 默认情况下,ColdFusion序列化所有返回类型(包括简单返回类型),但XML除外,转换为WDDX格式,并以XML文本形式返回XML数据。&#34; (http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f5c.html)。被调用的远程函数将xml作为字符串返回onCFCRequestonCFCRequest然后将简单返回类型(此时为字符串)转换为{ {1}}因为这是默认行为。

因此...

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

  1. 第一个补充是添加我的问题中已经显示的代码,用于发现Adam Cameron发现的错误。
  2. 第二个补充是在调用后立即执行此操作:WDDX。在本的评论中,它说&#34; ...返回的所有值都是字符串...... &#34;但事实似乎并非如此。我们有一些只返回数字的远程函数。没有<cfset local.result = ToString(local.result)>代码进一步向下将响应转换为二进制失败。
  3. 在设置mimeTypes的部分中,我改变了ToString()的IF语句。在我们编写的每个远程函数中,我们创建一个ColdFusion结构,然后返回它:json。这比手动拼接json字符串要容易得多,然后在<cfreturn SerializeJSON(z.response) />中对其进行序列化。因此,在json mimeType的onCFCRequest中,我只是将它视为一个字符串,因为它已经被序列化了;所以不需要第二次序列化。
  4. 同样在mimeType部分,我为onCFCRequest添加了一个IF语句。我们有许多远程函数为网格吐出xml,而不是xml。由于wddx returnFormat没有xml,我在returnType支票正上方添加了对xml wddx的检查。
  5. 根据@ Henry的评论,将JSONXML的responseMimeType更改为application/jsonapplication/xml。谢谢!
  6. 非常感谢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>