Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.belio.service.gateway; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import java.math.BigInteger; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Queue; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPFault; import javax.xml.soap.SOAPMessage; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import org.apache.commons.lang3.tuple.Pair; import org.belio.Launcher; import org.belio.domain.tix.Outbox; import org.belio.model.QueueType; /** * * @author jacktone */ public class SafcomGateway { private Properties spproperties; private Properties networkproperties; // public String mtEndpoint = "http://172.31.181.254/MQSDP/sendSms/notify.php"; // public String bulkEndpoint = "http://197.232.18.245/BULKMQSDP/sendSms/notify.php"; // static String url = "http://196.201.216.13:8310/SendSmsService/services/SendSms"; private Map<String, Queue<Outbox>> queMap = new HashMap<>(); public SafcomGateway() { try { this.spproperties = Launcher.fetchConfigurations("spid.properties"); this.networkproperties = Launcher.fetchConfigurations("networks.properties"); } catch (IOException ex) { Logger.getLogger(SafcomGateway.class.getName()).log(Level.SEVERE, null, ex); } // populateSpMap(); } public boolean sendSafaricomMessage(QueueType queueType, Outbox outbox) { return sendMessage(queueType, outbox); } private boolean sendMessage(QueueType queueType, Outbox outbox) { try { String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); MessageDigest md = MessageDigest.getInstance("MD5"); String serviceId = outbox.getServiceID(); String endpointDef = ""; // if (queueType.equals(QueueType.BULK)) { // endpointDef = networkproperties.getProperty("safcom_mt_endpoint"); // } else { endpointDef = networkproperties.getProperty("safcom_endpoint"); // } String code = outbox.getShortCode(); String spIdString = outbox.getSdpId(); String spPasswordString = createSpPass(spIdString, now, md); String recepient = "tel:" + outbox.getMsisdn(); String actualmessage = URLDecoder.decode(URLEncoder.encode(outbox.getText(), "UTF-8"), "UTF-8"); Launcher.LOG.info("T----------------------------------------J" + actualmessage); String gencorrelator = String.valueOf(outbox.getRefNo()); // Create SOAP Connection SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // Send SOAP Message to SOAP Server SOAPMessage messageToSend = getSoapMessageFromString(getMessage(spIdString, spPasswordString, recepient, serviceId, now, actualmessage, code, gencorrelator, endpointDef)); Calendar start = Calendar.getInstance(); SOAPMessage soapResponse = soapConnection.call(messageToSend, networkproperties.getProperty("safcom_sms")); Launcher.LOG.info( recepient + " - took " + (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis())); // SOAPMessage soapResponse = null; System.out.println("XXXXXXXXXXXXXXXXXXX====Sending Safaricom message"); // printSOAPResponse(soapResponse);//TODO log SOAPBody body = soapResponse.getSOAPBody(); if (body.hasFault()) { SOAPFault newFault = body.getFault(); // QName fcode = newFault.getFaultCodeAsQName(); // String string = newFault.getFaultString(); // String actor = newFault.getFaultActor(); // System.out.println(">>>>>>>>>>>>>"+fcode); System.out.println(">>>>>>>>>>>>>" + newFault.getFaultString()); soapConnection.close(); return false; } else { //TO DO log soapConnection.close(); return true; } } catch (Exception ex) { Launcher.LOG.info(ex.getMessage()); } return false; } private String createSpPass(String spIdString, String now, MessageDigest md) { String allpass = spIdString + (spproperties.getProperty(spIdString).split(",")[0]) + now; Launcher.LOG.info(spIdString); md.update(allpass.getBytes()); byte[] digest = md.digest(); BigInteger bigInt = new BigInteger(1, digest); String spPasswordString = bigInt.toString(16); while (spPasswordString.length() < 32) { spPasswordString = "0" + spPasswordString; } Launcher.LOG.info(spPasswordString); //String recepient = "tel:254724170138"; return spPasswordString; } private static String getMessage(String spId, String spPassword, String address, String serviceId, String timeStamp, String actualmessage, String senderName, String correlator, String endpoint) { String message = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + "xmlns:v2=\"http://www.huawei.com.cn/schema/common/v2_1\"\n" + "xmlns:loc=\"http://www.csapi.org/schema/parlayx/sms/send/v2_2/local\">\n" + "<soapenv:Header>" + "<v2:RequestSOAPHeader>" + "<spId>" + spId + "</spId>" + "<spPassword>" + spPassword + "</spPassword>" + "<serviceId>" + serviceId + "</serviceId>" + "<timeStamp>" + timeStamp + "</timeStamp>" + "<v2:OA>" + address + "</v2:OA>" + "<v2:FA>" + address + "</v2:FA>" + "</v2:RequestSOAPHeader>" + "</soapenv:Header>" + "<soapenv:Body>" + "<loc:sendSms>" + "<loc:addresses>" + address + "</loc:addresses>" + "<loc:senderName>" + senderName + "</loc:senderName>" + "<loc:message>" + actualmessage + "</loc:message>" + "<loc:receiptRequest>" + "<endpoint>" + endpoint + "</endpoint>" + "<interfaceName>SmsNotification</interfaceName>" + "<correlator>" + correlator + "</correlator>" + "</loc:receiptRequest>" + "</loc:sendSms>" + "</soapenv:Body>" + "</soapenv:Envelope>"; // Launcher.LOG.info(message); return message; } private static SOAPMessage getSoapMessageFromString(String xml) throws SOAPException, IOException { MessageFactory factory = MessageFactory.newInstance(); javax.xml.soap.SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")))); return message; } /** * Method used to print the SOAP Response */ private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); Source sourceContent = soapResponse.getSOAPPart().getContent(); // System.out.print("\nResponse SOAP Message = "); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(sourceContent, result); String xmlString = result.getWriter().toString(); Launcher.LOG.info(xmlString); } // private static class InstanceHolder { // // private static final SafcomGateway GATEWAY = new SafcomGateway(); // } // // public static SafcomGateway getInstance() { // return InstanceHolder.GATEWAY; // } }