简体   繁体   English

如何编写原始的多部分SOAP HTTP消息以通过Grails输出

[英]How to write raw multipart SOAP HTTP message to output with Grails

I have a Grails application that should respond with previously stored raw SOAP messages. 我有一个Grails应用程序,应该使用以前存储的原始SOAP消息进行响应。 These messages are read from a database. 这些消息是从数据库中读取的。 When I write the message to output, it is added to body part of the HTTP response. 当我将消息写入输出时,它将被添加到HTTP响应的主体部分。 The results is that reading the response on client end fails as the HTTP headers etc are part of the body. 结果是,由于HTTP标头等是主体的一部分,因此在客户端读取响应失败。 The results on client side looks something like this 客户端的结果看起来像这样

------=_Part_0_1123526246346
Content-Type: application/soap+xml; charset=utf-8
Content-Transfer-Encoding: 8bit
Content-ID: <some-id>

<soap:Envelope>
<!-- Message contents -->
</soap:Envelope>

------=_Part_0_1123526246346
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: <temp.pdf>
Content-Disposition: attachment; name="temp.pdf"

<!-- Lots of binary data -->

%%EOF
------=_Part_0_1123526246346--

All this means that it's a multipart SOAP message where a PDF doc comes as an attachment. 所有这些都意味着这是一条包含SOAP文档的多部分SOAP消息。 The messages written to DB are consumed correctly by the client and only the soap envelope is seen as the body, PDF as an attachment. 客户端正确使用了写入DB的消息,并且仅将肥皂信封视为正文,将PDF视为附件。

How can I write this message as RAW ouput with Grails, so that the HTTP stuff does not end up being duplicated? 如何使用Grails以RAW输出的形式编写此消息,以使HTTP内容最终不会被复制?

If you need full control of the response produced by a controller, including headers, then you should look at the response object which is available. 如果需要完全控制由控制器产生的response (包括标题),则应查看可用的响应对象。 Every controller in Grails has access to the HttpServletResponse through the response object so you can manage the raw response yourself. Grails中的每个控制器都可以通过response对象访问HttpServletResponse ,因此您可以自己管理原始响应。

However, your problem is that you need a multi-part response and the HTTP headers are embedded in your text. 但是,您的问题是您需要一个多部分响应,并且HTTP标头已嵌入文本中。 You should be able to parse them out and manually create a MultiPartResponse using the famous package from Jason Hunter. 您应该能够解析它们,并使用Jason Hunter中著名的软件包手动创建MultiPartResponse

With a bit of parsing of your data, combining the HttpServletResponse available in Grails and the MultipartResponse you should be able to get the results you need. 只需对数据进行一点分析,将Grails中可用的HttpServletResponseMultipartResponse结合起来,您就可以得到所需的结果。

Another possible option is to simply set the headers on the HttpServletResponse to indicate it's a multi-part response and write the text/data straight to the output stream. 另一个可能的选择是简单地在HttpServletResponse上设置标头以指示它是一个多部分响应,然后将文本/数据直接写入输出流。 This may or may not work depending on how it's being consumed, but it's worth a try. 这可能会或可能不会起作用,具体取决于消费方式,但值得尝试。

// some controller method
response.setContentType("multipart/x-mixed-replace")
response.outputstream << theDataAsAByteArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM