I am doing a CFHTTP post to a web service that is returning two parts (multipart), a XML and PDF. I am looking to get only the PDF. My cfhttp.filecontent is a java.io.ByteArrayOutputStream type. When I do a toString() I get the following
Part 1
Content-Type: application/xop+xml; type="text/xml"; charset=utf-8
Content-Transfer-Encoding: 8bit
Part 2
Content-Type: application/pdf
Content-Transfer-Encoding: binary
I get the response in cfhttp.fileContent and the data looks like the following
--MIME_Boundary
Content-ID: <aa82dfa.N51ec355b.3.15b86044531.59d6>
Content-Type: application/xop+xml; type="text/xml"; charset=utf-8
Content-Transfer-Encoding: 8bit
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">....</soapenv:Envelope>
--MIME_Boundary
Content-Id: <2958beaa-dd72-4879-9d80-cc19876b2c2a#example.jaxws.sun.com>
Content-Type: application/pdf
Content-Transfer-Encoding: binary
%PDF-1.4
%ÈÁÄ×
<content removed>
25081
%%EOF
--MIME_Boundary--
I tried to remove all the data that's not related to the PDF but it's still not a valid binary file.
Any thoughts?
From the comments
When I do a cfdump on the fileContent I get the following:
Class Name: java.io.ByteArrayOutputStream
Methods:
close() returns void
reset() returns void
size() returns int
toByteArray() returns byte[]
toString(java.lang.String) returns java.lang.String
toString() returns java.lang.String
toString(int) returns java.lang.String
write(byte[], int, int) returns void
write(int) returns void
writeTo(java.io.OutputStream) returns void
When I invoke toByteArray() I get binary data. I then save the data to a file and I see both XML and PDF parts of the file.
The workaround required two changes: a change to set the accepted encoding value to gzip,deflate and to work with binary data using java.
<cfhttpparam type="HEADER" name="Accept-Encoding" value="gzip,deflate">
Second I needed to manipulate the response using binary methods.
binResponse = result.fileContent.toByteArray();
Next I used a utility from Ben Nadel, Binary.cfc, that has all the binary manipulation I needed. I used the method binarySlice() to extract the start and end part of the binary. The sliced data contains the binary in the exact format that I needed. It was not base64 or any another type, it was binary.
sliced = binNadel.binarySlice( binResponse, <int posistion to start slice>, <int length of binary>));
This solution works, but it's ripe with potential issues, for example the order of the response could switch, the boundary name could change, etc. So this will require a lot of error handling to ensure smooth sailing.
Update:
Next I looked into Leigh's example to see if I could simplify my code. They suggested using Java's MimeMultipart class which supports parsing an MTOM multipart response. Here is the final working code:
<cfscript>
// Modify path as needed
saveToDirec = "c:\temp\";
// Hard coded "boundary" value for DEMO purposes. It MUST match actual value used in cfhttp response
// Best to use cfhttp.responseHeader.content-Type so [if] the service changes your code won't break.
contentType = "multipart/related; boundary=MIME_Boundary;";
// Load and parse ByteArrayOutputStream returned by CFHTTP
dataSource = createObject("java", "javax.mail.util.ByteArrayDataSource").init(m_strSoapResponse.fileContent.toByteArray(), javaCast( "string", contentType));
mimeParts = createObject("java", "javax.mail.internet.MimeMultipart").init(dataSource);
for (i = 0; i < mimeParts.getCount(); i++) {
writeOutput("<br>Processing part["& i &"]");
bp = mimeParts.getBodyPart( javacast("int", i));
// If this part is a PDF, save it to a file.
if (!isNull(bp) && bp.isMimeType("application/pdf")) {
outputFile = createObject("java", "java.io.File").init(saveToDirec &"demo_savedfile_"& i &".pdf");
bp.saveFile(outputFile);
writeOutput("<br>Saved: "& outputFile.getAbsolutePath());
}
}
</cfscript>
Thanks all for your input!
Related
I'm passing in a USERID into what I believe to be either a SOAP request. I've never worked with SOAP and not quite sure where to start so if my question doesn't quite make sense, let me know and I'll try to fill in missing details.
Context for question:
I'm converting an excel macro (the macro will query pass the userid to the server and the server will return employee details such as name, email, address, etc.) and turning into a web lookup.
I have the following code:
<cfscript>
variables.sso = '55555';
variables.serverURL = "http://search.corporate.ge.com/ldq/Query";
variables.queryString = "?serverID=ssoprod&searchBase=ou=domainWorker,+o=domain.com&Prebuilt=true&scope=2&filter=(domainoraclehrid=#variables.sso#)";
variables.webservice = "#variables.serverURL##variables.queryString#";
</cfscript>
<cfdump var="#variables.webservice#">
<cfhttp url="#variables.webservice#" method="get" result="response"></cfhttp>
<cfdump var="#response.fileContent#" label="soap content">
When I take the value from the dump of variables.webservice, and paste it directly into a browser, I get the following (assume userID of 55555):
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dsml="http://www.dsml.org/DSML">
<SOAP-ENV:Body>
<dsml:dsml xmlns:dsml="http://www.dsml.org/DSML">
<dsml:directory-entries>
<dsml:entry dn="domainssouid=1D8B0D04-91F0-1CAE-9BD7-002128B20D70,ou=domainWorker, o=domain.com">
...
<dsml:attr name="employeetype">
<dsml:value>Contractor</dsml:value>
</dsml:attr>
<dsml:attr name="givenname">
<dsml:value>John</dsml:value>
</dsml:attr>
<dsml:attr name="postalcode">
<dsml:value>90210</dsml:value>
</dsml:attr>
<dsml:attr name="domainoraclehrid">
<dsml:value>456456987</dsml:value>
</dsml:attr>
<dsml:attr name="mail">
<dsml:value>John.Doe#domain.com</dsml:value>
</dsml:attr>
<dsml:attr name="cn">
<dsml:value>Doe, John</dsml:value>
</dsml:attr>
...
</dsml:entry>
</dsml:directory-entries>
</dsml:dsml>
</SOAP-ENV:Body>
BUT when I dump of #response.fileContent#, I get something different, I get:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:f="http://www.w3.org/2001/06/soap-faults">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<SOAP-ENV:faultcode>MustUnderstand</SOAP-ENV:faultcode>
<SOAP-ENV:faultstring>ou=domainWorker,+o=domain.com: [LDAP: error code 34 - Invalid DN], Name Not valid - ou=domainWorker,+o=domain.com - filter -(domainoraclehrid=55555) </SOAP-ENV:faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Ultimately what I'd like to do is parse out the "cn", and "mail" details. What am I missing here? I suspect it might have something to do with ME accessing the URL directly (logged in user) where as the request is being made from a server and isn't "authenticated". If that is the case, how can I resolve this?
Update:
(Originally, I was a little perplexed about why I couldn't reproduce your results with CF2018, but now that I know you're using Lucee, the difference makes sense.)
Looking over the successful response, I noticed it doesn't contain a +, before the "o" (organization Name):
<dsml:entry dn="domainssouid=xxxx,ou=domainWorker, o=domain.com">
Which means the VBA call is treating the + as an encoding for a space, but CFHTTP is encoding it as a literal plus sign instead, causing an error because it breaks the LDAP query:
[LDAP: error code 34 - Invalid DN], Name Not valid - ou=domainWorker,+o=domain.com
The solution is to get rid of the plus sign + and replace it with a space:
searchBase=ou=domainWorker, o=domain.com
Interestingly, dumping the http request data shows that apparently CF2018 does things differently. Unlike Lucee, CF2018 treats the plus sign as a space.
CF2018 => %20 (space)
searchBase=ou%3DdomainWorker%2C%20o%3Ddomain.com
Lucee 5.2.8.50 => %2B (plus sign)
searchBase=ou%3DdomainWorker%2C%2Bo%3Ddomain.com
I am developing a SOAP client in Matlab for connection a Web Service. What I am doing is the following script:
createClassFromWsdl('http://192.168.107.239/WSDL/v4.0/iLON100.wsdl')
obj = iLON100
methods(obj)
With the next result:
Methods for class iLON100:
Clear Get List Set display
Delete InvokeCmd Read Write iLON100
Then, I am editing for example the method List in order to request the list of Items for the service. The dot m file is:
% Build up the argument lists.
values = { '','//Item[#xsi:type="Dp_Cfg"]'};
names = { 'iLonItem','xSelect'};
types = {};
% Create the message, make the call, and convert the response into a variable.
soapMessage = createSoapMessage('http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/',
'List', values, names, types, 'document');
I have also a SOAP tester from the vendor of the device. Then, if I compare both XML requests, they differ as you can see in the next example (firstly the original request and secondly the Matlab one):
<SOAP-ENV:Body>
<List xmlns="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<iLonItem>
<xSelect>
//Item[#xsi:type="Dp_Cfg"]
</xSelect>
</iLonItem>
</List>
</SOAP-ENV:Body>
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<List xmlns="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<iLonItem/>
<xSelect>Item</xSelect>
</List>
</soap:Body>
As you can observe, the tags are not included as sub-tags. I would like to know how to do it and generate the same structure of XML for sending the SOAP request correctly.
Thank you so much,
At the end, I have solved it by creating a structure as follows:
myStruct = struct('iLonItem',struct('xSelect','//Item[#xsi:type="Dp_Cfg"]'))
This structure is inserted into "values" and the method's name "iLONItem".
I am trying to parametrise web service requests in a web performance test. Using Fiddler2 I have recorded a sequence of over 60 web service requests for a transaction performed by my desktop application and saved them as a .webtest file. This web test runs without any errors and the responses that I have checked look correct.
When the web service requests are viewed in Visual Studio 2012 they appear in plain text and so I should be able to edit them to parametrise the values in the SOAP requests. For example, most of the requests contain the text <Database>db1a</Database> (actually it has <Database>db1a</Database>) and I want to change them to get the database name from a context parameter. There are several other items to replace with parameters. For this one transaction there are over 60 web service requests and I have other transactions to record. The .webtest file contains XML and the requests looks like:
<Request Method="POST" Version="1.1" Url="http://example.com/somewhere.asmx" ThinkTime="83" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8">
<Headers>
<Header Name="Content-Type" Value="text/xml; charset=utf-8" />
<Header Name="SOAPAction" Value=""http://example.com/webservices/VariousActionNamesHere"" />
</Headers>
<StringHttpBody ContentType="text/xml; charset=utf-8">PAA/AHgAbQBsACAAdg
... lots more characters not shown
+AA==</StringHttpBody>
</Request>
The StringHttpBody field contains an encoded version of the SOAP request. Visual Studio shows it as plain text. What is the encoding of this field and how can I decode and encode it?
I have installed Release 3.0 of the “Web and Load Test Plugins for Visual Studio Team Test” from http://teamtestplugins.codeplex.com/ . They provide a slightly better interface for editing the SOAP requests one at a time. But they do not allow mass changes.
Converting the web test to a coded web test (ie into C#) shows the SOAP requests as simple text and they could be edited there but I would prefer to keep the flexibility of a .webtest file.
Update: I have posted a partial answer to the question. Whilst it works, it feels the wrong way to do the work because it feels too complicated. So I am looking for a better overall approach.
StringHttpBody is base64 encoded. The raw body of the request is converted to a UTF-16 byte array and then base64 encoded, like so:
Convert.ToBase64String(Encoding.Unicode.GetBytes(oSession.GetRequestBodyAsString()));
For a quick view, you can copy/paste this string into Fiddler's Tools > TextWizard screen then use the From Base64 option to decode.
Here is part of an answer to working with the StringHttpBody fields. This is about decoding and encoding the fields to allow easier understanding and modification.
Read the input XML and find the contents of the StringHttpBody fields. Replace each field contents with the result of calling the following routine on the original contents. Write the all the lines to a new intermediate file. The byte array contains UTF-16 characters as high and low bytes. (All the characters I have seen so far have high byte zero.)
private string DecodeBody(string source) {
byte[] outBytes = Convert.FromBase64String(source);
StringBuilder sb = new StringBuilder();
Assert( (outBytes.Length % 2) != 0 );
for (int ix = 0; ix < outBytes.Length; ix += 2) {
Assert(outBytes[ix] != 0);
sb.Append((char)outBytes[ix + 1]);
}
return sb.ToString();
}
Now have a file containing a simple text version of the .webtest file. This file can easily be edited to parameterise fields of the requests. Have an routine used similar to the one above and writing to another intermediate file. The routine has statements such as:
source = source.Replace("<Database>db1a</Database>", "<Database>{{DatabaseName}}</Database>");
The final intermediate file is then reencoded to create a new .webtest file. Just as before the contents of the StringHttpBody fields are found and replaced with the result of calling a routine. The encoding routine is:
private string EncodeBody(string source) {
StringBuilder sb = new StringBuilder();
byte[] outBytes = new byte[2 * source.Length];
for (int ix = 0; ix < source.Length; ix++) {
char ch = source[ix];
outBytes[2 * ix] = (byte)(((int)ch) & 0xFF);
outBytes[2 * ix + 1] = (byte)((((int)ch) / 256) & 0xFF);
}
sb.Append(Convert.ToBase64String(outBytes));
return sb.ToString();
}
The flow of files is thus:
decode original.webtest > intermediate1
parameterise intermediate1 > intermediate2
encode intermediate2 > final.webtest
On the small number of .webtest files I have tried the encode operation is the inverse of the decode operation, the original file from before decoding is identical to the file after encoding. Having the two intermediate files allows easy checking and searching of the contents of the unencoded file and the effect of the parameterise step.
For
[StringHttpBody ContentType="application/json"]
To Decode the body:
var encodedString = childNode.InnerText;
var encodedStringBytes = Convert.FromBase64String(encodedString);
var decodedString = Encoding.Unicode.GetString(encodedStringBytes);
JObject jsonString =JsonConvert.DeserializeObject(decodedString);
To Encode the body:
childNode.InnerText =
Convert.ToBase64String(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(jsonString)));
I think this might help.
I am at my wit's end on this one, I just can't find the right combination of code to make this work. I'm trying to create an authentication digest for an API query. I've tried many CFML functions (for example: Coldfusion HMAC-SHA1 encryption and HMAC SHA1 ColdFusion), but I'm not coming up with the same results that are cited in the API documentation. Here's that example (basically elements of the request header with line breaks as delimiters.):
application/xml\nTue, 30 Jun 2009 12:10:24 GMT\napi.summon.serialssolutions.com\n/2.0.0/search\ns.ff=ContentType,or,1,15&s.q=forest\n
and here's the key:
ed2ee2e0-65c1-11de-8a39-0800200c9a66
which according to the documentation should result in:
3a4+j0Wrrx6LF8X4iwOLDetVOu4=
when the HMAC hash is converted to Base64. Any ideas would be most appreciated!
The problem is your input string, not the functions. The first one works fine. Though I would change the charset to UTF-8, or make it an argument. Otherwise, the results are dependent on the jvm default, which may not always be correct, and can change which would break the code.
Verify you are constructing the sample string correctly. Are you using chr(10) for new lines? Note: It must also end with a new line.
Code:
<cfscript>
headers = [ "application/xml"
, "Tue, 30 Jun 2009 12:10:24 GMT"
, "api.summon.serialssolutions.com"
, "/2.0.0/search"
, "s.ff=ContentType,or,1,15&s.q=forest"
];
theText = arrayToList(headers, chr(10)) & chr(10);
theKey = "ed2ee2e0-65c1-11de-8a39-0800200c9a66";
theHash = binaryEncode( hmacEncrypt(theKey, theText), "base64");
writeDump(theHash);
</cfscript>
Result:
3a4+j0Wrrx6LF8X4iwOLDetVOu4=
I have a webservice in C#.NET with the following namespace:
[WebService (Namespace = "http://enterpriseName/wsName")]
The web service contains a WebMethod GetServiceObject and a class MyObject.
This web method returns a string whose content is a serialized instance of MyObject.
[WebMethod (MessageName = "GetServiceObjectXML" Description = "Get ServiceObject from Server to Client")]
public string GetServiceObjectXML ()
This method returns the following XML:
<? Xml version = "1.0" encoding = "utf-16"?>
<ServiceObject Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services>
<service>
<id>3</id>
<date>02/08/2010</date>
</service>
</Services>
</ServiceObject>
The problem that I encounter is that when I call this method from the client side and try to deserialize this xml to class MyObject and I get a NULL object.
After that I created a new WebMethod with the following signature:
[WebMethod (MessageName = "GetServiceObject" Description = "Get ServiceObject from Server to Client")]
public MyObject GetServiceObject ()
When I call this method from the client side I get the object filled correctly and I can also serialize the object without problems, but the result of serialization is the following xml:
<? Xml version = "1.0" encoding = "utf-16"?>
<ServiceObject Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services Xmlns="http://enterpriseName/wsName">
<service>
<id>3</id>
<date>02/08/2010</date>
</service>
</Services>
</ServiceObject>
which is different from the xml generated by the WebMethod GetServiceObjectXML.
How can I get around this, since I intend to use both methods on the same webservice and in the same customer?
The obvious answer would be, fix GetServiceObjectXML() to return the same XML as GetServiceObject(). The difference seems to be that the object as serialized by the framework has a different XML namespace specified. Whatever method you're using to serialize the object into XML in GetServiceObjectXML() isn't doing that.