Skip to main content

Version: 23.10

Remote Service Calls

Journey Manager Groovy services are often used to call external REST or SOAP Web services to perform various integrations. Typical examples of calling remote services include:

  • dynamic data services
  • form prefill
  • delivery processes

Fluent HTTP

To call external services we recommend using the HTTP library. This library is extremely easy to use, automatically cleans up resources, and provides good protection against most failure modes which can occur when calling remote services.

Fluent HTTP provides a wrapper around the powerful Apache HttpComponents library. While Apache HTTP Components is incredibly flexible, it can be difficult to use and requires developers to write defensive code to handle the various remoting failure modes.

By default, the Fluent HTTP library automatically configures the following settings:

  • 10-second timeout for connection establishment
  • 60-second timeout for socket read
  • 8 MB maximum response read size
  • JVM proxy connection settings if defined

REST GET Example

The example below uses a GetRequest object to perform a REST HTTP GET request and returns a Path object. The Path object is used to access values from the JSON response content; in this example, a nested contact email address.

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.http.*
import com.avoka.tm.vo.*
import javax.servlet.http.*

class RestGetService {

String invoke(SvcDef svcDef, Txn txn, HttpServletRequest request, User user) {

Map parameters = [:]
parameters.id = request.getParameter('customerId')
parameters.account = request.getParameter('accountId')

String username = svcDef.paramsMap.username
String password = svcDef.paramsMap.password

// execute GET request and return a HttpResponse object
HttpResponse response = new GetRequest('https://service.mycorp.com/secure/rest/accounts/')
.setParams(parameters)
.setBasicAuth(username, password)
.execute()

// ensure response is OK
if (!response.isStatusOK()) {
throw new RuntimeException(response.statusLine)
}

// get Path object from the response
Path path = response.getPathContent()

return path.val("contact.email")
}
}

REST POST Example

The example below uses a PostRequest object to perform REST HTTP POST request and returns a JSON text object.

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.http.*
import com.avoka.tm.util.*
import com.avoka.tm.vo.*
import javax.servlet.http.*

class RestPostService {

String invoke(SvcDef svcDef, Txn txn, HttpServletRequest request, User user) {

String message = '''{
"cutomerId": 82881,
"name": "John Doe",
"email": "[email protected]",
"age": 42,
"married": true
}'''

String username = svcDef.paramsMap.username
String password = svcDef.paramsMap.password

// execute POST request and return a HttpResponse object
HttpResponse response = new PostRequest('https://service.mycorp.com/secure/rest/accounts/')
.setMessage(message)
.setBasicAuth(username, password)
.execute()

// ensure response is OK
if (!response.isStatusOK()) {
throw new RuntimeException(response.statusLine)
}

// get JSON object from the response
String json = response.getTextContent()

return json
}
}

SOAP POST Example

The example below uses the PostRequest object to perform a SOAP Web Service HTTP POST request, and returns an XML Document object. Note the request content type being set to 'text/xml'.

import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.http.*
import com.avoka.tm.vo.*
import javax.servlet.http.*
import org.w3c.dom.*

class SOAPService {

String invoke(SvcDef svcDef, Txn txn, HttpServletRequest request, User user) {

String message = '''
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<m:transaction-continue xmlns:m="java:com.verid.carbon.integration.datatypes">
<settings>
<account-name>$accountName</account-name>
<mode>$mode</mode>
<ruleset>$ruleset</ruleset>
<transaction-id>$transID</transaction-id>
</settings>
</m:transaction-continue>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

String username = svcDef.paramsMap.username
String password = svcDef.paramsMap.password

// execute POST request and return a HttpResponse object
HttpResponse response = new PostRequest('https://service.mycorp.com/secure/rest/accounts/')
.setContentType('text/xml')
.setMessage(message)
.setBasicAuth(username, password)
.execute()

// ensure response is OK
if (!response.isStatusOK()) {
throw new RuntimeException(response.statusLine)
}

// get Document XML object from the response
Document xml = response.getDocumentContent()

return xml
}
}