在许多网络应用中可能针对传输的数据进行加密操作,接收到数据之后进行解码操作。
在mina中提供许多加密和解密的解析方式:
1.带一定前缀的字符串的解析方式。
2.序列化对象的字符串解析方式。
3.分隔符方式的字符串解析方式。
在mina中提供相关的filterchain支持相关的操作。
Mina的源代码如下:
- packageorg.apache.mina.filter.codec;
- importorg.apache.mina.core.session.IoSession;
- /**
- *Provides{@linkProtocolEncoder}and{@linkProtocolDecoder}whichtranslates
- *binaryorprotocolspecificdataintomessageobjectandviceversa.
- *<p>
- *Pleasereferto
- *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.html"><code>ReverserProtocolProvider</code></a>
- *example.
- *
- *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
- */
- publicinterfaceProtocolCodecFactory{
- /**
- *Returnsanew(orreusable)instanceof{@linkProtocolEncoder}which
- *encodesmessageobjectsintobinaryorprotocol-specificdata.
- */
- ProtocolEncodergetEncoder(IoSessionsession)throwsException;
- /**
- *Returnsanew(orreusable)instanceof{@linkProtocolDecoder}which
- *decodesbinaryorprotocol-specificdataintomessageobjects.
- */
- ProtocolDecodergetDecoder(IoSessionsession)throwsException;
- }
加密处理类:
- packageorg.apache.mina.filter.codec;
- importorg.apache.mina.core.buffer.IoBuffer;
- importorg.apache.mina.core.session.IoSession;
- /**
- *Encodeshigher-levelmessageobjectsintobinaryorprotocol-specificdata.
- *MINAinvokes{@link#encode(IoSession,Object,ProtocolEncoderOutput)}
- *methodwithmessagewhichispoppedfromthesessionwritequeue,andthen
- *theencoderimplementationputsencodedmessages(typically{@linkIoBuffer}s)
- *into{@linkProtocolEncoderOutput}bycalling{@linkProtocolEncoderOutput#write(Object)}.
- *<p>
- *Pleasereferto
- *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineEncoder.html"><code>TextLineEncoder</code></a>
- *example.
- *
- *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
- *
- *@seeProtocolEncoderException
- */
- publicinterfaceProtocolEncoder{
- /**
- *Encodeshigher-levelmessageobjectsintobinaryorprotocol-specificdata.
- *MINAinvokes{@link#encode(IoSession,Object,ProtocolEncoderOutput)}
- *methodwithmessagewhichispoppedfromthesessionwritequeue,andthen
- *theencoderimplementationputsencodedmessages(typically{@linkIoBuffer}s)
- *into{@linkProtocolEncoderOutput}.
- *
- *@throwsExceptionifthemessageviolatedprotocolspecification
- */
- voidencode(IoSessionsession,Objectmessage,ProtocolEncoderOutputout)
- throwsException;
- /**
- *Releasesallresourcesrelatedwiththisencoder.
- *
- *@throwsExceptioniffailedtodisposeallresources
- */
- voiddispose(IoSessionsession)throwsException;
- }
解密类如下:
- packageorg.apache.mina.filter.codec;
- importorg.apache.mina.core.buffer.IoBuffer;
- importorg.apache.mina.core.session.IoSession;
- /**
- *Decodesbinaryorprotocol-specificdataintohigher-levelmessageobjects.
- *MINAinvokes{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
- *methodwithreaddata,andthenthedecoderimplementationputsdecoded
- *messagesinto{@linkProtocolDecoderOutput}bycalling
- *{@linkProtocolDecoderOutput#write(Object)}.
- *<p>
- *Pleasereferto
- *<ahref="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html"><code>TextLineDecoder</code></a>
- *example.
- *
- *@author<ahref="http://mina.apache.org">ApacheMINAProject</a>
- *
- *@seeProtocolDecoderException
- */
- publicinterfaceProtocolDecoder{
- /**
- *Decodesbinaryorprotocol-specificcontentintohigher-levelmessageobjects.
- *MINAinvokes{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
- *methodwithreaddata,andthenthedecoderimplementationputsdecoded
- *messagesinto{@linkProtocolDecoderOutput}.
- *
- *@throwsExceptionifthereaddataviolatedprotocolspecification
- */
- voiddecode(IoSessionsession,IoBufferin,ProtocolDecoderOutputout)
- throwsException;
- /**
- *Invokedwhenthespecified<tt>session</tt>isclosed.Thismethodisuseful
- *whenyoudealwiththeprotocolwhichdoesn'tspecifythelengthofamessage
- *suchasHTTPresponsewithout<tt>content-length</tt>header.Implementthis
- *methodtoprocesstheremainingdatathat{@link#decode(IoSession,IoBuffer,ProtocolDecoderOutput)}
- *methoddidn'tprocesscompletely.
- *
- *@throwsExceptionifthereaddataviolatedprotocolspecification
- */
- voidfinishDecode(IoSessionsession,ProtocolDecoderOutputout)
- throwsException;
- /**
- *Releasesallresourcesrelatedwiththisdecoder.
- *
- *@throwsExceptioniffailedtodisposeallresources
- */
- voiddispose(IoSessionsession)throwsException;
- }
在androidpn中的没有采取任何加密方式,但是提供相关的类org.androidpn.server.xmpp.codec,如果需要可以针对传输的数据进行加密和解密工作。
- packageorg.androidpn.server.xmpp.codec;
- importorg.apache.mina.core.session.IoSession;
- importorg.apache.mina.filter.codec.ProtocolCodecFactory;
- importorg.apache.mina.filter.codec.ProtocolDecoder;
- importorg.apache.mina.filter.codec.ProtocolEncoder;
- /**
- *FactoryclassthatspecifiestheencodeanddecodertouseforparsingXMPPstanzas.
- *
- *@authorSehwanNoh(devnoh@gmail.com)
- */
- publicclassXmppCodecFactoryimplementsProtocolCodecFactory{
- privatefinalXmppEncoderencoder;
- privatefinalXmppDecoderdecoder;
- /**
- *Constructor.
- */
- publicXmppCodecFactory(){
- encoder=newXmppEncoder();
- decoder=newXmppDecoder();
- }
- /**
- *Returnsanew(orreusable)instanceofProtocolEncoder.
- */
- publicProtocolEncodergetEncoder(IoSessionsession)throwsException{
- returnencoder;
- }
- /**
- *Returnsanew(orreusable)instanceofProtocolDecoder.
- */
- publicProtocolDecodergetDecoder(IoSessionsession)throwsException{
- returndecoder;
- }
- }
androidpn的解密类:
- packageorg.androidpn.server.xmpp.codec;
- importorg.androidpn.server.xmpp.net.XmppIoHandler;
- importorg.apache.mina.core.buffer.IoBuffer;
- importorg.apache.mina.core.session.IoSession;
- importorg.apache.mina.filter.codec.CumulativeProtocolDecoder;
- importorg.apache.mina.filter.codec.ProtocolDecoderOutput;
- importorg.jivesoftware.openfire.nio.XMLLightweightParser;
- /**
- *DecoderclassthatparsesByteBuffersandgeneratesXMLstanzas.
- *
- *@authorSehwanNoh(devnoh@gmail.com)
- */
- publicclassXmppDecoderextendsCumulativeProtocolDecoder{
- //privatefinalLoglog=LogFactory.getLog(XmppDecoder.class);
- @Override
- publicbooleandoDecode(IoSessionsession,IoBufferin,
- ProtocolDecoderOutputout)throwsException{
- //log.debug("doDecode(...)...");
- XMLLightweightParserparser=(XMLLightweightParser)session
- .getAttribute(XmppIoHandler.XML_PARSER);
- parser.read(in);
- if(parser.areThereMsgs()){
- for(Stringstanza:parser.getMsgs()){
- out.write(stanza);
- }
- }
- return!in.hasRemaining();
- }
- }
androidpn的加密类:
- packageorg.androidpn.server.xmpp.codec;
- importorg.apache.mina.core.session.IoSession;
- importorg.apache.mina.filter.codec.ProtocolEncoder;
- importorg.apache.mina.filter.codec.ProtocolEncoderOutput;
- /**
- *Encoderclassthatdoesnothing(tothealreadyencodeddata).
- *
- *@authorSehwanNoh(devnoh@gmail.com)
- */
- publicclassXmppEncoderimplementsProtocolEncoder{
- //privatefinalLoglog=LogFactory.getLog(XmppEncoder.class);
- publicvoidencode(IoSessionsession,Objectmessage,
- ProtocolEncoderOutputout)throwsException{
- //log.debug("encode()...");
- }
- publicvoiddispose(IoSessionsession)throwsException{
- //log.debug("dispose()...");
- }
- }
最终将编码解析器转换为过滤器使用,具体配置如下:
- <beanclass="org.apache.mina.filter.codec.ProtocolCodecFilter">
- <constructor-arg>
- <beanclass="org.androidpn.server.xmpp.codec.XmppCodecFactory"/>
- </constructor-arg>
- </bean>