---
title: "用Spring,smppapi,mina, commons ssl快速实现安全的smp(2)"
author: "lanmh"
date: "2008-07-21T15:37:04Z"
tags: ["Apache", "IE", "Mina", "Spring", "log4j"]
slug: "spring-smppapi-mina-commons-ssl-smp-2"
draft: false
lastmod: "2008-07-21T15:37:04Z"
id: 216953
url: "https://lanmh.iteye.com/blog/216953"
---

作者：[lizongbo](http://618119.com/)  
[版权声明](http://creativecommons.org/licenses/by/3.0/deed.zh)：可以任意转载，转载时请务必以超链接形式标明文章[原始出处](http://618119.com/archives/2007/11/29/34.html)和作者信息及[本版权声明](http://618119.com/archives/2007/11/29/34.html)。  
[http://618119.com/archives/2007/11/29/34.html](http://618119.com/archives/2007/11/29/34.html)

* * *

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

对[smpp](http://618119.com/tag/smpp "标签 smpp 下的日志") 数据包进行编码和解码的相关代码如下:

ProtocolCodecFactory的代码为:

 

[view plain](http://618119.com/archives/2007/11/29/34.html)[copy to clipboard](http://618119.com/archives/2007/11/29/34.html)[print](http://618119.com/archives/2007/11/29/34.html)[?](http://618119.com/archives/2007/11/29/34.html)

01. package com.lizongbo.smpp.server.codec;
02. import org.apache.mina.filter.codec.ProtocolCodecFactory;
03. import org.apache.mina.filter.codec.ProtocolEncoder;
04. import org.apache.mina.filter.codec.ProtocolDecoder;
05. public class SMPPProtocolCodecFactory
06. implements ProtocolCodecFactory {
07. ProtocolDecoder decoder = new SMPPProtocolDecoder();
08. ProtocolEncoder encoder = new SMPPProtocolEncoder();
09. 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为

 

[view plain](http://618119.com/archives/2007/11/29/34.html)[copy to clipboard](http://618119.com/archives/2007/11/29/34.html)[print](http://618119.com/archives/2007/11/29/34.html)[?](http://618119.com/archives/2007/11/29/34.html)

01. package com.lizongbo.smpp.server.codec;
02. import org.apache.mina.filter.codec.ProtocolDecoder;
03. import org.apache.mina.common.IoSession;
04. import org.apache.mina.common.ByteBuffer;
05. import org.apache.mina.filter.codec.ProtocolDecoderOutput;
06. import ie.omk.smpp.message.SMPPProtocolException;
07. import ie.omk.smpp.BadCommandIDException;
08. import ie.omk.smpp.util.PacketFactory;
09. 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() &gt;= 4 && (in.remaining() &gt;= 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() &gt;= (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, 0, 4);
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) &lt; 0) {
66. // throw new EOFException();
67. // }
68. // see: http://618119.com/archives/2007/10/27/18.html
69. return ( ( (ch1 & 0xff) &lt;&lt; 24) + ( (ch2 & 0xff) &lt;&lt; 16) +
70. ( (ch3 & 0xff) &lt;&lt; 8) + (ch4 &lt;&lt; 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非常简单了:

 

[view plain](http://618119.com/archives/2007/11/29/34.html)[copy to clipboard](http://618119.com/archives/2007/11/29/34.html)[print](http://618119.com/archives/2007/11/29/34.html)[?](http://618119.com/archives/2007/11/29/34.html)

01. package com.lizongbo.smpp.server.codec;
02. import org.apache.mina.filter.codec.ProtocolEncoder;
03. import org.apache.mina.common.IoSession;
04. import org.apache.mina.filter.codec.ProtocolEncoderOutput;
05. import org.apache.mina.common.ByteBuffer;
06. import ie.omk.smpp.message.SMPPPacket;
07. import java.io.ByteArrayOutputStream;
08. public class SMPPProtocolEncoder
09. 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. }