I want to add a new child node to an XML request I'm proxying through to WSO2 DSS using an API.
The request I receive is in the following format:
<contacts>
<firstName>Bob</firstName>
<lastName>Brown</lastName>
</contacts>
I need to add an "id" node to this request that I can retrieve from a URI variable.
<contacts>
<id>1</id>
<firstName>Bob</firstName>
<lastName>Brown</firstName>
</contacts>
I've tried using the "enrich" mediator, but this seems to just wrap the ID node around the first name and last name nodes.
So I've resorted to using a script mediator to modify the request. This is what I'd like to do:
//Get XML Request from message context
var request= mc.getPayloadXML();
//Create a child node using standard E4X notation
var child = <id>1</id>;
//Append this XML to the request
request.appendChild(child);
//Replace the payload
mc.setPayloadXML(request);
Every time I try to submit this code, the WSO2 ESB API UI says that everything is cool. But when I check the underlying XML configuration, it is not cool. Where I defined "id" node is now just blank, as if it was filtered without my knowledge!
It would be great if I could add this element using a script mediator, but I'm open to other solutions.
This was actually a pretty easy fix. Just use a CDATA section to have the XML parser ignore your code.
<![CDATA[ //YOUR CODE WITH XML INLINE// ]]>
Note that for some reason the WSO2 ESB XML Editor and UI will remove the CDATA section the next time you open your sequence for editing. I generally just copy out the XML configuration to a text file and paste it back into the configuration whenever it needs to be updated.
Not a perfect solution, but it'll get you over the line.
Related
I tried to create proxy which will receive soap request and store it to rabbitmq.
Example from documentation works well, but it always stores full message with envelope and body elements.
How can I store pure xml message without envelope?
I used payload mediator, but got same result.
When you are storing, it will store with envelope as it must have a proper message. Even though you try to remove that, it may cause an error if message doesn't have an envelope.
You can try sending binary format (which will attach xml file in to message: Try this with POSTMAN tool) and it will send the xml file separate one. But still its' better not to remove envelope as it may cause errors.
I want to use XSLT mediator in my custom sequence to transform message for an API. I have create XSLT file name "transform.xslt" and use following syntax in my sequences.
<xslt key="transform.xslt" source="*" />
My problem is, I don't know where to put the XSLT file. Do I have to put under synapse directory or have to import to carbon repository, or is there any other configuration ?
From WSO2ESB it seems there have to be some configuration in API definition but in case of WSO2 API Manager it is automatically generated and I don't want to edit the generated file.
Thank you very much.
You can upload XSLT file as a registry resource and refer that in the XSLT mediator.
1) Save XSLT file in local registry
- Log on to the APIM Management Console.
- Click on "Browse" in the left "Registry" menu
- Expand the "system" tree node and then click on the "config"
- Then click on the "Add Resource" option
- Upload the "transform.xslt" file and give the name as "transform"
- Click on the "Add" button.
You can refer uploaded xslt file in your XSLT mediator like below.
<xslt key="conf:/transform.xslt" source="*" />
I have to deal with javascript content in API resource in WSO2 ESB. Specifically, I want to get the md5 hash from message which it is a javascript.
When I use messageType = application/javascript I get a binary, no JS message.
Can I do this? If I use binary to get md5, Will it work?
Finally, I solved this question as given below:
At first, I changed in axis2.xml the messageBuldir and messageFormater property to "text/javascript" pointing to "org.apache.axis2.format.PlainTextBuilder" and "org.apache.axis2.format.PlainTextFormatter".
At second I got the message in API with next expression: "s11:Body/child::[fn:position()=1] | s12:Body/child::[fn:position()=1]". With that, I can save the js message in a property.
Finally, I wrote a class mediator to calculate MD5 of property with JS.
I have saved an xml file in Configuration registry /_system/config/test.xml. My xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<ServiceDefinition>
<Source1001>Endpoint1</Source1001>
<Source1002>Endpoint2</Source1002>
</ServiceDefinition>
Now can i create a proxy to update the contents of the above xml file by use of sequence or class mediator? If i pass new endpoint= Endpoint3 and Node = Source1001 in the request of the proxy. then for the node Source1001 i should be able to see the changed endpoint as Endpoint3
Looking forward to your answers. Thanks in advance.
No built-in mediator can do this. You can write a class mediator to read the xml placed in registry, then save it after editing. You have to get the reference to registry service via osgi at the class mediator for that.
FYI. you can get the input from message context and find the values to replace.
This [1] post explains about creating class mediator to read from registry resource. Hope this will be helpful during your task.
[1] http://vvratha.blogspot.com/2013/02/accessing-registry-resources-from-class.html
You can use the following code segment within a class mediator or you can do the same with script mediator as well.
mc.getConfiguration().getRegistry().updateResource(
resourcePath, mc.getProperty("myProperty").toString().getBytes());
You can find more details on how to use either one of those mediators here
I could implement my .Net web service API method in a way so it will set the new field to a value if a user is using the newer wsdl, and will set the field nil if the user is using the older wsdl.
Even then, is there a risk that the new field in the response could break some existing client app using an older wsdl. I know a client app could break if it gets a new field in the response that's set to a value (which the web service won't do), but could it break if it gets a new field in the response that's nil?
Yes, it can break the clients.
Most webservices or clients validate the request or response before processing it. This is mostly done with an XML schema and the new field is not in the schema of your old clients;
you will get errors like The element X in namespace Y has invalid child element Z.
This is not a problem if the field is declared with minOccurs="0" in the new schema. This means an optional element which, if the value is null, simply does not appear in the XML instance.
Nillable elements, even when their value is null, will always be included in the XML instance but marked with nil="true" which indicates that they are indeed null.
Here is some extra info: Web services tip: Representations of null in XML Schema.