`
dawuafang
  • 浏览: 1105192 次
文章分类
社区版块
存档分类
最新评论

androidpn的学习研究(六)Androidpn-server的Mina编码和解码解析过程

 
阅读更多

在许多网络应用中可能针对传输的数据进行加密操作,接收到数据之后进行解码操作。

在mina中提供许多加密和解密的解析方式:

1.带一定前缀的字符串的解析方式。

2.序列化对象的字符串解析方式。

3.分隔符方式的字符串解析方式。

在mina中提供相关的filterchain支持相关的操作。

Mina的源代码如下:

Java代码收藏代码
  1. packageorg.apache.mina.filter.codec;
  2. importorg.apache.mina.core.session.IoSession;
  3. /**
  4. *Provides{@linkProtocolEncoder}and{@linkProtocolDecoder}whichtranslates
  5. *binaryorprotocolspecificdataintomessageobjectandviceversa.
  6. *<p>
  7. *Pleasereferto
  8. *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.html"><code>ReverserProtocolProvider</code></a>
  9. *example.
  10. *
  11. *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
  12. */
  13. publicinterfaceProtocolCodecFactory{
  14. /**
  15. *Returnsanew(orreusable)instanceof{@linkProtocolEncoder}which
  16. *encodesmessageobjectsintobinaryorprotocol-specificdata.
  17. */
  18. ProtocolEncodergetEncoder(IoSessionsession)throwsException;
  19. /**
  20. *Returnsanew(orreusable)instanceof{@linkProtocolDecoder}which
  21. *decodesbinaryorprotocol-specificdataintomessageobjects.
  22. */
  23. ProtocolDecodergetDecoder(IoSessionsession)throwsException;
  24. }

加密处理类:

Java代码收藏代码
  1. packageorg.apache.mina.filter.codec;
  2. importorg.apache.mina.core.buffer.IoBuffer;
  3. importorg.apache.mina.core.session.IoSession;
  4. /**
  5. *Encodeshigher-levelmessageobjectsintobinaryorprotocol-specificdata.
  6. *MINAinvokes{@link#encode(IoSession,Object,ProtocolEncoderOutput)}
  7. *methodwithmessagewhichispoppedfromthesessionwritequeue,andthen
  8. *theencoderimplementationputsencodedmessages(typically{@linkIoBuffer}s)
  9. *into{@linkProtocolEncoderOutput}bycalling{@linkProtocolEncoderOutput#write(Object)}.
  10. *<p>
  11. *Pleasereferto
  12. *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineEncoder.html"><code>TextLineEncoder</code></a>
  13. *example.
  14. *
  15. *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
  16. *
  17. *@seeProtocolEncoderException
  18. */
  19. publicinterfaceProtocolEncoder{
  20. /**
  21. *Encodeshigher-levelmessageobjectsintobinaryorprotocol-specificdata.
  22. *MINAinvokes{@link#encode(IoSession,Object,ProtocolEncoderOutput)}
  23. *methodwithmessagewhichispoppedfromthesessionwritequeue,andthen
  24. *theencoderimplementationputsencodedmessages(typically{@linkIoBuffer}s)
  25. *into{@linkProtocolEncoderOutput}.
  26. *
  27. *@throwsExceptionifthemessageviolatedprotocolspecification
  28. */
  29. voidencode(IoSessionsession,Objectmessage,ProtocolEncoderOutputout)
  30. throwsException;
  31. /**
  32. *Releasesallresourcesrelatedwiththisencoder.
  33. *
  34. *@throwsExceptioniffailedtodisposeallresources
  35. */
  36. voiddispose(IoSessionsession)throwsException;
  37. }

解密类如下:

Java代码收藏代码
  1. packageorg.apache.mina.filter.codec;
  2. importorg.apache.mina.core.buffer.IoBuffer;
  3. importorg.apache.mina.core.session.IoSession;
  4. /**
  5. *Decodesbinaryorprotocol-specificdataintohigher-levelmessageobjects.
  6. *MINAinvokes{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
  7. *methodwithreaddata,andthenthedecoderimplementationputsdecoded
  8. *messagesinto{@linkProtocolDecoderOutput}bycalling
  9. *{@linkProtocolDecoderOutput#write(Object)}.
  10. *<p>
  11. *Pleasereferto
  12. *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html"><code>TextLineDecoder</code></a>
  13. *example.
  14. *
  15. *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
  16. *
  17. *@seeProtocolDecoderException
  18. */
  19. publicinterfaceProtocolDecoder{
  20. /**
  21. *Decodesbinaryorprotocol-specificcontentintohigher-levelmessageobjects.
  22. *MINAinvokes{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
  23. *methodwithreaddata,andthenthedecoderimplementationputsdecoded
  24. *messagesinto{@linkProtocolDecoderOutput}.
  25. *
  26. *@throwsExceptionifthereaddataviolatedprotocolspecification
  27. */
  28. voiddecode(IoSessionsession,IoBufferin,ProtocolDecoderOutputout)
  29. throwsException;
  30. /**
  31. *Invokedwhenthespecified<tt>session</tt>isclosed.Thismethodisuseful
  32. *whenyoudealwiththeprotocolwhichdoesn'tspecifythelengthofamessage
  33. *suchasHTTPresponsewithout<tt>content-length</tt>header.Implementthis
  34. *methodtoprocesstheremainingdatathat{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
  35. *methoddidn'tprocesscompletely.
  36. *
  37. *@throwsExceptionifthereaddataviolatedprotocolspecification
  38. */
  39. voidfinishDecode(IoSessionsession,ProtocolDecoderOutputout)
  40. throwsException;
  41. /**
  42. *Releasesallresourcesrelatedwiththisdecoder.
  43. *
  44. *@throwsExceptioniffailedtodisposeallresources
  45. */
  46. voiddispose(IoSessionsession)throwsException;
  47. }

在androidpn中的没有采取任何加密方式,但是提供相关的类org.androidpn.server.xmpp.codec,如果需要可以针对传输的数据进行加密和解密工作。

Java代码收藏代码
  1. packageorg.androidpn.server.xmpp.codec;
  2. importorg.apache.mina.core.session.IoSession;
  3. importorg.apache.mina.filter.codec.ProtocolCodecFactory;
  4. importorg.apache.mina.filter.codec.ProtocolDecoder;
  5. importorg.apache.mina.filter.codec.ProtocolEncoder;
  6. /**
  7. *FactoryclassthatspecifiestheencodeanddecodertouseforparsingXMPPstanzas.
  8. *
  9. *@authorSehwanNoh(devnoh@gmail.com)
  10. */
  11. publicclassXmppCodecFactoryimplementsProtocolCodecFactory{
  12. privatefinalXmppEncoderencoder;
  13. privatefinalXmppDecoderdecoder;
  14. /**
  15. *Constructor.
  16. */
  17. publicXmppCodecFactory(){
  18. encoder=newXmppEncoder();
  19. decoder=newXmppDecoder();
  20. }
  21. /**
  22. *Returnsanew(orreusable)instanceofProtocolEncoder.
  23. */
  24. publicProtocolEncodergetEncoder(IoSessionsession)throwsException{
  25. returnencoder;
  26. }
  27. /**
  28. *Returnsanew(orreusable)instanceofProtocolDecoder.
  29. */
  30. publicProtocolDecodergetDecoder(IoSessionsession)throwsException{
  31. returndecoder;
  32. }
  33. }

androidpn的解密类:

Java代码收藏代码
  1. packageorg.androidpn.server.xmpp.codec;
  2. importorg.androidpn.server.xmpp.net.XmppIoHandler;
  3. importorg.apache.mina.core.buffer.IoBuffer;
  4. importorg.apache.mina.core.session.IoSession;
  5. importorg.apache.mina.filter.codec.CumulativeProtocolDecoder;
  6. importorg.apache.mina.filter.codec.ProtocolDecoderOutput;
  7. importorg.jivesoftware.openfire.nio.XMLLightweightParser;
  8. /**
  9. *DecoderclassthatparsesByteBuffersandgeneratesXMLstanzas.
  10. *
  11. *@authorSehwanNoh(devnoh@gmail.com)
  12. */
  13. publicclassXmppDecoderextendsCumulativeProtocolDecoder{
  14. //privatefinalLoglog=LogFactory.getLog(XmppDecoder.class);
  15. @Override
  16. publicbooleandoDecode(IoSessionsession,IoBufferin,
  17. ProtocolDecoderOutputout)throwsException{
  18. //log.debug("doDecode(...)...");
  19. XMLLightweightParserparser=(XMLLightweightParser)session
  20. .getAttribute(XmppIoHandler.XML_PARSER);
  21. parser.read(in);
  22. if(parser.areThereMsgs()){
  23. for(Stringstanza:parser.getMsgs()){
  24. out.write(stanza);
  25. }
  26. }
  27. return!in.hasRemaining();
  28. }
  29. }

androidpn的加密类:

Java代码收藏代码
  1. packageorg.androidpn.server.xmpp.codec;
  2. importorg.apache.mina.core.session.IoSession;
  3. importorg.apache.mina.filter.codec.ProtocolEncoder;
  4. importorg.apache.mina.filter.codec.ProtocolEncoderOutput;
  5. /**
  6. *Encoderclassthatdoesnothing(tothealreadyencodeddata).
  7. *
  8. *@authorSehwanNoh(devnoh@gmail.com)
  9. */
  10. publicclassXmppEncoderimplementsProtocolEncoder{
  11. //privatefinalLoglog=LogFactory.getLog(XmppEncoder.class);
  12. publicvoidencode(IoSessionsession,Objectmessage,
  13. ProtocolEncoderOutputout)throwsException{
  14. //log.debug("encode()...");
  15. }
  16. publicvoiddispose(IoSessionsession)throwsException{
  17. //log.debug("dispose()...");
  18. }
  19. }

最终将编码解析器转换为过滤器使用,具体配置如下:

Xml代码收藏代码
  1. <beanclass="org.apache.mina.filter.codec.ProtocolCodecFilter">
  2. <constructor-arg>
  3. <beanclass="org.androidpn.server.xmpp.codec.XmppCodecFactory"/>
  4. </constructor-arg>
  5. </bean>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics