1

I receive from an rabbitmq inbound an XML message and I have to send it to mongodb with restheart. I must modify the content of the Json generate by the formatter before sending it to the mongo rest api, because I must modify a datetime format send in string to add "ISOdate ()" for mongodb.

My incomming message can be different from other messages.

For exemple, I have this from inbound :

<CONTENT><CODE_USER>100</CODE_USER><DATE>2017-12-12</DATE></CONTENT>

and I get this for the endpoint,

{"CONTENT" : {"CODE_USER":100","DATE":"2017-12-12"}}

So I want to modify the Json format before sending to the endpoint to modify the date string.

Is there a way to trigger the esb Json formatter and get the result before send it to the endpoint ?

Thanks, Nicolas

Community
  • 1
  • 1
Nicolas
  • 117
  • 1
  • 10
  • Why do you want to trigger the formatter and then alter the date string. It would probably be easier to modify the date and then send the message out as JSON – ophychius Nov 05 '18 at 11:49
  • Yes, You're right but I have to send to mongodb a date like this : { "DATE" : ISODate("2017-12-12")}, so that why I need to have Json and then modify it before sending to mongodb – Nicolas Nov 05 '18 at 14:53
  • I dont think that would be valid JSON though. Check http://json.org/ – ophychius Nov 05 '18 at 15:21
  • 1
    dates on RESTHeart have the following format {"$date": epoch time_millis }. See https://restheart.org/learn/representation-format/ – Andrea Di Cesare Nov 06 '18 at 08:34

2 Answers2

0

I find an solution,

I use json-eval($.*.) function and an script mediator to modify the date format before sending to the endpoint, like this :

 <property expression="json-eval($.*.)" name="location" scope="default" type="STRING"/>

<script language="js"><![CDATA[var message =  new String(mc.getProperty('location'));
  var reg = /\"((\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{1,3})Z)\"/g;
  var mess=message.replace (reg,'{\"$date\" : \"\$1\"}');
  mess=mess.substring (1,mess.length-1);
  mc.setPayloadJSON(mess);]]></script>

<property name="ContentType" scope="axis2" type="STRING" value="application/json"/>
Nicolas
  • 117
  • 1
  • 10
-1

Yes that is possible. You can add a mediator to the in-sequence such that the transformation happens before releasing the response to mongo-db.

Please check the available mediators and if there is no any suitable one for your requirement, you can simply write your own mediator by extending org.apache.synapse.mediators.AbstractMediator

For the details on how to write a custom mediator, you can refer the blog: https://medium.com/@nirothipanram/esb-few-tips-in-writing-a-custom-class-mediator-b9a322f4eaa8

PasinduJay
  • 487
  • 5
  • 17