MT SMS (Java)

Space Search
Searching Messaging API
Table of Contents

This example demonstrates how to send an MT SMS using the API, and it uses the POST method to pass content in UTF-8 format. Please remember to substiture your own username and password into the code when testing.

package fi.roottori.example;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Send an SMS message using the Roottori Messaging API.
 *
 * @author alindh
 */
public class SMSExample {

	public static final String API_USERNAME = "xxx";
	public static final String API_PASSWORD = "yyy";
	public static final String API_URI = "http://gw1.roottori.fi/eapi/push";

	protected Log log = LogFactory.getLog(this.getClass());

	public static void main(String[] args) {

		SMSExample sms = new SMSExample();

		String recipient = "358405134265";
		String originator = "Roottori";
		String message = "Message API test 1, 2, 3, å, ä, ö, €";

		try {
			sms.sendSMS(recipient, originator, message);
		} catch (Exception e) {
			System.out.println("Failed: " + e);
		}

	}

	/**
	 * Send given message to given recipient
	 */
	protected boolean sendSMS(String recipient, String originator, String message) throws Exception {

		// build request URI
		StringBuffer uri = new StringBuffer(API_URI);
		uri.append('?');
		uri.append("l=");
		uri.append(URLEncoder.encode(API_USERNAME, "ISO-8859-1"));

		uri.append("&p=");
		uri.append(URLEncoder.encode(API_PASSWORD, "ISO-8859-1"));

		uri.append("&msisdn=");
		uri.append(URLEncoder.encode(recipient, "ISO-8859-1"));

		log.debug("Submitting SMS (request uri: '" + uri.toString() + "')");

		byte[] content = message.getBytes("UTF-8");
		String contentType = "text/plain; charset=UTF-8";

		PostMethod method = doHttpPostRequest(uri.toString(), content, contentType);
		if (method.getStatusCode() >= 200 && method.getStatusCode() < 300)
			return true;

		return false;
	}

	/**
	 * Do an HTTP POST reuqest to given URL. The content is places in the request body, and the "Content-Type"
	 * header of the request is set according to given contentType parameter.
	 */
	protected PostMethod doHttpPostRequest(String url, byte[] content, String contentType) throws Exception {
		PostMethod method = null;
		try {
			HttpClient http = new HttpClient();
			HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
	    	    public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
	    	    	return false;
	    	    }
	    	};
	    	method = new PostMethod(url);
	    	method.setRequestHeader("Content-Type", contentType);
	    	method.setRequestEntity(new ByteArrayRequestEntity(content));
	    	http.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);

	    	http.executeMethod(method);
		} catch (Exception e) {
			log.error("doHttpPostRequest: " + e);
			return null;
		}

		return method;
	}

}

The example has the following dependencies:

Labels

 
(None)