1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient.server;
33
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39 /***
40 * Maintains a chain of {@link HttpRequestHandler}s where new request-handlers
41 * can be prepended/appended.
42 *
43 * For each call to {@link #processRequest(ResponseWriter,SimpleHttpServerConnection,RequestLine,Header[])}
44 * we iterate over the chain from the start to the end, stopping as soon as a handler
45 * has claimed the output.
46 *
47 * @author Christian Kohlschuetter
48 */
49 public class HttpRequestHandlerChain implements HttpRequestHandler {
50
51 private List subhandlers = new ArrayList();
52
53 public HttpRequestHandlerChain(final HttpRequestHandlerChain chain) {
54 super();
55 if (chain != null) {
56 this.subhandlers.clear();
57 this.subhandlers.addAll(chain.subhandlers);
58 }
59 }
60
61 public HttpRequestHandlerChain() {
62 super();
63 }
64
65 public synchronized void clear() {
66 subhandlers.clear();
67 }
68
69 public synchronized void prependHandler(HttpRequestHandler handler) {
70 subhandlers.add(0,handler);
71 }
72
73 public synchronized void appendHandler(HttpRequestHandler handler) {
74 subhandlers.add(handler);
75 }
76
77 public synchronized boolean processRequest(
78 final SimpleHttpServerConnection conn,
79 final SimpleRequest request) throws IOException
80 {
81 for(Iterator it=subhandlers.iterator();it.hasNext();) {
82 HttpRequestHandler h = (HttpRequestHandler)it.next();
83 boolean stop = h.processRequest(conn, request);
84 if (stop) {
85 return true;
86 }
87 }
88 return false;
89 }
90 }