How to create malformed XML (SOAP request) in nodejs - node.js

I have been given a task to generate malformed SOAP Request and checking for what kind of error response I receive.
For example:
const payload = `
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test="urn:TEST"
>
<soap:Body>
<test:GetOperatingDataValues
xmlns:test="urn:TEST"
>
<test:ListId>1</test:ListId>
</test:GetOperatingDataValues>
</soap:Body>
</soap:Envelope>`
Now, if I send the above request body to the backend server like :
const result = axios.post("backendServer", payload)
console.log(result) will give the response from backend (in this case, response from GetOperatingDataValues function for listId 1)
Now, I have to modify that payload in different ways like, removing the end tag, different opening and closing tag names, deleting '<' from any tag, or removing '/' from end tags, different method name, etc and send that payload and check for the error response.
Is there any way to modify the correctly formed xml to malformed xml (in node js).
I have gone through different packages such as xml2js, fast-xml-parser. But these packages just form the correct xml.
Any help would be highly appreciated.

I would have thought it fairly obvious that to create a non-XML file, you don't want to use an XML tool. Just treat the XML as a character string and apply a regular expression, for example replacing </ by <.

Related

Node.js My XML is not reading my XSLT

My Node.js follows this format.
if(req.method =="POST"){ refineXML(req, result =>{
res.writeHead(200, {'Context-Type': 'text/xml'});
res.write(result);
res.end();
});
};
The refineXML code is a simple method that takes in an encoded xml string, decodes it, removes the id, and then sends it back to the user. When I console.log the result, the answer is in promper xml format. However, I can't figure out how to link it to my XSL document. My XSL document currently lies under my project->WebContent->XSL
My Console keeps printing out
TypeError: Cannot read property 'Symbol(asyncId)' of null
whatever that means while my firefox says
Error Loading stylesheet:The TLS handshake finished for ...
http://localhost:3000/my.xsl
What does the stylesheet declaration look like in your xml document?
should look something like:
<?xml-stylesheet href="my.xsl"?>
the href should be a relative path so if your xml is at my project->WebContent then the PI would be:
<?xml-stylesheet href="./XSL/my.xsl"?>
technically the leading ./ isn't needed but I find it aids clarity

ServiceResponse Result is always null (JAXB, KIE Workbench & KIE Server 6.5.0)

I have a project setup in KIE Workbench, version 6.5.0 and a KIE execution server 6.5.0 running, all on the same local Wildfly 10. It is working fine.
I have a simple rule that is being executed in a stateless KieSession, no process or whatsoever. I expect the rule to validate basic information and to add information about the fired validations to a global variable. In the example I am testing I just modify a global variable to see if it works, but for some reason I never get a result from the service, which I expect to have at least something.
This is the JAXB request I send:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch-execution lookup="kieSessionStateless">
<insert disconnected="false" entry-point="DEFAULT" return-object="false" out-identifier="myorganization.myproject.bom.MyObject">
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="myObject">
<myDescription>description</myDescription>
<myID>0</myID>
</object>
</insert>
<set-global out-identifier="globalList" identifier="globalList">
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaxbListWrapper">
<type>LIST</type>
</object>
</set-global>
<get-objects out-identifier="globalList"/>
<fire-all-rules max="-1"/>
</batch-execution>
The rule that is executed is quite simple:
rule "MY FIRST RULE"
when
myorganization.myproject.bom.MyObject( myID == 0 )
then
myorganization.myproject.bom.GlobalResponses globalResponses = new myorganization.myproject.bom.GlobalResponses();
globalResponses.setRuleName("MY FIRST RULE");
globalResponses.setRuleResponse("MY ID IS 0");
globalList.add(globalResponses);
System.out.println("MY ID IS 0");
end
And in the Wildfly console I see the line printed without any additional information (like a stacktrace of an error), so I conclude all is working well:
13:04:36,315 INFO [stdout] (default task-39) MY ID IS 0
But I always get the same response: it indicates that the "process" has terminated correctly (so it seems at first sight) and therefore I am expecting that Result has something (an empty xml response, an xml response with some data in it, ...):
System.out.println("Message: "+response.getMsg());
System.out.println("Result: "+response.getResult());
System.out.println("Type: "+response.getType());
Message: Container KieContainer successfully called.
Result: null
Type: SUCCESS
Response: ServiceResponse[SUCCESS, msg='Container KieContainer successfully called.']
The client is called with KieServicesClient:
KieServicesConfiguration config = KieServicesFactory.
newRestConfiguration(urlKieServer,
name,
password);
config.setMarshallingFormat(MarshallingFormat.JAXB);
KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
JAXBContext jaxbContext = DroolsJaxbHelperProviderImpl.createDroolsJaxbContext(classNames, null);
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter xml = new StringWriter();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(command, System.out);
marshaller.marshal(command, xml);
ServiceResponse<String> response = client.executeCommands("instances/"+containerName, xml.toString());
I am missing something but I really do not know what.
All the related projects are on GitHub. Since I cannot post more than two links, I will add the direct links in a comment.
This question is also on:
Drools User group
I was able to complete the life cycle and send a valid request and obtain a valid answer. Part of my problem was the code used to construct the XML. The best way to construct a valid JAXB request is by using the API provided by KIE, an example can be found
here.
Anyway interested in establishing connection to a KIE Server 6.5.0 with a generic client project can consult the code on my GitHub.

XML Parsing Error: no element found Location: http://localhost:8081/web-app/pages/login.xhtml Line Number 1, Column 1: ^

My login.xhtml starts with:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I am using Java 8, JSF, Primefaces, Maven, Tomcat8.
I think that something is wrong with my configuration e.g. servlets and web.xml.
XML Parsing Error: no element found Location: [...] Line Number 1, Column 1
To the point, this exact error means that the webbrowser retrieved an entirely empty response while it is being instructed to interpret the response as XML, usually via the Content-Type header such as application/xhtml+xml or application/xml, or if absent, via the file extension in URL such as .xhtml or .xml.
Given that this happens while requesting a JSF login page, this strongly suggests that it contained a syntax/runtime error and that the webapp doesn't have error handling correctly configured. Apparently error pages themselves are also blocked behind a login/security constraint, causing the server being unable to present a "normal" error page with all exception detail and therefore return a completely empty response.
Your best bet is reading the server logs for the actual exception and/or running a debugger and/or creating a JSF exception handler which explicitly logs exceptions to server log (just in case you couldn't find exceptions and thus they appear to be swallowed). Once having the actual exception at hands, it's usually a matter of using the exception type + message + 1st line as keywords to find clues on the Internet.
XML Parsing Error: no root element found
The ERROR occurs when your client send a request like below:
curl -X GET --header 'Accept: application/json' 'http://192.168.1.249:9001/students/
you can see in header like 'Accept: application/json' this means the client only accept the server response in JSON format.
But Server produces a response type in XML content type and it will try to parse the XML to JSON. During this phase exception triggers.
SOLUTION :
Please change the response content type to application/json or
Change the request header to 'Accept: application/xml'.
Thanks.

Error page fails with XML Parsing Error: no element found Location: http://localhost:8080/3c/login/Login.xhtml Line Number 1, Column 1 [duplicate]

My login.xhtml starts with:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I am using Java 8, JSF, Primefaces, Maven, Tomcat8.
I think that something is wrong with my configuration e.g. servlets and web.xml.
XML Parsing Error: no element found Location: [...] Line Number 1, Column 1
To the point, this exact error means that the webbrowser retrieved an entirely empty response while it is being instructed to interpret the response as XML, usually via the Content-Type header such as application/xhtml+xml or application/xml, or if absent, via the file extension in URL such as .xhtml or .xml.
Given that this happens while requesting a JSF login page, this strongly suggests that it contained a syntax/runtime error and that the webapp doesn't have error handling correctly configured. Apparently error pages themselves are also blocked behind a login/security constraint, causing the server being unable to present a "normal" error page with all exception detail and therefore return a completely empty response.
Your best bet is reading the server logs for the actual exception and/or running a debugger and/or creating a JSF exception handler which explicitly logs exceptions to server log (just in case you couldn't find exceptions and thus they appear to be swallowed). Once having the actual exception at hands, it's usually a matter of using the exception type + message + 1st line as keywords to find clues on the Internet.
XML Parsing Error: no root element found
The ERROR occurs when your client send a request like below:
curl -X GET --header 'Accept: application/json' 'http://192.168.1.249:9001/students/
you can see in header like 'Accept: application/json' this means the client only accept the server response in JSON format.
But Server produces a response type in XML content type and it will try to parse the XML to JSON. During this phase exception triggers.
SOLUTION :
Please change the response content type to application/json or
Change the request header to 'Accept: application/xml'.
Thanks.

ActiveSync Sync message not supporting all properties

I'm implementing an ActiveSync Java client. I can already communicate with the server and perform the protocol sequence to Sync emails, as defined in the documentation.
However, when I try and send a Sync command it seems I can only use a subset of the available commands in the specification. When I use certain property tags in the Sync XML message I receive a Status 4 ('Protocol Error') code, even though these should be valid according to the specification.
When I use only the GetChanges property I do get a proper response, however I'd like to be able to specify how many items to return etc.
I've included the XML snippet that I'm sending: the commented out lines are lines that were attempted but caused a Status 4.
<?xml version="1.0" ?>
<Sync xmlns="AirSync:">
<Collections>
<Collection>
<Class>Email</Class>
<SyncKey>{23423972324}</SyncKey>
<CollectionId>{23423sdfsdfsdfsf972324}</CollectionId>
<GetChanges/>
<!--<GetChanges>0</GetChanges>-->
<!--<WindowSize>512</WindowSize>-->
<!--<Options>-->
<!--<Class>Email</Class>-->
<!--<FilterType>3</FilterType>-->
<!--<MaxItems>10</MaxItems>-->
<!--</Options>-->
<!--<Commands>-->
<!-- <Fetch>-->
<!-- <ServerId>1:323</ServerId>-->
<!-- </Fetch>-->
<!--</Commands>-->
</Collection>
</Collections>
<!--<WindowSize>512</WindowSize>-->
</Sync>
Is there any reason why these properties shouldn't be supported? Or is there something I've left out of the message?
The GetOptions command for the server returns the following (truncated):
Server: Microsoft-IIS/6.0,
X-Powered-By: ASP.NET,
Pragma: no-cache,
Public: OPTIONS, POST, Allow: OPTIONS, POST,
MS-Server-ActiveSync: 6.5.7653.19,
MS-ASProtocolVersions: 1.0,2.0,2.1,2.5,
MS-ASProtocolCommands:Sync,SendMail,SmartForward,SmartReply,GetAttachment,GetHierarchy,CreateCollection,DeleteCollection,MoveCollection,FolderSync,FolderCreate,FolderDelete,FolderUpdate,MoveItems,GetItemEstimate,MeetingResponse,ResolveRecipients,ValidateCert,Provision,Search,Notify,Ping
It turns out that the WBXML encode/decoder I was using wasn't working correctly. I figured this out by comparing the byte code generated by two different WBXML encoders and saw a difference. The encoder I was using didn't process empty elements like properly. I fixed it and now the server acts as expected.

Resources