accesing xml content from node.js - node.js

I have a bunch of XML documents in MarkLogic database.
I am working on a node.js middletier, it needs to pass on certain properties of all xml documents in a certain collection into a JSON array.
So far I have this:
var marklogic = require('marklogic');
var my = require('./my-connection.js');
var db = marklogic.createDatabaseClient(my.connInfo);
var qb = marklogic.queryBuilder;
db.documents.query(
qb.where(qb.collection("alert"))
).result( function(documents) {
console.log('The alerts collection:')
documents.forEach( function(document) {
//console.log('\nURI: ' + document.uri);
var aObj = document.content
//var alert = aObj.getElementsByTagNameNS("http://example.com/sccs/alert","alert");
console.log('Alert: ' + document.content);
});
}, function(error) {
console.log(JSON.stringify(error, null, 2));
});
Which gives me the content back.
Example result:
Alert: <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0"?>
<obj:object xmlns:obj="http://marklogic.com/solutions/obi/object">
<obi:metadata xmlns:obi="http://marklogic.com/solutions/obi" createdBy="admin" createdDateTime="2015-09-23T20:42:48.562829Z" lastUpdatedBy="admin" lastUpdatedDateTime="2015-09-23T20:42:48.562829Z"/>
<obj:label>Active</obj:label>
<obj:type>alert</obj:type>
<obj:id>41e718eb-a2e2-49e0-939a-68bb87c1e301</obj:id>
<obj:content>
<alert xmlns="http://example.com/sccs/alert">
<obj:property sourceCount="1">
<status>Active</status>
</obj:property>
<obj:property sourceCount="1">
<position>{"type":"Point", "coordinates":[52.2, 4.3]}</position>
</obj:property>
<obj:property sourceCount="1">
<scc:id xmlns:scc="http://example.com/sccs">2a65b4cc-acee-4675-a8ba-d8c5dfaac9dd</scc:id>
</obj:property>
</alert>
</obj:content>
<obj:workspaces>
<obj:workspace id="Public"/>
</obj:workspaces>
<obj:sourceIds count="1">
<source:id xmlns:source="http://marklogic.com/solutions/obi/source">42aebdc7-41aa-4695-b514-2bb63f85d47c</source:id>
</obj:sourceIds>
</obj:object>
Question:
I want to access for example the 'obj:content' element in this content.
In the Query Console I can do this by :
var alerts = fn.collection("alert").toArray();
var aXML = alerts[2].getElementsByTagNameNS("http://example.com/sccs/alert","alert");
How can I do this from node.js?
(I do not understand the object type returned from the query in node.js)

The MarkLogic node.js API returns XML documents as strings; you'll need to use an XML parser in order to query, project, or process that XML. There are lots of modules for interacting with XML on npm, see
The best node module for XML parsing
https://www.npmjs.com/search?q=xml
You may find it easier to use a server-side transformation with MarkLogic, and convert your XML documents to JSON first: http://docs.marklogic.com/guide/node-dev/extensions#id_78179

Related

How to save split file using xsl:result-document in Azure, FTP and SFTP through c# xsltTransformer?

