Friday, October 2, 2015

MMS with Apache Camel

Following is the sample code to send MultiMedia message to MMSc over MM7 protocol

public class MmsRouter extends RouteBuilder {

     public void configure() {
        from("timer:foo?period=5m")
        .process(    new MmsPreProcessor()      )
        .to("http://x.x.x.x/vasp/servlet/messagerouter");
    }

}

public class MmsPreProcessor implements Processor {

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

              MessageFactory factory = MessageFactory.newInstance();
              SOAPMessage soapMeassage = factory.createMessage();

              SOAPEnvelope envelope = soapMeassage.getSOAPPart().getEnvelope();
           
              SOAPHeader header = envelope.getHeader();
              SOAPBody body = envelope.getBody();

              SOAPFactory soapFactory = SOAPFactory.newInstance();

              //Header
              Name headerName = soapFactory.createName("TransactionID", "mm7", "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4");

              SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
              headerElement.setTextContent("TID.1234567");
              headerElement.setMustUnderstand(true);
           
              //Soap message body
              Name submitReqName = soapFactory.createName("SubmitReq");
              SOAPBodyElement submitReq = body.addBodyElement( submitReqName );
         
              SOAPElement MM7Version =  submitReq.addChildElement(  soapFactory.createName("MM7Version") );
              MM7Version.setTextContent("6.8.0");

              //SO senderIdentification
              SOAPElement senderIdentification =  submitReq.addChildElement( soapFactory.createName("SenderIdentification") );
           
              SOAPElement vaspID =  senderIdentification.addChildElement( soapFactory.createName("VASPID") );
              vaspID.setTextContent("vaspid");
              SOAPElement vasID =  senderIdentification.addChildElement( soapFactory.createName("VASID") );
              vasID.setTextContent("1234");

              SOAPElement senderAddress =  senderIdentification.addChildElement( soapFactory.createName("SenderAddress") );
              SOAPElement shortCode =  senderAddress.addChildElement( soapFactory.createName("Number") );
              shortCode.setTextContent("9394");

              SOAPElement recipients =  submitReq.addChildElement( soapFactory.createName("Recipients") );
              SOAPElement to =  recipients.addChildElement( soapFactory.createName("To") );
              SOAPElement number =  to.addChildElement( soapFactory.createName("Number") );
              number.setTextContent("+9xxxxxxxxx");

              SOAPElement messageClass =  submitReq.addChildElement( soapFactory.createName("MessageClass") );
              messageClass.setTextContent("Auto");

              SOAPElement subject =  submitReq.addChildElement( soapFactory.createName("Subject") );
              subject.setTextContent("Hello World");
           
              Name contentName = soapFactory.createName("Content");
              SOAPElement contentElement =  submitReq.addChildElement( contentName );
              contentElement.addAttribute(envelope.createName("href"), "cid:mm7-content");
              contentElement.addAttribute(envelope.createName("allowAdaptations"), "false");
           
              DataHandler dataHandler = new DataHandler(  new URL("file:///abc.jpg")   );
              AttachmentPart imgAttachment = soapMeassage.createAttachmentPart( dataHandler );
              imgAttachment.setContentId("mms-content-img");

              soapMeassage.addAttachmentPart( imgAttachment );

              exchange.getOut().setHeader(Exchange.HTTP_METHOD,"POST");

 Iterator it = soapMeassage.getMimeHeaders().getAllHeaders();
                 
              while (it.hasNext()) {
                   MimeHeader mimeHeader = (MimeHeader) it.next();
                   exchange.getOut().setHeader(mimeHeader.getName(),mimeHeader.getValue());
              }
                       
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              soapMeassage.writeTo(baos);

              exchange.getOut().setBody( baos );
           
       }

}

Any suggetions to improve above code are welcome.

No comments: