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);

}

}

No comments: