55 * handler 56 */ 57 private ListIterator<Filter> iter; 58 private HttpHandler handler; 59 60 public Chain (List<Filter> filters, HttpHandler handler) { 61 iter = filters.listIterator(); 62 this.handler = handler; 63 } 64 65 /** 66 * calls the next filter in the chain, or else 67 * the users exchange handler, if this is the 68 * final filter in the chain. The Filter may decide 69 * to terminate the chain, by not calling this method. 70 * In this case, the filter <b>must</b> send the 71 * response to the request, because the application's 72 * exchange handler will not be invoked. 73 * @param exchange the HttpExchange 74 * @throws IOException let exceptions pass up the stack 75 * @throws NullPointerException if exchange is <code>null</code> 76 */ 77 public void doFilter (HttpExchange exchange) throws IOException { 78 if (!iter.hasNext()) { 79 handler.handle (exchange); 80 } else { 81 Filter f = iter.next(); 82 f.doFilter (exchange, this); 83 } 84 } 85 } 86 87 /** 88 * Asks this filter to pre/post-process the given exchange. The filter 89 * can :- 90 * <ul><li>examine or modify the request headers</li> 91 * <li>filter the request body or the response body, by creating suitable 92 * filter streams and calling 93 * {@link HttpExchange#setStreams(InputStream,OutputStream)}</li> 94 * <li>set attribute Objects in the exchange, which other filters or the 95 * exchange handler can access.</li> 96 * <li>decide to either :-<ol> 97 * <li>invoke the next filter in the chain, by calling 98 * {@link Filter.Chain#doFilter(HttpExchange)}</li> 99 * <li>terminate the chain of invocation, by <b>not</b> calling 100 * {@link Filter.Chain#doFilter(HttpExchange)}</li></ol> 101 * <li>if option 1. above taken, then when doFilter() returns all subsequent 102 * filters in the Chain have been called, and the response headers can be 103 * examined or modified.</li> 104 * <li>if option 2. above taken, then this Filter must use the HttpExchange 105 * to send back an appropriate response</li></ul><p> 106 * @param exchange the <code>HttpExchange</code> to be filtered. 107 * @param chain the Chain which allows the next filter to be invoked. 108 * @throws IOException may be thrown by any filter module, and if 109 * caught, must be rethrown again. 110 * @throws NullPointerException if either exchange or chain are <code>null</code> 111 */ 112 public abstract void doFilter (HttpExchange exchange, Chain chain) 113 throws IOException; 114 115 /** 116 * returns a short description of this Filter 117 * @return a string describing the Filter 118 */ 119 public abstract String description (); 120 121 } | 55 * handler 56 */ 57 private ListIterator<Filter> iter; 58 private HttpHandler handler; 59 60 public Chain (List<Filter> filters, HttpHandler handler) { 61 iter = filters.listIterator(); 62 this.handler = handler; 63 } 64 65 /** 66 * calls the next filter in the chain, or else 67 * the users exchange handler, if this is the 68 * final filter in the chain. The Filter may decide 69 * to terminate the chain, by not calling this method. 70 * In this case, the filter <b>must</b> send the 71 * response to the request, because the application's 72 * exchange handler will not be invoked. 73 * @param exchange the HttpExchange 74 * @throws IOException let exceptions pass up the stack 75 * @throws NullPointerException if exchange is {@code null} 76 */ 77 public void doFilter (HttpExchange exchange) throws IOException { 78 if (!iter.hasNext()) { 79 handler.handle (exchange); 80 } else { 81 Filter f = iter.next(); 82 f.doFilter (exchange, this); 83 } 84 } 85 } 86 87 /** 88 * Asks this filter to pre/post-process the given exchange. The filter 89 * can: 90 * <ul><li>examine or modify the request headers</li> 91 * <li>filter the request body or the response body, by creating suitable 92 * filter streams and calling 93 * {@link HttpExchange#setStreams(InputStream,OutputStream)}</li> 94 * <li>set attribute Objects in the exchange, which other filters or the 95 * exchange handler can access.</li> 96 * <li>decide to either<ol> 97 * <li>invoke the next filter in the chain, by calling 98 * {@link Filter.Chain#doFilter(HttpExchange)}</li> 99 * <li>terminate the chain of invocation, by <b>not</b> calling 100 * {@link Filter.Chain#doFilter(HttpExchange)}</li></ol> 101 * <li>if option 1. above taken, then when doFilter() returns all subsequent 102 * filters in the Chain have been called, and the response headers can be 103 * examined or modified.</li> 104 * <li>if option 2. above taken, then this Filter must use the HttpExchange 105 * to send back an appropriate response</li></ul> 106 * 107 * @param exchange the {@code HttpExchange} to be filtered. 108 * @param chain the Chain which allows the next filter to be invoked. 109 * @throws IOException may be thrown by any filter module, and if 110 * caught, must be rethrown again. 111 * @throws NullPointerException if either exchange or chain are {@code null} 112 */ 113 public abstract void doFilter (HttpExchange exchange, Chain chain) 114 throws IOException; 115 116 /** 117 * returns a short description of this Filter 118 * @return a string describing the Filter 119 */ 120 public abstract String description (); 121 122 } |