Find herewith a list of examples useful to manage Java code
Testing the Connection to the service
URL serviceUrl = new URL("https://servername/ws/wsapi?wsdl");
MagNewsAPIService service = new MagNewsAPIService(serviceUrl);
MagNewsAPI port = service.getMagNewsAPIPort();
java.lang.String serviceVersion = port.getVersion();
System.out.println("Connected to MagNews WSAPI v. "+serviceVersion);
Testing the research for a contact in the database
URL serviceUrl = new URL("https://servername/ws/wsapi?wsdl");
String password="My_Access_Token";
Credentials c = new Credentials();
c.setPassword(password);
MagNewsAPIService service = new MagNewsAPIService(serviceUrl);
MagNewsAPI port = service.getMagNewsAPIPort();
String idDataBase = "1"; // this is the default database
String primaryKey = "name@domain.com";
MnContact contact = port.findContactByPrimaryKey(idDataBase, primaryKey, c);
Testing for a contact subscription to the database
URL serviceUrl = new URL("https://servername/ws/wsapi?wsdl");
String password="My_Access_Token";
Credentials c = new Credentials();
c.setPassword(password);
MagNewsAPIService service = new MagNewsAPIService(serviceUrl);
MagNewsAPI port = service.getMagNewsAPIPort();
String idDataBase = "1"; // this is the default database
String primaryKey = "name@domain.com";
List<MnContactValue> values = new ArrayList<MnContactValue>();
List<Option> opt = new ArrayList<Option>();
MnContactValue emailValue = new MnContactValue();
emailValue.setField("EMAIL");
emailValue.setValue("name@domain.com");
values.add(emailValue);
MnContactOperation subscribeResult = port.subscribeContact(idDataBase, values, opt, c);
if (subscribeResult.isOk()) {
System.out.println("Subscription OK");
} else {
System.out.println("Subscription Failed");
}
Testing a communication creation
URL serviceUrl = new URL("https://servername/ws/wsapi?wsdl");
String password="My_Access_Token";
Credentials c = new Credentials();
c.setPassword(password);
MagNewsAPIService service = new MagNewsAPIService(serviceUrl);
MagNewsAPI port = service.getMagNewsAPIPort();
Content content = new Content();
content.setName("newsletterName");
//Test from HTML Content
content.setContentType("text/html");
content.setText("myBodyExample"); //html
//Test from URL Content
content.setContentType("text/url");
content.setText("http://www.google.it"); //url
List advanceOptiondelivery = new ArrayList();
String createNewsletter = port.createNewsletter("myIdCampaign", content, advanceOptiondelivery, credentials);
System.out.println("idnewsletter: " + createNewsletter);
Add contact info to a rowSet from file and insert contact to database
String idDb = "1";
//Connection to the service
URL serviceUrl = new URL("https://ws-mn1.mag-news.it/ws/wsapi?wsdl");
MagNewsAPIService service = new MagNewsAPIService(serviceUrl);
MagNewsAPI port = service.getMagNewsAPIPort();
java.lang.String serviceVersion = port.getVersion();
System.out.println("Connected to MagNews WSAPI v. " + serviceVersion);
//Set credentials
String password = "My_Access_Token"; //see OAuth 2 section.
Credentials c = new Credentials();
c.setPassword(password);
//create rowset
String idRowset = port.createContactRowSet(c);
//create static audience with autoexpire option
Option ogroup1 = new Option();
ogroup1.setKey("autoexpire");
ogroup1.setValue("true");
Option ogroup2 = new Option();
ogroup2.setKey("expiredate");
ogroup2.setValue("20/05/2017 10:00");
List optionslist = new ArrayList<>();
optionslist.add(ogroup1);
optionslist.add(ogroup2);
String idgroup = port.createSimpleStaticGroup(idDb, "My_Static_Group_Name", optionslist, c);
//read contacts from file
Path path = Paths.get("myfile.csv");
byte[] file = Files.readAllBytes(path);
//options for file
Option o = new Option();
o.setKey("field_separator");
o.setValue(";");
FileOptions fileOpt = new FileOptions();
//column mapping
fileOpt.getColumnMapping().add("NOME");
fileOpt.getColumnMapping().add("COGNOME");
fileOpt.getColumnMapping().add("EMAIL");
fileOpt.getOptions().add(o);
port.addDataToContactsRowsSetFromFile(idRowset, file, fileOpt, null, c);
//start asyncronous batch operation adding contacts to a static audience
MnBatchStartOptions batchOptions = new MnBatchStartOptions();
batchOptions.setName("operationName");
batchOptions.setIdDatabase(idDb);
batchOptions.setAddContactsToSimpleStaticGroup(true);
batchOptions.setIdGroup(idgroup);
String idbatch = port.startBatchContactsUpdate(idRowset, batchOptions, null, c);
MnBatchStatusInfo statusInfo = port.getBatchStatusInfo(idbatch, c);
System.out.println(statusInfo.getStatus());
</>