/* * Copyright (C) Responsfabrikken A/S. All Rights Reserved. * * [Description...] * @author kim@responsfabrikken.dk * @since x.y * @version $Revision: 1.2 $, $Date: 2007/11/21 08:12:47 $ * */ package rf.smsoutbox; import java.io.IOException; import java.io.InputStream; import java.util.GregorianCalendar; import java.util.StringTokenizer; import java.util.concurrent.atomic.AtomicLong; import net.tambur.wbxml.messages.ServiceIndicationMessage; import org.apache.log4j.Logger; import dk.responsfabrikken.lang.Sms; import dk.responsfabrikken.util.DateTimeKit; public class SonofonCode implements Code { private Logger log = null; private String user; private String replaceWithUID = "#-#replaceWithUID#-#"; private static AtomicLong counter = new AtomicLong(); public static final String authenticateStart = ""; public static final String authenticateEnd = ""; private static final String MPAYMENT_CHANNEL_1410 = "1410"; private static final String MPAYMENT_CHANNEL_1292 = "1292"; public SonofonCode(String name, String username) { user = username; log = Logger.getLogger("smsoutbox.rf.smsoutbox.SonofonCode."+name); } public byte[] get(int type) throws CodeException { if(log.isDebugEnabled()){ log.debug("Request message of type: "+type); } byte[] result = null; switch( type) { case Code.LOGIN_PACKET : result = (authenticateStart+user+authenticateEnd+"\r\n").getBytes(); break; case Code.ALIVE_REQUEST : result = (""+replaceWithUID+"\r\n").getBytes(); break; default: throw new CodeException("Unknown type: "+type); } return result; } public CodePacket decode(byte[] receivedPacket) throws CodeException { String tmp = new String(receivedPacket); if(tmp.startsWith(""); //index2 = tmp.indexOf("<",index); index2 = tmp.indexOf("",index); String txt = tmp.substring(index+1,index2); txt = txt.replaceAll("&","&"); txt = txt.replaceAll("<","<"); txt = txt.replaceAll(">",">"); txt = txt.replaceAll(""","\""); txt = txt.replaceAll("'","'"); txt = txt.replaceAll("
","\n"); txt = txt.replaceAll(""," "); txt = txt.replaceAll("æ","æ"); txt = txt.replaceAll("&Aelig;","Æ"); txt = txt.replaceAll("ø","ø"); txt = txt.replaceAll("Ø","Ø"); txt = txt.replaceAll("å","å"); txt = txt.replaceAll("Å","Å"); Message payload = new Message(System.currentTimeMillis(), mobile, channel, txt, "sonofon"); return new CodePacket(MESSAGE, receivedPacket, payload); }else if(tmp.startsWith("")){ return new CodePacket(ALIVE_REQUEST,receivedPacket); }else if(tmp.startsWith("")){ return new CodePacket(NO_ACTION,receivedPacket); }else if(tmp.startsWith("")){ if(tmp.contains("method name=\"Ping\"")){ return new CodePacket(ALIVE_REPLY,receivedPacket); }else if(tmp.contains("")){ int index = tmp.indexOf("")+7; int index2 = tmp.indexOf("true")){ status = true; } CodePacket cp = new CodePacket(MESSAGE_RECEIVED,receivedPacket, seqNo, status); cp.setSmscID(seqNo); return cp; } }else if(tmp.contains("")){ //login reply return new CodePacket(LOGIN_REPLY,receivedPacket,tmp.contains("true")); }else if(tmp.startsWith("")){ if(tmp.contains("DeliveryControlDELIVERYCONTROLtrue")){ status = true; } int index = tmp.indexOf("")+7; index2 = tmp.indexOf("").append(replaceWithUID).append(""); str.append("").append(message.getServiceKey()).append(""); str.append("").append(message.getPrefix()).append("").append(""); str.append("").append(""); //log.debug("JACOBBillingCode:"+message.getPrice()+";"); if(!message.getPrice().equals("0.00")){ if( message.getAppnr().equals(MPAYMENT_CHANNEL_1410) || message.getAppnr().equals(MPAYMENT_CHANNEL_1292) ) { str.append("").append(getBillingCode(message.getPrice(),message.getDonation(),"mpayment")).append(""); } else { str.append("").append(getBillingCode(message.getPrice(),message.getDonation(),message.getType())).append(""); } } if(message.getType().equals(Sms.TYPE_TEXT)){ String txt = xmlEncode(message.getMessage()); str.append("").append(txt).append(""); //check for length and use CONCTEXT if over 160 character, else use TEXT if(GSM7.length > 160){ str.append("").append("CONCTEXT").append(""); } else { str.append("").append("TEXT").append(""); } if(message.getPrice().equals("0.00") && message.getServiceFrom() != null && message.getServiceFrom().length() > 0 && !message.getServiceFrom().equals(message.getAppnr())){ str.append(""); } }else if(message.getType().equals(Sms.TYPE_WAPPUSH)){ String txt = xmlEncode(message.getMessage()); if(message.getPrice() == null || (message.getPrice().equals("0.00"))){ //setup different call for wap push StringBuffer strWapPush = new StringBuffer(/*authenticateStart+user+authenticateEnd*/); strWapPush.append(""); strWapPush.append(""); strWapPush.append(""); strWapPush.append("").append(replaceWithUID).append(""); strWapPush.append(""); strWapPush.append(""); strWapPush.append("").append(txt).append(""); strWapPush.append(""); strWapPush.append(""); //get wap push as bytes byte[] resultWapPush = strWapPush.toString().getBytes(); //return wap push return new CodePacket[]{new CodePacket(MESSAGE, resultWapPush)}; }else{ //Send raw message ServiceIndicationMessage si = new ServiceIndicationMessage(); si.setMessage(message.getMessage()); si.setHref(message.getWapurl()); byte[] b = null; try{ b = si.toByteArray(); }catch(Exception e){ throw new FatalCodeException("Unable to encode message",e); } String text = RealtimeGateway.toHexString(b); if(text.length() > 266){ int length = text.length()/254+(text.length()%254==0?0:1); str.append(""); for(int i = 0; i < length;i++){ //0B05040B8423F0 050003000"+cpA.length+"0"+(i+1) if(text.length() > 254){ str.append("0B05040B8423F00003000").append(length).append("0").append((i+1)).append(text.substring(0,254)).append(""); text = text.substring(254); }else{ str.append("0B05040B8423F00003000").append(length).append("0").append((i+1)).append(text).append(""); } } str.append("").append("").append("RAW").append(""); }else{ text = "0605040B8423F0"+text; str.append("").append(text).append(""); str.append("").append("RAW").append(""); } } if(message.getPrice().equals("0.00") && message.getServiceFrom() != null && message.getServiceFrom().length() > 0 && !message.getServiceFrom().equals(message.getAppnr())){ str.append(""); } }else if(message.getType().equals(Sms.TYPE_RAW)){ String txt = message.getMessage(); String header = null; StringTokenizer st = new StringTokenizer(txt, ":"); header = st.nextToken(); log.debug("Header: " + header); String data = st.nextToken(); log.debug("Data: " + data); str.append("").append(header+data).append(""); str.append("").append("RAW").append(""); if(message.getPrice().equals("0.00") && message.getServiceFrom() != null && message.getServiceFrom().length() > 0 && !message.getServiceFrom().equals(message.getAppnr())){ str.append(""); } }else{ log.error("Trying to send unsupported type: "+message.getType()+"["+message+"]"); throw new CodeException("Trying to send unsupported type: "+message.getType()+"["+message+"]"); } str.append(""); str.append(""); //log.debug("Message: " + str.toString()); byte[] result = str.toString().getBytes(); return new CodePacket[]{new CodePacket(MESSAGE, result)}; } public byte[] reply(CodePacket packet) throws CodeException { byte[] result = null; switch(packet.getType()){ case Code.ALIVE_REQUEST: //String seqNo = getTransNumber(packet.getOriginal()); /*if(seqNo == null){ log.error("Unable to get seqNo in packet: "+new String(packet.getOriginal())); return null; }*/ //String timestamp = getTimeStamp(); //result = (""+timestamp.toString()+"true"+seqNo+"").getBytes(); result = "".getBytes(); break; case Code.MESSAGE: //apperently no reply break; case Code.NOTIFICATION: result = "\n".getBytes(); break; case Code.NO_ACTION: log.debug("HACK: sending "); result = "".getBytes(); break; case Code.ALIVE_REPLY: log.debug("HACK: sending "); result = "".getBytes(); break; case Code.LOGIN_REPLY: case Code.MESSAGE_RECEIVED: break; default: throw new CodeException("Unknown type: "+packet.getType()); } if(log.isDebugEnabled()){ log.debug("Reply is: "+result); } return result; } public byte[] finalize(byte[] packet) throws CodeException { String tmp = new String(packet); String uid = getUniqueID(); return tmp.replace(replaceWithUID,uid).getBytes(); } public String getTransNumber(byte[] packetI) { String packet = new String(packetI); int indexS = packet.indexOf(""); int indexE = packet.indexOf("",">"); txt = txt.replaceAll("\"","""); txt = txt.replaceAll("'","'"); txt = txt.replaceAll("\n","
"); txt = txt.replaceAll(" ",""); /*txt = txt.replaceAll("Æ","&Aelig;");*/ txt = txt.replaceAll("ø","ø"); txt = txt.replaceAll("Ø","Ø"); txt = txt.replaceAll("å","å"); return txt.replaceAll("Å","Å"); } public byte[] read(InputStream in, SocketHandler sh) throws IOException{ StringBuffer sb = new StringBuffer(); int i; char c; StringBuffer temp = new StringBuffer(); do { i = in.read(); temp.append((char)i); if (i < 0) { log.debug("Stream closed before getting start byte - received: "+temp.toString()); sh.reportError(); return new byte[0]; } c = (char) i; //log.debug(""+c);//nice to have when debuging a connection } while (c != '<'); sb.append(c); c = sh.charRead(in); if(c == '?'){ sb.append(c); //start tag, we really ought to read the encoding somehow. boolean cont = true; c = sh.charRead(in); sb.append(c); while(cont){ if(c == '?'){ c = sh.charRead(in); sb.append(c); if(c == '>'){ log.debug("read starttag: "+sb.toString()+"...dropping"); return read(in,sh); } }else{ c = sh.charRead(in); sb.append(c); } } } //get tag while(c != '>' && c != ' '){ sb.append(c); c = sh.charRead(in); } String tag = sb.toString(); if(!tag.startsWith(".length()-1 int complete = 0; while(complete <= goal){ c = sh.charRead(in); sb.append(c); if((complete == 0 && c == '<') || (complete == 1 && c == '/') || (complete > 1 && complete < goal && c == tag.charAt(complete-2))){ complete++; }else if(complete == goal && c == '>'){ //All read break; }else{ complete = 0; } } } String result = sb.toString(); if(log.isDebugEnabled() && !tag.startsWith("