Search code examples
wso2esbproxy-servergoogle-bookswso2-esb

WSO2 Proxy service generates a wrong response


I am building a proxy service using WSO2 enterprise service bus. This service accepts and XML

<GetBookInfo>
    <isbn>someibnnumberhere</isbn>
</GetBookInfo>

Here's the XML file or actual proxy service

Code for the parameters is👇

<inSequence>
<log level="full" description="Log SOAP request"/> 
<property xmlns:ns="http://waa.swin.edu.au" 
     name="uri.var.isbn" 
     xpression="//ns:GetBookInfo/isbn" 
     scope="default"
     type="STRING"/>
 <property name="messageType" 
     value="application/json"
     scope="axis2"
     type="STRING"
     description="Transform message format to JSON"/>

Code for sending the request is 👇

<send>
    <endpoint name="GeoLocationProxyService">
        <http method="GET" uri-template="https://www.googleapis.com/books/v1/volumes?q=isbn:{uri.var.isbn}&amp;key=AIzaSyBBJis6L2DK2PROIy2SsFBdgTJohR7GuFg&amp;fields=items(volumeInfo/title,volumeInfo/authors,volumeInfo/publisher,volumeInfo/publishedDate,volumeInfo/industryIdentifiers,saleInfo/country,saleInfo/saleability,volumeInfo/averageRating)" />
    </endpoint>
</send>
</inSequence>

Code for sending and receiving the response is👇

<outSequence>
     <log description="Log returned JSON payload">
        <property name="JSON‐Payload" expression="json-eval($.)"/>
     </log>
     <payloadFactory media-type="xml" description="Generate response data">
        <format>
           <GetGeoLocationResponse xmlns="http://waa.swin.edu.au">
              <title>$1</title>
              <authors>$2</authors>
              <publisher>$3</publisher>
              <publishedDate>$4</publishedDate>
              <type>$5</type>
              <identifier>$6</identifier>
              <country>$7</country>
              <saleability>$8</saleability>
           </GetGeoLocationResponse>
        </format>
        <args>
           <arg evaluator="json" expression="$.items[0].volumeInfo.title"/>
           <arg evaluator="json" expression="$.items[0].volumeInfo.authors"/> 
           <arg evaluator="json" expression="$.items[0].volumeInfo.publisher"/> 
           <arg evaluator="json" expression="$.items[0].volumeInfo.publishedDate"/> 
           <arg evaluator="json" expression="$.items[0].volumeInfo.industryIdentifiers[1].type"/> 
           <arg evaluator="json" expression="$.items[0].volumeInfo.industryIdentifiers[1].identifier"/> 
           <arg evaluator="json" expression="$.items[0].saleInfo.country"/> 
           <arg evaluator="json" expression="$.items[0].saleInfo.saleability"/> 
        </args>
     </payloadFactory>
     <property name="messageType"
        value="application/xml"
        scope="axis2"
        type="STRING"
        description="Transform message format to SOAP"/>
     <send/>
  </outSequence>

The reponse this service creates is all some random numbers but of same type. Here's what I'm talking about 🤷‍♂️

<GetGeoLocationResponse xmlns="http://waa.swin.edu.au">
   <title>ISBN Review</title>
   <authors>
      <jsonElement>International ISBN Agency</jsonElement>
   </authors>
   <publisher/>
   <publishedDate>1998</publishedDate>
   <type/>
   <identifier/>
   <country>AU</country>
   <saleability>NOT_FOR_SALE</saleability>
</GetGeoLocationResponse>

All of the above👆 missing fields are results of multiple 👇items[i] where i: <=10 (i think) returned containing diffirent rubbish/garbage value. So, yea, either fields are missing or they are wrong🤷‍♂️

If I request the same service via a browser/postman 👇

{
items: [
    {
        volumeInfo: {
            title: "If At First You Don't Conceive",
            authors: [
                "Liz Ellis"
            ],
            publisher: "Macmillan Publishers Aus.",
            publishedDate: "2018-04-24",
            industryIdentifiers: [
                {
                    type: "ISBN_13",
                    identifier: "9781760780364"
                },
                {
                    type: "ISBN_10",
                    identifier: "1760780367"
                }
            ]
        },
        saleInfo: {
            country: "AU",
            saleability: "FOR_SALE"
        }
    }
    ]
}

Solution

  • You can use REST_URL_POSTFIX property, of which the value is appended at the end of the endpoint URL.

    <property name="REST_URL_POSTFIX" expression="fn:concat('?q=isbn:',$ctx:isbn_no,'&amp;key=',$ctx:key)" scope="axis2"/>
    

    Then define the endpoint without query params.

    <send>
        <endpoint name="GeoLocationProxyService">
            <http method="GET" uri-template="https://www.googleapis.com/books/v1/volumes" />
        </endpoint>
    </send>