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