Advanced Services - SMS Connectivity

JAVA Sample application using SMPP v3.3 and Noctor SMS JDK

HSL Mobile Messaging

The following sample application written in Java sends an SMS message to a single mobile telephone. The application uses the Noctor SMS JDK to communicate with HSL's systems using SMPP v3.3 and is run from the command line. The Noctor SMS JDK is available at http://www.noctor.com/.

package com.haysystems.examples;

import java.net.*;
import java.io.*;
import java.security.*;

/* Import the Noctor SMS JDK. */
import sms.*;

/**
 * A simple example class that sends some text via the HSL SMPP gateway.
 *
 * @see Noctor documentation.
 */
public class NoctorExample implements BindResponse, SendResponse {
    public static void main(String[] args) {
        /* To receive the callbacks from the Noctor JDK, we must have an
           instance of this class. So, we make the instance and essentially
           hand off to it. */
        NoctorExample instance = new NoctorExample();
        instance.instanceMain(args);
    }

    public void instanceMain(String[] args) {
        // Storage for the data that will be extracted from the command-line.
        String systemIdentifier = null;
        String systemType = null;
        String password = null;
        String text = null;
        String number = null;
        String ton = null;
        String npi = null;

        // Process command-line parameters.
        for(int i = 0; i < args.length; i++) {
            if(args[i].startsWith("system_id="))
                systemIdentifier = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("system_type="))
                systemType = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("password="))
                password = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("text="))
                text = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("number="))
                number = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("ton="))
                ton = args[i].substring(args[i].indexOf("=") + 1);
            else if(args[i].startsWith("npi="))
                npi = args[i].substring(args[i].indexOf("=") + 1);
            else {
                printUsage();
                return;
            }
        }

        // Check that we've actually got all of the parameters.
        if(systemIdentifier == null || systemType == null || password == null
            || text == null || number == null || ton == null || npi == null)
        {
            printUsage();
            return;
        }

        try {
            /* Note: The response to the bind is via a callback. The same is
               true of the send operation. These callbacks are implemented by
               the bindResponse() and sendResponse() methods respectively. */

            // Bind into the HSL server
            Smpp transmit = new Smpp("xxx.xxx.xxx.xxx", "xxx");
            Binding binding = new Binding(systemIdentifier, systemType, password);
            transmit.bind(this, binding);

            // Send the SMS
            Address address = new Address(number,
                                          Integer.parseInt(ton),
                                          Integer.parseInt(npi));
            Message message = new Message(address, text);
            transmit.send(this, message);

            transmit.close();
        } catch(SMSException smse) {
            System.err.println("NoctorExample: SMSException; " + smse.getMessage());
        } catch(IOException ioe) {
            System.err.println("NoctorExample: IOException; " + ioe.getMessage());
        } catch(utils.socksException se) {
            System.err.println("NocterExample: socksException; " + se.getMessage());
        }
    }

    /**
     * Callback interface for the result of a bind operation.
     *
     * @param e Any exception signalled by the Noctor JDK.
     * @param binding The binding object that was used in the original
     * bind operation.
     * @see sms.BindResponse
     */
    public void bindResponse(Exception e, Binding binding) {
        if(e != null) {
            System.err.println("NoctorExample: Bind exception; " + e.getMessage());
            System.exit(1);
        }
    }

    /**
     * Callback interface for the result of a send operation.
     *
     * @param e Any exception signalled by the Noctor JDK.
     * @param message The message object that was used in the original
     * send operation.
     * @see sms.BindResponse
     */
    public void sendResponse(Exception e, Message message) {
        if(e != null) {
            System.err.println("NoctorExample: Send exception; " + e.getMessage());
            System.exit(1);
        }
    }

    private static void printUsage() {
        System.err.println("com.haysystems.examples.NoctorExample: usage...");
        System.err.println("  client ID, text and gsm number required in format \"key=value\"");
        System.err.println("  e.g. system_id=foo");
        System.err.println("  valid keys are: system_id, system_type, password, text, number, ton and npi");
        System.err.println("  note: key ordering is unimportant");
    }
}

Copyright © Hay Systems Ltd 2001-2008

Owner: support@haysystems.com 18 September 2001