package com.neolane;
import java.net.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class MyNeolaneAPIJavaApp {
// Session token for authentication (login/password)
static String strSessionToken = "api2/api2";
// URL of Neolane server
static String strUrl = "http://sandbox2.us.neolane.net/nl/jsp/soaprouter.jsp";
// Build SOAP envelope message
static String strQueryDefHeader =
"" +
"" +
""+
""+
""+strSessionToken+""+
"";
static String strQueryDefServiceBody =
" " +
" " +
" " +
" " +
" "+
" "+
" " +
" " +
" " +
" " +
"";
static String strQueryDefRecipientBody =
" " +
" " +
" " +
" " +
" "+
" "+
" "
+ " "
+ " "
+ " "
+ " "
+ "";
static String strQueryDefFooter =
"" +
""+
""+
"";
static String strWriteHeader =
"" +
""+
""+
""+
""+
""+strSessionToken+""+
"";
static String strWriteFooter =
"" +
""+
""+
"";
public static void main(String s[])
{
// Example One: Write to nms:recipient with insertOrUpdate with hierarchical XML objects
WriteRecipientXMLObject(strSessionToken,strUrl);
// Example Two: Write to nms:recipient with insertOrUpdate using a string converted to XML
WriteRecipientXMLString(strSessionToken,strUrl);
// Example Three: Query nms:service table in Neolane with a string converted to XML
QueryService(strSessionToken,strUrl);
// Example Four: Query nms:recipient for specific email and folder using XML hierarchical objects
QueryRecipientXMLObject(strSessionToken,strUrl);
// Example Five: Query nms:recipient for specific email and folder based on an XML string
QueryRecipientXMLString(strSessionToken,strUrl);
}
// Example One: Write to nms:recipient with insertOrUpdate with hierarchical XML objects
public static void WriteRecipientXMLObject(String strSessionToken, String strUrl)
{
// TBD
}
// Example Two: Write to nms:recipient with insertOrUpdate using a string converted to XML
public static void WriteRecipientXMLString(String strSessionToken, String strUrl)
{
try {
URL url = new URL(strUrl);
String strSoapAction = "xtk:persist#Write";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("connection = "+connection.toString());
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("SOAPAction", strSoapAction);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
// be sure to insert trailing space at end of each line
String strWriteRecipientBody =
" "
+ " "
+ " "
+ " ";
String strRequest = strWriteHeader + strWriteRecipientBody + strWriteFooter;
PrintStream p = new PrintStream(connection.getOutputStream());
p.println(strRequest);
int iResponseCode = connection.getResponseCode();
if( iResponseCode == HttpURLConnection.HTTP_OK )
{
// Parse XML result document
InputStream inXml = connection.getInputStream();
System.out.println("XML="+inXml.toString());
System.out.println("Insert completed");
}
else
{
throw new Exception("Execution Error, response code="+iResponseCode);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
// ExampleThree: Query nms:service table in Neolane with a string converted to XML
public static void QueryService(String strSessionToken, String strUrl)
{
try {
URL url = new URL(strUrl);
String strSoapAction = "xtk:queryDef#ExecuteQuery";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("connection = "+connection.toString());
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("SOAPAction", strSoapAction);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
String strRequest = strQueryDefHeader + strQueryDefServiceBody + strQueryDefFooter;
PrintStream p = new PrintStream(connection.getOutputStream());
p.println(strRequest);
int iResponseCode = connection.getResponseCode();
if( iResponseCode == HttpURLConnection.HTTP_OK )
{
// Parse XML result document
InputStream inXml = connection.getInputStream();
System.out.println("XML="+inXml.toString());
/*
// Write the inputStream to a FileOutputStream
OutputStream outputStream = new FileOutputStream(new File("C:\\temp\\output.xml"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inXml.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
*/
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (inXml);
// normalize text representation
doc.getDocumentElement().normalize();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList objects = doc.getElementsByTagName("service");
int count = objects.getLength();
System.out.println("Count of objects: " + count);
int objectLength = objects.getLength();
for(int i=0; i