I am trying to split output into multiple file by using xsl:result-document. But not sure how to store the split result in Azure, FTP and SFTP server.
Input XML:
<ArrayOfBatch>
<Batch>
<BatchName>BatchName-1</BatchName>
</Batch>
<Batch>
<BatchName>BatchName-2</BatchName>
</Batch>
<Batch>
<BatchName>BatchName-3</BatchName>
</Batch>
</ArrayOfBatch>
XSLT Format:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="3.0" exclude-result-prefixes="fn xs">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="/ArrayOfBatch/Batch">
<xsl:result-document href="Batch-{position()}.xml" >
<xsl:copy-of select="*"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
c# code:
public static string SaxonProcessorXSLT(string xmlToExport, string xslStylesheet)
{
using (StringReader xmlStream = new StringReader(xmlToExport))
{
using (StringReader xslStream = new StringReader(xslStylesheet))
{
Processor xsltProcessor = new Processor();
DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder();
documentBuilder.BaseUri = new Uri("file://");
XdmNode xdmNode = documentBuilder.Build(xmlStream);
XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler();
XsltExecutable xsltExecutable = xsltCompiler.Compile(xslStream);
XsltTransformer xsltTransformer = xsltExecutable.Load();
xsltTransformer.InitialContextNode = xdmNode;
xsltTransformer.BaseOutputUri = new Uri("D://home//data//");
using (StringWriter stringWriter = new StringWriter())
{
Serializer serializer = xsltProcessor.NewSerializer();
serializer.SetOutputWriter(stringWriter);
xsltTransformer.Run(serializer);
return stringWriter.ToString();
}
}
}
}
From the above code the result of split file is stored in local path "D://home//data8//" but I am looking for a solution to store this result in azure, FTP and SFTP server by using URI on xsltTransformer.BaseOutputUri.
I have done some R&D on this but its not solving my problem. Looking for quick response. Thanks in advance.
For the result documents, you can set a ResultDocumentHandler on the XsltTransformer (https://www.saxonica.com/html/documentation10/dotnetdoc/Saxon/Api/XsltTransformer.html#ResultDocumentHandler) that could then create a Serializer over a Stream or Writer a .NET (S)FTP client gives you.
A assume you can use Serializer.SetOutputStream to provide a stream representing the remote file:
serializer.SetOutputStream(remoteFileStream);
Use your favorite FTP/SFTP library to create the output stream.
For SFTP, you can use SSH.NET SftpClient.Open.
For FTP, you can use FtpWebRequest.GetRequestStream. Some examples:
Upload a file to an FTP server from a string or stream
Upload a streamable in-memory document (.docx) to FTP with C#?

Error parsing xml when namespace directives are present

I need to read multiple XML-files using node.js. When the root node contains namespace directives, parsing the xml file fails. When removing the namespace directives, all works well. All my files can have different declarations. How do I parse the XML, ignoring the namespace attributes? I need to use xPath to get some values.
I'm using ...
var fs = require('fs');
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;
var xml = fs.readFileSync('/test.xml', 'utf8').toString();
var doc = new dom().parseFromString(xml);
var id = xpath.select("/export/asset/id", doc);
console.log(id[0].firstChild.data);
XML-file
<export xmlns="some url" xmlns:xsi="some url" format="archive" version="2.4" xsi:schemaLocation="some url.xsd">
<asset>
<id>1445254514291</id>
<name>test</name>
<displayName />
<origin>demo</origin>
</asset>
<export>
Generally speaking, taking into account namespaces is preferable, but if you have to deal with too many of these, one way to avoid them altogether is to use xpath and the somewhat convoluted, but effective, local-name() function.
So I would change
var id = xpath.select("/export/asset/id", doc);
to
var id = xpath.select("//*[local-name()='export']//*[local-name()='asset']//*[local-name()='id']", doc);
With the sample xml in the question, this should output:
"1445254514291"

Editing XML files with Node.JS

So as you can see, we have this XML
<?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>
<GetECConfigResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>2.0</Version>
<DeviceId>4498122730</DeviceId>
<MessageId>ECDK-4498122730-39653600512638909</MessageId>
<TimeStamp>1599408189821</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<ContentPrefixURL>http://example.com</ContentPrefixURL>
<UncachedContentPrefixURL>http://example.com</UncachedContentPrefixURL>
<SystemContentPrefixURL>http://example.com</SystemContentPrefixURL>
<SystemUncachedContentPrefixURL>http://example.com</SystemUncachedContentPrefixURL>
<EcsURL>http://example.com</EcsURL>
<IasURL>http://example.com</IasURL>
<CasURL>http://example.com</CasURL>
<NusURL>http://example.com</NusURL>
</GetECConfigResponse>
</soapenv:Body>
</soapenv:Envelope>
I do this so I can edit stuff inside the xml depending on the user's POST body but I have to edit the stuff I want, and turn it into a XML back again and send it as a response, since it's not possible with Node.JS to edit the XML directly without converting to JS or JSON
const xmlParser = require("xml2json");
var json2xml = require("json2xml");
// Current time in epoch
function currentEpoch() {
const now = Date.now()
return now
}
// Read the XML file
fs.readFile("./ecs/ecommercesoap/getecconfigresponse.xml", function (err, data) {
// Convert the XML to JSON
const xmlObj = xmlParser.toJson(data, {
object: true
})
// Get the user's body details
var deviceId = req.body["DeviceId"]
// Change XML index depending on the user
xmlObj["soapenv:Envelope"]["soapenv:Body"]["GetECConfigResponse"]["DeviceId"] = deviceId
xmlObj["soapenv:Envelope"]["soapenv:Body"]["GetECConfigResponse"]["TimeStamp"] = currentEpoch().toString()
This is where it converts xmlObj back to xml again
// Convert the JSON to XML and finalize it
const finalXml = xmlParser.toXml(xmlObj)
// Set the response's type as application/xml
res.type('application/xml');
// Sending the completely edited XML back to the user
res.send(finalXml)
And as you can see, the output changes when the converting happens. The XML below is after it's edited and parsed back into XML from JSON. If you noticed, <?xml version="1.0" encoding="utf-8"?> is also deleted, which shouldn't be.
<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>
<GetECConfigResponse xmlns="urn:ecs.wsapi.broadon.com" Version="2.0" DeviceId="4498122730" MessageId="ECDK-4498122730-39653600512638909" TimeStamp="1599505063565" ErrorCode="0" ServiceStandbyMode="false" ContentPrefixURL="http://example.com" UncachedContentPrefixURL="http://example.com" SystemContentPrefixURL="http://example.com" SystemUncachedContentPrefixURL="http://example.com" EcsURL="http://example.com" IasURL="http://example.com" CasURL="http://example.com" NusURL="http://example.com"/>
</soapenv:Body>
</soapenv:Envelope>
So I wonder what can I do to fix this, the tags start and end with <> in the original while edited one doesn't even have it. Long story short, bunch of stuff got changed. I also tried xml2js instead of xml2json but the output is still the same. I just want my output exactly same as the original file, but with my stuff changed.
In our project we have to deal with manually modifiying soap/xml in NodeJS as well - we've recently switched to fast-xml-parser and are really happy with it. I took your sample input and using the following code:
const xmlToJsonParser = require('fast-xml-parser');
const options = {
ignoreAttributes: false,
ignoreNameSpace: false,
parseNodeValue: true,
parseAttributeValue: true,
trimValues: true,
};
// xmlData refers to the xml-string
const tObj = xmlToJsonParser.getTraversalObj(xmlData, options);
const jsonObj = xmlToJsonParser.convertToJson(tObj, options);
// modify json
jsonObj["soapenv:Envelope"]["soapenv:Body"]["GetECConfigResponse"].DeviceId = 123456;
const JsonToXmlParser = require("fast-xml-parser").j2xParser;
const parser = new JsonToXmlParser({format: true, ignoreAttributes: false});
const xml = parser.parse(jsonObj);
console.log(xml);
it produces:
<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>
<GetECConfigResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>2</Version>
<DeviceId>123456</DeviceId>
<MessageId>ECDK-4498122730-39653600512638909</MessageId>
<TimeStamp>1599408189821</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<ContentPrefixURL>http://example.com</ContentPrefixURL>
<UncachedContentPrefixURL>http://example.com</UncachedContentPrefixURL>
<SystemContentPrefixURL>http://example.com</SystemContentPrefixURL>
<SystemUncachedContentPrefixURL>http://example.com</SystemUncachedContentPrefixURL>
<EcsURL>http://example.com</EcsURL>
<IasURL>http://example.com</IasURL>
<CasURL>http://example.com</CasURL>
<NusURL>http://example.com</NusURL>
</GetECConfigResponse>
</soapenv:Body>
</soapenv:Envelope>
All that's left is to prepend the xml-header, as they've decided to exclude this feature from the parser (https://github.com/NaturalIntelligence/fast-xml-parser/issues/184).

how to pull the parameter from cdata response and use it in another request using groovy in soapUi

This is my response from soapUI
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchAirFaresResponse xmlns="http://www.sample.com/xchange">
<SearchAirFaresResult>
<![CDATA[
<FareSearchResponse>
<MasterDetails>
<CurrencyCode>INR</CurrencyCode>
<RecStNo>1</RecStNo>
<SessionID>5705b1a6-95ac-486c-88a1f90f85e57590</SessionID>
</MasterDetails>
</FareSearchResponse>
]]>
</SearchAirFaresResult>
</SearchAirFaresResponse>
</soap:Body>
</soap:Envelope>
How to extract SessionID element which is inside CDATA using groovy script and use it in another request like
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<xch:GetMoreFares>
<xch:sGetMoreFare>
<![CDATA[
<MoreFlights>
<MasterDetails>
<NoOfResult Index="1">40</NoOfResult>
<BranchId>1</BranchId>
<SessionId>5705b1a6-95ac-486c-88a1f90f85e57590</SessionId>
</MasterDetails>
<Journey>DOM</Journey>
<ResponseType>XML</ResponseType>
<SearchType>OW</SearchType>
</MoreFlights>
]]>
</xch:sGetMoreFare>
</soap:Body>
</soap:Envelope>
3.I have been searching lot but didnt get the right one,and also am new to groovy script using soapUi , pls guide me stepwise procedure in soapUi to implement .
To do so you can use a Groovy testStep, inside it get the SOAP testStep where you've the response with desired sessionID and use a XmlSlurper to parse the response and get the CDATA value. Note that XmlSlurper treat CDATA as String so you've to parse it again. Finally save the returned value as a TestSuite or TestCase level (in the example I use TestCase):
// get your first testStep by its name
def tr = testRunner.testCase.getTestStepByName('Test Request')
// get your response
def response = tr.getPropertyValue('response')
// parse the response and find the node with CDATA content
def xml = new XmlSlurper().parseText(response)
def cdataContent = xml.'**'.find { it.name() == 'SearchAirFaresResponse' }
// XmlSlurper treat CDATA as String so you've to parse
// its content again
def cdata = new XmlSlurper().parseText(cdataContent.toString())
// finally get the SessionID node content
def sessionId = cdata.'**'.find { it.name() == 'SessionID' }
// now save this value at some level (for example testCase) in
// order to get it later
testRunner.testCase.setPropertyValue('MySessionId',sessionId.toString())
Then change a bit your second testStep to use property expansion to get the MySessionId property in your second request as ${#TestCase#MySessionId}:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<xch:GetMoreFares>
<xch:sGetMoreFare>
<![CDATA[
<MoreFlights>
<MasterDetails>
<NoOfResult Index="1">40</NoOfResult>
<BranchId>1</BranchId>
<SessionId>${#TestCase#MySessionId}</SessionId>
</MasterDetails>
<Journey>DOM</Journey>
<ResponseType>XML</ResponseType>
<SearchType>OW</SearchType>
</MoreFlights>
]]>
</xch:sGetMoreFare>
</soap:Body>
</soap:Envelope>
This is also most similar to albciff's answer, but with little variant (using reusable closure to parse).
Here is the Script Assertion for the first request step. This will avoid additional groovy script step in the test case.
Please follow the appropriate comments inline:
Script Assertion:
/**
* This is Script Assertion for the first
* request step which extracts cdata from response,
* then sessionId from extracted cdata. And
* Assigns the value at test case level, so that
* it can be used in the rest of the test steps of
* the same test case.
* However, it is possible to store the sessionId
* at Suite level as well if you want use the sessionId
* across the test cases too.
*
* */
/**
* Closure to search for certain element data
* input parameters
* xml data, element name to search
**/
def searchData = { data, element ->
def parsedData = new XmlSlurper().parseText(data)
parsedData?.'**'.find { it.name() == element} as String
}
//Assert the response
assert context.response, "Current step response is empty or null"
//Get the cdata part which is inside element "SearchAirFaresResult"
def cData = searchData(context.response,'SearchAirFaresResult')
//Assert CDATA part is not null
assert cData, "Extracted CDATA of the response is empty or null"
//Get the SessionID from cdata
def sessionId = searchData(cData, 'SessionID')
//Assert sessionId is not null or empty
assert sessionId, "Session Id is empty or null"
log.info "Session id of response $sessionId1"
//Set the session to test case level custom property
//So, that it can be used for the rest of the steps
//in the same test case
context.testCase.setPropertyValue('SESSION_ID', sessionId)
In the following test steps, you can use the saved SESSION_ID in the following ways:
If the following step is request step (REST, SOAP, HTTP, JDBC etc), then use property expansion ${#TestCase#SESSION_ID} like <SessionId>${#TestCase#SESSION_ID}</SessionId>
If the following step is groovy script, then use one of the below:
context.expand('${#TestCase#SESSION_ID}') or
context.testCase.getPropertyValue('SESSION_ID') or
testRunner.testCase.getPropertyValue('SESSION_ID')

Can anyone give me example how to create new IBM Connections Activity using xPages Social Enabler?

Can anyone give me an example how to create new IBM Connections Activity using xPages Social Enabler? I cant find any usefull info in documentation so I have adapted an example from Niklas Heidloff on how to create a new bookmark in Connections. I have the following code for creating a new activity:
try {
var svc = new sbt.ConnectionsService("/activities/service/atom2/activities");
var sb = new java.lang.StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<entry xmlns:snx=\"http://www.ibm.com/xmlns/prod/sn\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:thr=\"http://purl.org/syndication/thread/1.0\" xmlns=\"http://www.w3.org/2005/Atom\">");
sb.append("<title type=\"text\">");
sb.append("test activity from xpages");
sb.append("</title>");
sb.append("<content type=\"html\">");
sb.append("</content>");
sb.append("</entry>");
var msg = svc.post(null, sb.toString(), "xml");
} catch(e) {
print(e)
}
But code above do not create anything but raises error on Domino console. This is returned by svc.post() command:
[31726:00075-3041917840] 11/19/2012 01:03:59 PM HTTP JVM: Client service request to: http://vhost1279.site1.compute.ihost.com:81/activities/service/atom2/activities did not return OK status. Status returned: 415, reason: Unsupported Media Type, expected:information, please consult error-l
[31726:00075-3041917840] 11/19/2012 01:03:59 PM HTTP JVM: g-0.xml located in /local/opt/ibm/lotus/notesdata/domino/workspace/logs
[31726:00075-3041917840] 11/19/2012 01:03:59 PM HTTP JVM: com.ibm.xsp.extlib.sbt.services.client.ClientServicesException: HTTP Status 415, Unsupported Media Type. HTTP error response code received in response to request to url: http://vhost1279.site1.comties/service/atom2/activities
Can anyone give me a hint how to use it properly or point me to some usefull documentation?
Don't use a StringBuilder to create XML. At least use SAX or better Apache Abdera for creating XML (Tutorial here). This ensures that your XML is valid and in case of Abdera also valid ATOM.
It is important to use this approach, since you get a Node object in return, that automatically triggers the needed content type.
Then check how to create an Activity in the Connections documentation wiki (yes - confusing). In this article you find the code to retrieve an activity - I actually recommend to use CURL to get the valid format as an example. Some CURL URLs are here. The closest you get to a full example is Luis' demo of a status update.
To explore connections I use the following batch file:
set server=[server]
set HOME=c:\work
curl %server%%1 –-netrc -G --basic -k -v -L -o %2 %3 %4 %5 %6 %7
with an .netrc file (see the CURL documentation)
machine [server] login [user] password [password]
This is the XML format you need for an activity:
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="activity" label="Activity"/>
<title type="text">Posted activity</title>
<content type="html">
This is an activity that has been automatically uploaded from the cURL command line
</content>
</entry>
And post it like this:
post activities/service/atom2/activities newactivity.xml activityresult.xml
Open the activityresult.xml and locate ocate the href attribute of the app:collection element - you need it to add actions. Use the following XML:
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="todo"/>
<category term="Connection4.0"/>
<category term="Test"/>
<title type="text">Some things that need to be done</title>
<content type="html">
This is an <b>action</b> in an activity that has been automatically uploaded from the cURL command line.
</content>
<snx:assignedto>noreply#ibm.com</snx:assignedto>
</entry>
and this command:
post [the-url-you-found-above] newaction.xml actionresult.xml
Once the CURL version works you can try yourself using Abdera code.
Here is a working sample from REST client in Firefox:
https://vhost1279.site1.compute.ihost.com/activities/service/atom2/activities
Header: Content-Type application/atom+xml
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:snx="http://www.ibm.com/xmlns/prod/sn" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="activity" label="Activity" />
<content type="html"/>
<title type="text">
test
</title>
</entry>
The problem with the code above is that you pass in a String to the post method. That however doesn't set the right content type. Please use APIs Stephan suggests to create a org.w3c.dom.Node with the XML and pass this in instead. This will set automatically the right content type in the header.
SOLVED!!! I looked to source and problem was obvious. This is rather a bug or at least misconception but can be easily resolved . According the docs and my testing proves this Connections requires the following header in request: Content-Type = application/atom+xml .... but in source code of Social Enabler I found these two related methods:
protected void prepareRequest(HttpClient httpClient, HttpRequestBase httpRequestBase, Options options) throws ClientServicesException {
// TODO: add support for gzip content
//httpClient.addRequestHeader("Accept-Encoding", "gzip");
if(options.getHeaders()!=null) {
addHeaders(httpClient, httpRequestBase, options);
}
if (options.content != null) {
String contentType = null;
HttpEntity entity = null;
Object content = options.content;
try {
//If a subclass overrides com.ibm.xsp.extlib.services.client.Service.processRequestContent(HttpRequestBase, Object, Options)
//the the subclass must set the content type of the request, and also set the request's entity!
if(processRequestContent(httpClient, httpRequestBase, options)){
if (content instanceof IValue) {
JsonFactory jsFactory = new JsonJavaScriptFactory(DesignerRuntime.getJSContext());
entity = new StringEntity(JsonGenerator.toJson(jsFactory, content, true));
contentType = "application/json";
}
else if (content instanceof JsonObject) {
JsonFactory jsFactory = JsonJavaFactory.instanceEx;
entity = new StringEntity(JsonGenerator.toJson(jsFactory, content, true));
contentType = "application/json";
}
else if (content instanceof Node) {
entity = new StringEntity(DOMUtil.getXMLString((Node) content, true));
contentType = "application/xml";
}
else {
entity = new StringEntity(content.toString());
contentType = findRequestTextContentType(options);
}
}
} catch (Exception ex) {
if(ex instanceof ClientServicesException) {
throw (ClientServicesException)ex;
}
throw new ClientServicesException(ex, "Error while parsing request content");
}
if (entity != null && (httpRequestBase instanceof HttpEntityEnclosingRequestBase)) {
httpRequestBase.setHeader("Content-type", contentType);
((HttpEntityEnclosingRequestBase) httpRequestBase).setEntity(entity);
}
}
}
protected String findRequestTextContentType(Options options) {
return "text/plain";
}
As you can see there is no such header (application/atom+xml) for any case. But if you provide XML content as string, the code uses the 'findRequestTextContentType' method to return a default content type, which is 'text/plain' that is not correct for our situation. It is hardcoded so there is no way how to setup the default encoding. But, at least, the 'findRequestTextContentType' is type protected so it can be overriden. So I have created my own ConnectionsService class that extends the former one and overrides the findRequestTextContentType method to return correct content type for my case. And this works fine and resolved the problem !!
import sbt.ConnectionsService;
public class ConnectionsServiceCustom extends ConnectionsService {
public ConnectionsServiceTcl(String serviceUrl) {
super(serviceUrl);
// TODO Auto-generated constructor stub
}
#Override
protected String findRequestTextContentType(Options options) {
return "application/atom+xml";
}
}
As Niklas and Stephen point out you need to use a Dom object (Node, Document etc).. If you are getting an error when creating such an object then it is most likely because the contents of the document/node is poorly formatted or incorrect..
There is a built in XPages util class that allows you to create documents from strings
com.ibm.commons.xml.DOMUtil
Check out
com.ibm.commons.xml.DOMUtil.createDocument(String, String)
e.g.
com.ibm.commons.xml.DOMUtil.createDocument("my xml string", null);
The first parameter is the contents of the XML document, the second is the format.
This class provides several utility methods for parsing and constructing DOM documents.

Resources