Parsing a multipart SOAP response in Flex? - apache-flex

I have a Flex application that needs to grab reporting data from a JasperReports Server, through the JasperReports Server Web Services API. Flex Builder 3 does a nice job of generating the web services consumption code, with one exception. If you ask for a directory list or an accounting of report parameters, JR Server returns plain XML in the SOAP wrapper and Flex parses it just fine. However, if you ask for a report itself, whether in XML or PDF format, it comes back as a multipart MIME message with some descriptive XML as the first part and the report -- even if the report itself is XML -- as the second part. Flex doesn't know what to do with multipart messages and just complains about invalid XML.
Here's a sample of the response. My current strategy is do some string parsing and manage the bits individually. But does Flex have any built-in methods to handle this? (I've been unable to find any.)
------=_Part_2_27050467.1235668849951
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <0F082AF1DAF83B3077B1867B4FC8AAA6>
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:runReportResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://axis2.ws.jasperserver.jaspersoft.com">
<runReportReturn xsi:type="xsd:string"><?xml version="1.0" encoding="UTF-8"?>\n<operationResult version="2.0.1">\n\t<returnCode><![CDATA[0]]></returnCode>\n</operationResult>\n
</runReportReturn>
</ns1:runReportResponse>
</soapenv:Body>
</soapenv:Envelope>
------=_Part_2_27050467.1235668849951
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-Id: <report>
%PDF-1.4\n%âãÏÓ\n1 0 obj <</Filter/FlateDecode/Length 29>>stream\nx+är\ná26S°00S\bIár\rá\näâ

What I am going to say is only what I believe:I may be wrong since I haven't tried this out.
I don't think you have much choice. Flex HTTPService (or whatever you are using) can offer only so many (/or so few) data formats. See here. For any custom stream you will have to retrieve it as an object and pass it through your own decoder. It appears that Flex does not do any parsing of the MIME message but depends on (which is also how browsers behave typically) the server to identify the content that is being transmitted. If the server only sends a text file but changes the content type to say audio/mpeg I think you will have the same problem. Even when Flex does know hot to handle text.
If you ever can get around to doing this experiment (with text files as MIME type video or whatever fancies you) do let us know.
Meanwhile, you can take a look at SerializationFilter and go on and add a new MIME type for PDF!
Hopefully, life will be a little easy with Flex 4 and the HTTPMultiService.

Related

Difference between soap and xml over http?

I understand that we can encode any object in xml and send the xml in post request over http.So What extra advantage did we have using soap and why it became popular.
Your question is very generic and broad, hence there could long discussion/debate on whether SOAP is popular, and its pros vs cons. Even not sure if its duplicate question.
I would like to answer it shortly.
Because SOAP is standard because accepted/published by W3.org, hence widely accepted, and XML RPC or just XML over HTTP is not, hence would be less acceptable to organizations/service providers/developers.
SOAP as per wiki
SOAP (originally Simple Object Access Protocol) is a messaging protocol specification for exchanging structured information in the implementation of web services in computer networks. Its purpose is to induce extensibility, neutrality and independence. It uses XML Information Set for its message format, and relies on application layer protocols, most often Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
XML over HTTP as per wiki,
XML-RPC is a remote procedure call (RPC) protocol which uses XML to encode its calls and HTTP as a transport mechanism.1 "XML-RPC" also refers generically to the use of XML for remote procedure call, independently of the specific protocol. This article is about the protocol named "XML-RPC".
Hence, XML over HTTP is subset of SOAP.
Meaning, every SOAP transaction is also XML over HTTP/HTTPS, but every XML over HTTP/HTTPS can't be SOAP.
SOAP Example XML,
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
XML over HTTP example:
<array>
<data>
<value><i4>1404</i4></value>
<value><string>Something here</string></value>
<value><i4>1</i4></value>
</data>
</array>
I would suggest you to do googling to more details, both are wide topics and I think can't be 100% in stack-overflow answer.

Is the order of the parameters important in an ASPNET web service with a SOAP 1.0 XML request?

When we try to call a web service using a java client (Axis) we are sending the method parameters fields in different order.
That is to say, if the WSDL order of the parameters is like;
<soap:Body>
<somemethod>
<messagetype>
**<x>int</x>**
<y>int</y>
</messagetype>
</somemethod>
</soap:Body>
and if we send them like
<soap:Body>
<somemethod>
<messagetype>
**<y>int</y>**
<x>int</x>
</messagetype>
</somemethod>
</soap:Body>
parameters are not filled in the ASPNET side. "x" and "y" stays as zeros ("0").
Any ideas if this is the standard?
Well, a SOAP request is just XML so my thoughts are that it's the SOAP implementation that would determine how parameters should match up.
But that varies from vendor to vendor. Some vendors may grab SOAP parameter values by element name (the proper way) and others may grab the values by node position (what you're probably running into).
Stick to the format specified by the WSDL and nobody gets hurt.

How should I parse Request.GetBufferlessInputStream()

I'm working on an upload control for ASP.NET, and I need to work with Request.GetBufferlessInputStream()
This returns the raw unprocessed request stream. Is there a built in way to parse the content of this stream, stripping out headers such as the example I've copied below.
If not what is the best approach to parsing the file?
-----------------------------13166267887793
Content-Disposition: form-data; name="uploadFile"; filename="ABigFile.txt"
Content-Type: text/plain
The solution came from inspecting how it's done in System.Web, and adapting to my needs. The core functionality is found in an internal class called HttpMultipartContentTemplateParser. The system for processing the input stream is written up here:
http://blog.appsoftware.com/2014/03/aspnet-file-uploader-with-signalr.html

How can I decode a multipart HTTP response?

I have an ASP.NET web application that's returning a multi-part response containing a JSON-encoded object, plus a bunch of binary files. (For the code that produces this, see https://stackoverflow.com/a/12334553/98422 - that question also gives a bit of background).
The HTTP response data looks like this:
--b621907a-a740-44f4-b495-a91a078b90ef
Content-Type: application/json; charset=utf-8
[1,2]
--b621907a-a740-44f4-b495-a91a078b90ef
Content-Type: image/jpeg
<image file content>
--b621907a-a740-44f4-b495-a91a078b90ef
Content-Type: text/plain
<text file content>
--b621907a-a740-44f4-b495-a91a078b90ef--
So, I now want to consume the response at the client end. My client is a C# application.
How can I do this? I assume that there must be some framework support for it, but I'm not sure where to start looking.
It probably isn't relevant, but I'm using RestSharp for the bulk of my interaction with the web server. So far as I can tell, RestSharp would not be able to process this response (which is fair enough - it's not very RESTy).
What options do I have?

append conditional html file output with xml response

hey guys i have a restful xml service where client passes current version of html they are viewing. if the version on the server is the same as the client, i just respond with the current server version in xml. example: <Response ServerHTMLVersion="1" />
however if server html version is greater than current client version, i still spit out the same response like above like <Response ServerHTMLVersion="2" />. but problem being my client application needs to do a seperate http request to download the html file incase response xml version is greater than clients version
for performance reasons, i wanted to cut down this http request and i wanted to know what is the best way to do this. should i simply encode the html to make it xml safe and append that with xml response - problem with this being html is FAT and encoding makes it even fatter
OR
is there a better way of managing this? note that i am already gziping my response for both, xml as well as html right now
i wanted to know the way to do this keeping performance in mind. the restful xml service is implemented via asp.net 3.5 and iis 7
Have you thought about using HTTP headers? Since really the primary data here is the HTML, and the ServerHTMLVersion is a sort of "meta data" about that html, it should work.
Personally, I'd make the response to the request 1) blank when the versions match and 2) the HTML for non-matching versions; then, use the Pragma HTTP header to send something like Pragma: "ServerHTMLVersion=2". By doing this, you can easily check if the client and server versions differ, and just grab the full response if they're different.
Some people would debate the idea of returning HTML from a REST service, but I personally would consider this totally valid, and an nice clean way of separating your meta data from the actual user data.
-Jerod

Resources