`

用Spring,smppapi,mina, commons ssl快速实现安全的smp(2)

    博客分类:
  • MINA
阅读更多

作者:lizongbo
版权声明:可以任意转载,转载时请务必以超链接形式标明文章和作者信息及本版权声明


接上一篇: http://618119.com/archives/2007/11/27/31.html

smpp 数据包进行编码和解码的相关代码如下:

ProtocolCodecFactory的代码为:

 

  1. package com.lizongbo.smpp.server.codec;   
  2. import org.apache.mina.filter.codec.ProtocolCodecFactory;   
  3. import org.apache.mina.filter.codec.ProtocolEncoder;   
  4. import org.apache.mina.filter.codec.ProtocolDecoder;   
  5. public class SMPPProtocolCodecFactory   
  6. implements ProtocolCodecFactory {   
  7. ProtocolDecoder decoder = new SMPPProtocolDecoder();   
  8. ProtocolEncoder encoder = new SMPPProtocolEncoder();   
  9. public ProtocolEncoder getEncoder() throws Exception {   
  10. return encoder;   
  11. }   
  12. public ProtocolDecoder getDecoder() throws Exception {   
  13. return decoder;   
  14. }   
  15. }  
package com.lizongbo.smpp.server.codec;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolDecoder;
public class SMPPProtocolCodecFactory
implements ProtocolCodecFactory {
ProtocolDecoder decoder = new SMPPProtocolDecoder();
ProtocolEncoder encoder = new SMPPProtocolEncoder();
public ProtocolEncoder getEncoder() throws Exception {
return encoder;
}
public ProtocolDecoder getDecoder() throws Exception {
return decoder;
}
}

 

数据包解码的ProtocolDecoder为

 

  1. package com.lizongbo.smpp.server.codec;   
  2. import org.apache.mina.filter.codec.ProtocolDecoder;   
  3. import org.apache.mina.common.IoSession;   
  4. import org.apache.mina.common.ByteBuffer;   
  5. import org.apache.mina.filter.codec.ProtocolDecoderOutput;   
  6. import ie.omk.smpp.message.SMPPProtocolException;   
  7. import ie.omk.smpp.BadCommandIDException;   
  8. import ie.omk.smpp.util.PacketFactory;   
  9. import org.apache.log4j.Logger;   
  10. import ie.omk.smpp.message.SMPPPacket;   
  11. import java.io.IOException;   
  12. public class SMPPProtocolDecoder   
  13. implements ProtocolDecoder {   
  14. protected Logger log = Logger.getLogger(getClass());   
  15. public void decode(IoSession session, ByteBuffer in,   
  16. ProtocolDecoderOutput out) throws Exception {   
  17. try {   
  18. if (in.remaining() >= 4 && (in.remaining() >= in.getInt(in.position()))) {   
  19. byte[] byteLen = new byte[4];   
  20. in.get(byteLen);   
  21. int len = readInt(byteLen);   
  22. //System.out.println(”消息长度为:” + len);   
  23. if (in.remaining() >= (len - 4)) {   
  24. byte[] cmds = new byte[len - 4];   
  25. in.get(cmds);   
  26. SMPPPacket pak = null;   
  27. int id = -1;   
  28. id = readInt(cmds);   
  29. pak = PacketFactory.newInstance(id);   
  30. if (pak != null) {   
  31. byte[] reqb = new byte[4 + len];   
  32. System.arraycopy(byteLen, 0, reqb, 04);   
  33. System.arraycopy(cmds, 0, reqb, 4, cmds.length);   
  34. pak.readFrom(reqb, 0);   
  35. if (log.isDebugEnabled()) {   
  36. StringBuilder sb = new StringBuilder(”收到的包为: “);   
  37. int l = pak.getLength();   
  38. int s = pak.getCommandStatus();   
  39. int n = pak.getSequenceNum();   
  40. sb.append(”commandId:”).append(Integer.toHexString(id))   
  41. .append(” len:”).append(Integer.toString(l))   
  42. .append(” commandStatus:”).append(Integer.toString(s))   
  43. .append(” sequenceNum:”).append(Integer.toString(n));   
  44. log.debug(sb);   
  45. }   
  46. out.write(pak);   
  47. }   
  48. }   
  49. }   
  50. }   
  51. catch (BadCommandIDException x) {   
  52. throw new SMPPProtocolException(”Unrecognised command received”, x);   
  53. }   
  54. }   
  55. public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws  
  56. Exception {   
  57. }   
  58. public void dispose(IoSession session) throws Exception {   
  59. }   
  60. public final int readInt(byte[] b) throws IOException {   
  61. int ch1 = b[0];   
  62. int ch2 = b[1];   
  63. int ch3 = b[2];   
  64. int ch4 = b[3];   
  65. // if ( (ch1 | ch2 | ch3 | ch4) < 0) {   
  66. // throw new EOFException();   
  67. // }   
  68. // see: http://618119.com/archives/2007/10/27/18.html   
  69. return ( ( (ch1 & 0xff) << 24) + ( (ch2 & 0xff) << 16) +   
  70. ( (ch3 & 0xff) << 8) + (ch4 << 0));   
  71. }   
  72. }  
package com.lizongbo.smpp.server.codec;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import ie.omk.smpp.message.SMPPProtocolException;
import ie.omk.smpp.BadCommandIDException;
import ie.omk.smpp.util.PacketFactory;
import org.apache.log4j.Logger;
import ie.omk.smpp.message.SMPPPacket;
import java.io.IOException;
public class SMPPProtocolDecoder
implements ProtocolDecoder {
protected Logger log = Logger.getLogger(getClass());
public void decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
try {
if (in.remaining() >= 4 && (in.remaining() >= in.getInt(in.position()))) {
byte[] byteLen = new byte[4];
in.get(byteLen);
int len = readInt(byteLen);
//System.out.println(”消息长度为:” + len);
if (in.remaining() >= (len - 4)) {
byte[] cmds = new byte[len - 4];
in.get(cmds);
SMPPPacket pak = null;
int id = -1;
id = readInt(cmds);
pak = PacketFactory.newInstance(id);
if (pak != null) {
byte[] reqb = new byte[4 + len];
System.arraycopy(byteLen, 0, reqb, 0, 4);
System.arraycopy(cmds, 0, reqb, 4, cmds.length);
pak.readFrom(reqb, 0);
if (log.isDebugEnabled()) {
StringBuilder sb = new StringBuilder(”收到的包为: “);
int l = pak.getLength();
int s = pak.getCommandStatus();
int n = pak.getSequenceNum();
sb.append(”commandId:”).append(Integer.toHexString(id))
.append(” len:”).append(Integer.toString(l))
.append(” commandStatus:”).append(Integer.toString(s))
.append(” sequenceNum:”).append(Integer.toString(n));
log.debug(sb);
}
out.write(pak);
}
}
}
}
catch (BadCommandIDException x) {
throw new SMPPProtocolException(”Unrecognised command received”, x);
}
}
public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws
Exception {
}
public void dispose(IoSession session) throws Exception {
}
public final int readInt(byte[] b) throws IOException {
int ch1 = b[0];
int ch2 = b[1];
int ch3 = b[2];
int ch4 = b[3];
// if ( (ch1 | ch2 | ch3 | ch4) < 0) {
// throw new EOFException();
// }
// see: http://618119.com/archives/2007/10/27/18.html
return ( ( (ch1 & 0xff) << 24) + ( (ch2 & 0xff) << 16) +
( (ch3 & 0xff) << 8) + (ch4 << 0));
}
}

 

数据包的ProtocolEncoder非常简单了:

 

  1. package com.lizongbo.smpp.server.codec;   
  2. import org.apache.mina.filter.codec.ProtocolEncoder;   
  3. import org.apache.mina.common.IoSession;   
  4. import org.apache.mina.filter.codec.ProtocolEncoderOutput;   
  5. import org.apache.mina.common.ByteBuffer;   
  6. import ie.omk.smpp.message.SMPPPacket;   
  7. import java.io.ByteArrayOutputStream;   
  8. public class SMPPProtocolEncoder   
  9. implements ProtocolEncoder {   
  10. public void encode(IoSession session, Object message,   
  11. ProtocolEncoderOutput out) throws Exception {   
  12. if (message == null) {   
  13. return;   
  14. }   
  15. SMPPPacket packet = (SMPPPacket) message;   
  16. ByteArrayOutputStream outb = new ByteArrayOutputStream(packet.getLength());   
  17. packet.writeTo(outb);   
  18. byte[] b = outb.toByteArray();   
  19. ByteBuffer buf = ByteBuffer.allocate(b.length, true);   
  20. buf.setAutoExpand(true);   
  21. buf.put(b);   
  22. buf.flip();   
  23. session.write(buf);   
  24. }   
  25. public void dispose(IoSession session) throws Exception {   
  26. }   
  27. }  
分享到:
评论

相关推荐

    java开源包10

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包3

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包4

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包2

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包8

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包1

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包11

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包6

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包5

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包7

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包9

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    Java资源包01

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包101

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

Global site tag (gtag.js) - Google Analytics