1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java,v 1.8 2004/12/11 22:35:26 olegk Exp $
3    * $Revision: 1.8 $
4    * $Date: 2004-12-11 17:38:35 -0500 (Sat, 11 Dec 2004) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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  }