public HttpContext service ( HttpRequest request , HttpResponse response ) throws IOException , HttpException { String host = request . getHost ( ) ; if ( _requestsPerGC > 0 && _gcRequests ++ > _requestsPerGC ) { _gcRequests = 0 ; System . gc ( ) ; } while ( true ) { PathMap contextMap = ( PathMap ) _virtualHostMap . get ( host ) ; if ( contextMap != null ) { List contextLists = contextMap . getMatches ( request . getPath ( ) ) ; if ( contextLists != null ) { if ( log . isTraceEnabled ( ) ) log . trace ( "Contexts at " + request . getPath ( ) + ": " + contextLists ) ; for ( int i = 0 ; i < contextLists . size ( ) ; i ++ ) { Map . Entry entry = ( Map . Entry ) contextLists . get ( i ) ; List contextList = ( List ) entry . getValue ( ) ; for ( int j = 0 ; j < contextList . size ( ) ; j ++ ) { HttpContext context = ( HttpContext ) contextList . get ( j ) ; if ( log . isDebugEnabled ( ) ) log . debug ( "Try " + context + "," + j ) ; context . handle ( request , response ) ; if ( request . isHandled ( ) ) return context ; } } } } // try no host if ( host == null ) break ; host = null ; } synchronized ( this ) { if ( _notFoundContext == null ) { _notFoundContext = new HttpContext ( ) ; _notFoundContext . setContextPath ( "/" ) ; _notFoundContext . setHttpServer ( this ) ; try { _notFoundContext . addHandler ( ( NotFoundHandler ) Class . forName ( "org.browsermob.proxy.jetty.http.handler.RootNotFoundHandler" ) . newInstance ( ) ) ; } catch ( Exception e ) { _notFoundContext . addHandler ( new NotFoundHandler ( ) ) ; } addComponent ( _notFoundContext ) ; try { _notFoundContext . start ( ) ; } catch ( Exception e ) { log . warn ( LogSupport . EXCEPTION , e ) ; } } _notFoundContext . handle ( request , response ) ; if ( ! request . isHandled ( ) ) response . sendError ( HttpResponse . __404_Not_Found ) ; return _notFoundContext ; } }
Service a request . Handle the request by passing it to the HttpHandler contained in the mapped HttpContexts . The requests host and path are used to select a list of HttpContexts . Each HttpHandler in these context is offered the request in turn until the request is handled .