package org.apache.asyncweb.server.transport.mina; public class SingleHttpSessionIoHandler implements SingleSessionIoHandler { protected DefaultHttpServiceContext createContext( HttpRequest request ) { return new DefaultHttpServiceContext( request ); } private class ContextConverter extends IoFilterAdapter { @Override public void filterWrite( NextFilter nextFilter, IoSession session, WriteRequest writeRequest ) throws Exception { nextFilter.filterWrite( session, new DefaultWriteRequest( ( ( HttpServiceContext ) writeRequest.getMessage() ) .getCommittedResponse(), writeRequest.getFuture())); } @Override public void messageReceived( NextFilter nextFilter, IoSession session, Object message ) throws Exception { HttpRequest request = ( HttpRequest ) message; currentContext = createContext( request ); nextFilter.messageReceived( session, currentContext ); } } private class ServiceFilterAdapter extends IoFilterAdapter { private final HttpServiceFilter filter; public ServiceFilterAdapter( HttpServiceFilter filter ) { this.filter = filter; } @Override public void messageReceived( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception { HttpServiceFilter.NextFilter nextFilterAdapter = new HttpServiceFilter.NextFilter() { public void invoke() { nextFilter.messageReceived(session, message); } }; filter.handleRequest( nextFilterAdapter, ( HttpServiceContext ) message ); } @Override public void filterWrite( final NextFilter nextFilter, final IoSession session, final WriteRequest writeRequest ) throws Exception { HttpServiceFilter.NextFilter nextFilterAdapter = new HttpServiceFilter.NextFilter() { public void invoke() { nextFilter.filterWrite( session, writeRequest ); } }; HttpServiceContext context = ( HttpServiceContext ) writeRequest.getMessage(); filter.handleResponse( nextFilterAdapter, context ); } } private class DefaultHttpServiceContext extends AbstractHttpServiceContext { private WriteFuture writeFuture; private DefaultHttpServiceContext( HttpRequest request ) { super( ( InetSocketAddress ) session.getLocalAddress(), ( InetSocketAddress ) session.getRemoteAddress(), request, container ); } private WriteFuture getWriteFuture() { return writeFuture; } private void setWriteFuture( WriteFuture writeFuture ) { if ( ! isResponseCommitted() ) { throw new IllegalStateException(); } this.writeFuture = writeFuture; } @Override protected void doWrite( boolean requiresClosure ) { currentContext = null; WriteFuture future = session.write( this ); if ( requiresClosure ) { LOG.debug( "Added CLOSE future listener." ); future.addListener( IoFutureListener.CLOSE ); } } public void fireClientIdle( long idleTime, int idleCount ) { super.fireClientIdle( idleTime, idleCount ); } public void fireClientDisconnected() { super.fireClientDisconnected(); } } }ContextConverter 中的messageReceived,这个方法第三个参数是Object 型的消息,通过IoFilter 的过滤。
对象从最早的ioBuffer——》HttpRequest——》HttpServiceContext,最后通过ServiceFilterAdapter类进行
分发。到这里,HttpServiceContext和HttpService的实现类之间,就差他们是如何匹配的。
下期预告:《AsyncWeb原理分析(六)——分解器》