I have a Grails application that should respond with previously stored raw SOAP messages. These messages are read from a database. When I write the message to output, it is added to body part of the HTTP response. The results is that reading the response on client end fails as the HTTP headers etc are part of the body. 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. 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.
How can I write this message as RAW ouput with Grails, so that the HTTP stuff does not end up being duplicated?
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. Every controller in Grails has access to the HttpServletResponse through the response
object so you can manage the raw response yourself.
However, your problem is that you need a multi-part response and the HTTP headers are embedded in your text. You should be able to parse them out and manually create a MultiPartResponse using the famous package from Jason Hunter.
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.
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. 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