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 package org.apache.commons.httpclient.server;
32
33 import java.io.IOException;
34
35 import org.apache.commons.httpclient.Credentials;
36
37 /***
38 * Simple server that registers default request handlers to act as a proxy.
39 *
40 * @author Ortwin Glueck
41 * @author Oleg Kalnichevski
42 */
43 public class SimpleProxy extends SimpleHttpServer {
44
45 private SimpleConnManager connmanager = null;
46 private HttpRequestHandlerChain stdchain = null;
47
48 public SimpleProxy(int port) throws IOException {
49 super(port);
50 this.connmanager = new SimpleConnManager();
51 this.stdchain = new HttpRequestHandlerChain();
52 this.stdchain.appendHandler(new TransparentProxyRequestHandler());
53 this.stdchain.appendHandler(new ProxyRequestHandler(this.connmanager));
54 setRequestHandler(this.stdchain);
55 }
56
57 public SimpleProxy() throws IOException {
58 this(0);
59 }
60
61 public void requireAuthentication(final Credentials creds, final String realm, boolean keepalive) {
62 HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain);
63 chain.prependHandler(new ProxyAuthRequestHandler(creds ,realm, keepalive));
64 setRequestHandler(chain);
65 }
66
67 public void requireAuthentication(final Credentials creds) {
68 HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain);
69 chain.prependHandler(new ProxyAuthRequestHandler(creds));
70 setRequestHandler(chain);
71 }
72
73 public void destroy() {
74 super.destroy();
75 this.connmanager.shutdown();
76 }
77
78 public void addHandler(final HttpRequestHandler handler) {
79 this.stdchain.prependHandler(handler);
80 }
81
82 }