Saturday, April 5, 2014

Parlay Rest SMS with Apache Camel

Following sample will use http4 Apache component to send Sms using Parlay Rest Specification

package com.test.rest.sms;

import org.apache.camel.main.Main;

public class TestRestSms {

public static void main(String[] args) throws Exception {
            Main main = new Main();
            main.enableHangupSupport();
            main.addRouteBuilder(new TestRestSmsRoute());
            System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
            main.run();
}
}

Following is RouterClass

import org.apache.camel.builder.RouteBuilder;

public class TestRestSmsRoute extends RouteBuilder {

@Override
public void configure() throws Exception {

from("timer://foo?delay=0&period=20000")
.process(new SendSmsRequest())
.to("http4://172.21.7.1:5001/rest/sms/messages")
.process(new SendSmsResponse())
}

}

Following are processor classes

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.Processor;

public class SendSmsRequest implements Processor  {

@Override
public void process(Exchange exchange) throws Exception {

exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE,"application/json");
   
String toAddress = "\"addresses\": [\"tel:6666666666\"]";
String fromAddress = "\"senderName\": \"tel:321\"";
String msgText = "\"message\": \"hello world\"";
        String sms                 = "{"+toAddress+","+fromAddress+","+msgText+"}";

        exchange.getIn().setBody(sms);

} //

}

public class SendSmsResponse implements Processor  {

@Override
public void process(Exchange exchange) throws Exception {

Object result = exchange.getIn().getBody();

System.out.println("sms response="+result);

}

}

How to Send SMS with Parlay Rest

OMA provide specification for communication with telecom nodes, one of them is parlay rest to send sms.

Following is java sample to send sms following Parlay Rest specification

package com.test.rest.sms;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class RestSms {

Private URL sendSmsUrl = new URL("http://192.168.0.1:8080/rest/sms/messages");

public void sendSms() {

try {
HttpURLConnection connection =   (HttpURLConnection)sendSmsUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
        connection.connect();
//For simplicity we write JSON String format, we can use any library for this

String toAddress = "\"addresses\": [\"tel:666666666\"]";
String fromAddress = "\"senderName\": \"tel:321\"";
String msg Text = "\"message\": \"hello world\"";
String sms                 = "{"+toAddress+","+fromAddress+","+msgText+"}";

OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write( sms );
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
    while ((line = rd.readLine()) != null) {
        // Process line... and output response code
    }
    wr.close();
    rd.close();
                        
    connection.disconnect();
                        
        } catch (Exception e) {
e.printStackTrace();
        }//EO try-catch
   }

}