External Web Service can't be registered in Management Console today when outbound HTTP requests to the Internet have to go through a proxy. This Transform is a workaround that enables REST/Json service calls through proxy. In that case the REST service doesn't require authentication. This Tranform will work even if you don't need proxy access OR if proxy authentication is not required.
import groovy.json.JsonSlurper;
// Class import for JSON parsing
import java.io.*;
String strurl = "https://api-adresse.data.gouv.fr/search/?q="+URLEncoder.encode(data['Address'])+"&limit=1";
// URL to call. limit=1 will limit candidates based on higher score
// Input variable is a single line address in France. Example : "105 rue anatole france 92300 levallois perret"
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "MyCompanyProxyName");
// Proxy server name
System.getProperties().put("proxyPort", " MyCompanyProxyPort ");
// Proxy server – Use Nestat – an to find out what port is used
Authenticator.setDefault(new MyAuthenticator());
def url = new URL(strurl);
def conn = url.openConnection();
def jsonObj = new JsonSlurper().parseText(url.text);
// Service call. JSON response in text format
// The JSON is parsed and Spectrum variables are instanciated
// Try this to understand data response structure https://api-adresse.data.gouv.fr/search/?q=105%20rue%20anatole%20france%2092300%20levallois%20perret
if (jsonObj.features[0] != null)
{
data['city'] = jsonObj.features[0].properties.city;
data['citycode'] = jsonObj.features[0].properties.citycode;
data['context'] = jsonObj.features[0].properties.context;
data['housenumber'] = jsonObj.features[0].properties.housenumber;
data['id'] = jsonObj.features[0].properties.id;
data['importance'] = jsonObj.features[0].properties.importance;
data['label'] = jsonObj.features[0].properties.label;
data['name'] = jsonObj.features[0].properties.name;
data['postcode'] = jsonObj.features[0].properties.postcode;
data['score'] = jsonObj.features[0].properties.score;
data['street'] = jsonObj.features[0].properties.street;
data['type'] = jsonObj.features[0].properties.type;
data['x'] = jsonObj.features[0].properties.x;
data['y'] = jsonObj.features[0].properties.y;
}
// Spatial coordinates provided in Lambert93
// Proxy authentication – Optional
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("domain\\username", "password".toCharArray()); /* Change Here */
}
}
------------------------------
Eric Hubert
PreSales Engineer
Pitney Bowes Software France
Levallois Perret
------------------------------