Sample client

Unica provides sample transactional message clients to guide application developers that create client applications that make web service calls to the Deliver Transactional Messaging Service (TMS). This sample client is based on the second-generation WSDL.

The major difference between this WSDL and the earlier version is that this version supports using attachments with transactional message and provides authentication credentials as parameters.

The following program is an example that demonstrates how to structure a request to the Deliver Transactional Mailing Service. Review this example as an illustration of how to work with the application programming interface for the Deliver TMS.

This example is based on libraries for axis2 1.3. For details about axis2 1.3, see the following website: http://ws.apache.org/axis2/ .


public class SampleTestClient {
	public static void main(String[] args) throws AxisFault, RemoteException {
		/**
         * The sendMailing method of the TMS webservice requires:
         * 1) proper authentication information - a valid username and password recognized by the TMS 
         * 2) a mailingCode to identify the Mailing that contains the document to be sent
         * 3) an audience identifier (used primarily for tracking)
         * 4) personalized fields that will be merged into the document to be sent
         * 5) optional cellCode(s) associated to the audience identifier. 
         * 6) optional additionalOptions - there are currently no additional options supported, 
         *      but is here for future use.  For now this parameter can be left as null.
         * 7) optional locale for the response messages to be returned otherwise default is "en" (Locale.US) for english
		 */

		// authentication information
		String userName = "MyTMSUserName";
		String password = "MyTMSPassword";

		// mailing code
        String mailingCode = "mailing 123";

        // audience id: note, an audience id is comprised of at least one name value pair.
		// a custom type called NameValuePair needs to be constructed.
		NameValuePair[] audienceId = new NameValuePair[1];

		NameValuePair nvp = new NameValuePair();
		nvp.setName("CustomerID");
		nvp.setValueDataType("numeric");
        nvp.setValueAsNumeric(2021);

		audienceId[0] = nvp;

        // personalized fields: each personalized field is a name value pair, so again we use the
        // custom type "NameValuePair".  For this example, we want to send two personalized fields (emailAddress, gender)
		NameValuePair[] personalizedFields = new NameValuePair[2];

		NameValuePair nvp1 = new NameValuePair();
        nvp1.setName("emailAddress");
        nvp1.setValueDataType("string");
        nvp1.setValueAsString("johndoe@foobar.com");

		personalizedFields[0] = nvp1;

		NameValuePair nvp2 = new NameValuePair();
        nvp2.setName("gender");
        nvp2.setValueDataType("string");
        nvp2.setValueAsString("male");

		personalizedFields[1] = nvp2;

		// Cell code
		String[] cellCodes = { "CC243935" };

		// Load the attachment data from the file system using a data source
		FileDataSource logo = new FileDataSource(new File("C:\\logo.png"));
		DataHandler handler = new DataHandler(logo);
		Base64Binary attachmentBinary = new Base64Binary();
		attachmentBinary.setBase64Binary(handler);
		ContentType_type0 actualContentType = new ContentType_type0();
		actualContentType.setContentType_type0(handler.getContentType());
		// specify the content type for the attachment
		attachmentBinary.setContentType(actualContentType);

		// Add the attachment
		Attachment attachment = new Attachment();
		attachment.setFileName("First Attachment");
		attachment.setLabel("Attachment");
		attachment.setFileContent(attachmentBinary);

		// Configure attachments
		Attachment[] attachments = new Attachment[] {attachment};

		// Additional Options - this is a name value pair again - but for now
		// send as null
		NameValuePair[] additionalOptions = null;

		NameValuePair[] trackingFields = null;

		// locale - rely on default by setting as null;
		String locale = null;

		/**
         * Calling the Method:
         * 1) set up a connection object with the URL of the TMS webservice
         * 2) Construct the required security header with the authentication credentials
         * 3) Construct the method and Set the parameters
         * 4) Make the call
         * 5) Process the response
		 */

		// connection object
		TMSStub stub = new TMSStub("http://<Replace IP of Deliver TMS Service>:<PORT>/delivertms/services/TMS");

		ServiceClient serviceClient = stub._getServiceClient();
		serviceClient.getOptions().setProperty(HTTPConstants.SO_TIMEOUT, new Integer(60 * 1000));
		serviceClient.getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT, new Integer(60 * 1000));

		// authentication: the TMS web service requires the client to submit
		// user and pw info via soap headers.
		// the following code sets up the authentication credentials that are
		// passed in via the headers.
		UserName un = new UserName();
		un.setUserName(userName);

		Password pwd = new Password();
		pwd.setPassword(password);

		// make the call
		Response response = stub.sendMailing(mailingCode, audienceId, personalizedFields, cellCodes, additionalOptions,
				attachments, trackingFields, locale, un, pwd);

		// process the response - a customType Response is returned
		// all responses come back with a top level code that indicates whether
		// or not the request was
		// successful (0) or a warning (1) or error (2) occurred. If the request
		// was not successful, the client code
		// should log/alert the issue, and possibly retry the request depending
		// on the issue
		if (response.getStatusCode() == 0) {
			System.out.println("Request to TMS successful");
		} else // an error or warning occurred