2

We have a proxy service which uses jms transport to receive messages. The messages received need to be sent to a backend REST service using http POST. The following is done on the messages

  1. xslt transformation to extract specific fields
  2. set message type to application/json
  3. send to the endpoint

The REST service endpoint needs to have a path parameter appended dynamically using one of the values that comes as part of the input message from jms. The url will look like http://<server-ip>/service/<client>. Here the value for the "client" comes as part of the message.

How can we dynamically add the path param using wso2 esb?

bhaskar
  • 43
  • 1
  • 7

2 Answers2

2

I think links [1] & [2] will help you to set jms with WSO2 proxy... To dynamically add path param to the url use the link [3], it is for XML configuration file. similar to this you can assign the part of the message to a property add append that to the url...

[1] http://docs.wso2.org/wiki/display/ESB460/Publish-Subscribe+%28Pub-Sub%29+with+JMS

[2] http://wso2.org/library/articles/2011/11/wso2-esb-example-two-wayrequestresponse-semantic-jms

[3] How to dynamically route message in WSO2 ESB based on XML configuration file

Thanks,

Mohan

Community
  • 1
  • 1
Mohanadarshan
  • 830
  • 5
  • 5
  • Thanks for the quick response. For adding a path parameter to the endpoint url, lets say i define a property for the url and append the part of the request to the url and use it. Will the ESB do the protocol switching from jms to http? Also, i will not be able to error handle the endpoint configuration using timeout, suspension settings. Any ways to use an address endpoint from registry and append some text to it dynamically? – bhaskar Apr 29 '13 at 09:55
  • see below links.. [1] http://docs.wso2.org/wiki/display/ESB460/Sample+253%3A+Bridging+from+JMS+to+HTTP+and+Replying+with+a+202+Accepted+Response [2] http://docs.wso2.org/wiki/pages/viewpage.action?pageId=16846499 – Mohanadarshan Apr 29 '13 at 10:50
  • And refer this also... http://docs.wso2.org/wiki/display/ESB451/Sample+10%3A+Introduction+to+Dynamic+Endpoints+with+the+Registry I thing WSO2 ESB documentation have answers for your issues... Thanks... – Mohanadarshan Apr 29 '13 at 10:55
  • I should have been more clear. I know that WSO2 ESB does transport switching from jms to http and vice versa quite well. But my question is if i don't provide an endpoint in the send mediator and just use a property value (with dynamic endpoint) to set the 'To' header, then would the ESB be able to do the transport switching. As i see from the link [1](http://stackoverflow.com/questions/8815994/dynamic-endpoint-in-wso2) the transport switching is not done by just setting the 'To' header. – bhaskar Apr 29 '13 at 11:56
  • 1
    Also the dynamic endpoints using templates would not solve my problem as there is only set value and no set expression while creating the endpoint from the template. [link](http://stackoverflow.com/questions/15876410/wso2-esb-dynamically-change-endpoint-address). It is not about choosing one from the list of endpoints from the registry. It is about appending some text dynamically to one endpoint from registry. – bhaskar Apr 29 '13 at 11:57
2

I believe what you are looking for is the REST_URL_POSTFIX property. If you set this property, the value will be appended to the rest endpoint url.

It can be defined as follows with the scope of axis2.

<property name="REST_URL_POSTFIX"
          expression="//client"
          scope="axis2"
          type="STRING"/>

An example on this can be found in this guide, Using REST with a Proxy Service.

EDIT: Following is example using a simple proxy with a POST request using curl. Providing as per the comments. Here, I'm invoking the jaxrs_basic rest service in WSO2 Application Server.

curl -H "Content-Type: application/xml" -H "Accept: application/json" -d "<Customer><name>KasunG</name></Customer>" http://localhost:8281/services/new1/

.

curl -H "Content-Type: application/json" -H "Accept: application/json" -d "{ 'Customer' : { 'name' : 'KasunG' } }  " http://localhost:8281/services/new1/

.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="new1"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <inSequence>
         <property name="REST_URL_POSTFIX"
                   value="customers"
                   scope="axis2"
                   type="STRING"/>
         <property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
         <switch source="$axis2:HTTP_METHOD">
            <case regex="GET">
               <property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
            </case>
            <case regex="POST">
               <property name="messageType" value="application/json" scope="axis2"/>
               <property name="ContentType"
                         value="application/JSON"
                         scope="axis2"
                         type="STRING"/>
               <property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
            </case>
            <default/>
         </switch>
         <send>
            <endpoint>
               <address uri="http://localhost:8888/jaxrs_basic/services/customers/customerservice"
                        format="rest"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="messageType" value="application/json" scope="axis2"/>
         <send/>
      </outSequence>
   </target>
</proxy>
Kasun Gajasinghe
  • 2,735
  • 1
  • 21
  • 30
  • I tried this. But it works well with Http GET. But the Rest service that i am calling is Http POST. The property REST_URL_POSTFIX does not seem to work for Http POST. – bhaskar Apr 29 '13 at 12:05
  • @bhaskar , I have verified that the said property works. I tried this with a simple http proxy, but I don't see how it could be different for jms. Make sure the expression you have set it correct. You can verify which endpoint url it invoked in the end using tcpmon. WSO2 ESB ships tcpmon by default. – Kasun Gajasinghe Apr 29 '13 at 12:32
  • I again tried with a simple http proxy itself. But still not able to make it work. This is the configuration that i am using. The value of id is not null as it prints in the log. – bhaskar Apr 29 '13 at 15:54
  • it works for HTTP GET but does not work for HTTP POST. Anything that i am missing? – bhaskar Apr 29 '13 at 16:04
  • good question @bhaskar. Is it possible to parameterize endpoint with properties? Like this: ` ` Seems there is no way. – surlac Apr 29 '13 at 16:53
  • @bhaskar I have added a sample proxy and sample curl POST requests. May be you missed the 'format' attribute in the endpoint. Please re-check your proxy config. – Kasun Gajasinghe Apr 29 '13 at 18:30
  • @Kasun thanks. The configuration that you have given worked. But one observation is it works only if the endpoint is defined inline with the send mediator. It does not work if the endpoint is defined separately and referenced using a key (though the endpoint definition given separately contains rest format). – bhaskar Apr 30 '13 at 09:46
  • 1
    @surlac You can do this modifying the `To` header. ex. `
    `. Another option is to use template endpoints. You can find more info on that here - http://docs.wso2.org/wiki/display/ESB460/Endpoint+Template
    – Kasun Gajasinghe Apr 30 '13 at 12:19
  • @bhaskar No, it does work. I just tried and verified with esb 4.6.0. I believe you should be more descriptive on your comments. May be point to a pastebin which have the proxies/endpoints you tried. – Kasun Gajasinghe Apr 30 '13 at 12:25
  • I have verified it many times. It does not work. The configuration is exactly the same as you have used except that i defined an endpoint in a separte file and stored in endpoints directory. And used the following send mediator in my sequence (instead of specifying the endpoit inline). . I am using ESB 4.0.3. I cannot use a newer version as we already have 4.0.3 in our production server and we are going to use the same server for the new proxy service. – bhaskar Apr 30 '13 at 12:35
  • I tried this on ESB 403 just for you. This worked for me there as well though I had to remove the `messageType` property in outSequence. It should work with that too, but I didn't have time to investigate. My endpoint looked like the following which was references by the proxy via a key. `
    `
    – Kasun Gajasinghe Apr 30 '13 at 14:01
  • Thanks Kasun. It actually works. My endpoint configuration was wrong. I had given the format attribute in the endpoint element instead of the address element by mistake like below. ``. After putting it in the right place it worked. – bhaskar May 02 '13 at 09:23
  • Good to know. You could have used the UI to configure the format attribute. – Kasun Gajasinghe May 02 '13 at 10:12